Heap fragmentation



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;
}






.



Relevant Pages

  • Re: Calculating Virtual Memory Fragmentation
    ... When does an allocation fail because of an fragmentation pattern ... Ite depends of the kind of allocation. ... For heap allocations greater then 0xFFFE * sizeof, ... To calculate the largest available virtual-address, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Calculating Virtual Memory Fragmentation
    ... Of course, this considers all heap segments in use even if they have sub-blocks that are not yet in use, since VirtualQuery knows nothing of process heaps. ... VM fragmentation usually only causes problems when you have mostly exhausted the available address space. ... There is no contiguous virtual memory space large enough for the ...
    (microsoft.public.win32.programmer.kernel)
  • would yo please provide help?
    ... i have written a code of a 'MINIMUM' Heap the code seems ... private int maxSize; ... int largerChild; ... String sel; ...
    (comp.lang.java.programmer)
  • Re: smail remote and local root holes (really, it is exploitable)
    ... * heap is at 0x080d.. ... ssize_t Send(int s, const void *buf, size_t len, int flags) ...
    (Bugtraq)
  • Re: Heap fragmentation (2nd attempt)
    ... fragments the heap and measure the time it take to allocate memory. ... heap fragmentation depends on the malloc package much ...
    (comp.arch.embedded)