Heap fragmentation
- From: Thomas Ruschival <Thomas@xxxxxxxxxxxx>
- Date: Thu, 13 Apr 2006 10:06:23 +0200
Hi groups,
in University I learnt about heap fragmentation, and I wanted to measure
it by some example code written in C running linux. Strangely I
couldn't measure any timing differences.
What I do is allocate 500 000 chunks of memory all randomly sized between
1 and 1234 bytes and place them in an array of fixed size. This action is
measured. Then I free each element of the array again.
over this procedure I cycle a hundred times.
After the first cycle I expect to have a horribly fragmented heap but
_all_ cycles take the same time to execute plus minus 4 milliseconds
which I think come form scheduling.
I there any wrong in my assumption or measurement, or is there a fancy
low fragmentation heap management in Linux glibc?
Please bring light in my understanding? you can have a look at the code
below.
Thanks in advance!
Thomas
<------------ Code used for measurement ------------------>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
/* number of objects to be created */
#define NO_HEAPOBJS 500000
/* maximum size of each object */
#define MAX_OBJSIZE 1234
/* how many cycles the progam should run */
#define NO_CYCLES 300
typedef struct memchunk{
unsigned int size;
char* memory;
} memchunk_t;
int main(int argc, char** argv){
int ret = 0;
int cycle = 0;
int obj_idx = 0;
memchunk_t objs[NO_HEAPOBJS];
size_t size;
double delta;
struct timeval starttime;
struct timeval stoptime;
while(cycle < NO_CYCLES){
printf("Cycle %d allocating %d objects of sizes between 1
and %d Bytes \n",cycle,
NO_HEAPOBJS,MAX_OBJSIZE); gettimeofday(&starttime,NULL);
for(obj_idx=0; obj_idx< NO_HEAPOBJS ; obj_idx++){
size = (size_t)(( random() % MAX_OBJSIZE)+1);
objs[obj_idx].size = size;
objs[obj_idx].memory =
(char*)malloc(sizeof(char)*size);
}
gettimeofday(&stoptime,NULL);
delta = (stoptime.tv_sec - starttime.tv_sec);
delta += (stoptime.tv_usec -
starttime.tv_usec)/(double)1000000;
printf("Time spent: %lf s \n",delta);
for(obj_idx=0; obj_idx<NO_HEAPOBJS ;obj_idx++){
free(objs[obj_idx].memory);
}
cycle++;
}
return ret;
}
.
- Follow-Ups:
- Re: Heap fragmentation
- From: Nobody Here
- Re: Heap fragmentation
- Prev by Date: 2006 IEEE 1588 Conference
- Next by Date: Re: Heap fragmentation
- Previous by thread: 2006 IEEE 1588 Conference
- Next by thread: Re: Heap fragmentation
- Index(es):
Relevant Pages
|