[Gc] dynamic structures and gc

Mark Richards mark.richards at massmicro.com
Fri Sep 2 12:14:07 PDT 2011


gnu c
linux: built gc for  armv5tejl GNU/Linux
gc version 6.8


My code uses a bunch of dynamically allocated structures and I have 
turned to gc as it seems when I free the structures (in the same manner 
as in the example below) the heap just keeps growing.  It is as if I had 
not freed at all.

Here is a test case:

struct _aemErrors
{
     int stored;
     int mailed
     char *cFunction;
     char *cDesc;
};
struct _aemErrors **aemErrors;
int aemErrorsCount;

// print the heap size here
printf("Heap size = %d\n", GC_get_heap_size());
// add some
int i=0;
for (i=0;i<100;i++)
{

     ++aemErrorsCount;
     aemErrors = (struct _aemErrors **)realloc(aemErrors, 
(aemErrorsCount+1) * sizeof(struct _aemErrors *));
     aemErrors[aemErrorsCount] = (struct _aemErrors 
*)malloc(sizeof(struct _aemErrors));    return(aemErrorsCount);

     aemErrors[aemErrorsCount]->cFunction=strdup("this is just a test");
     aemErrors[aemErrorsCount]->cDesc=strdup("this is just a test");

}
// print the heap size here
printf("Heap size = %d\n", GC_get_heap_size());

// now free them
for (i=0;i<100;i++)
{
     GC_free(aemErrors[i]->cFunction);
     GC_free(aemErrors[i]->cDesc);
     GC_free(aemErrors[i]);
}
GC_free(aemErrors);
// print the heap size here
printf("Heap size = %d\n", GC_get_heap_size());



The heap reports 0 when we start, something (significant) after we do 
the allocation and, I see the same large heap size after the free 
process.  Same as my test case without using gc.

I am forcing GC_free() as I wish to see if in fact the allocated memory 
is returned to the system.  It does not seem to be happening.. or 
perhaps I am missing something here.

So question: If I do not use GC_free, how does gc determine when to 
reclaim allocated memory?  In the case of my dynamic structure, many of 
these are built when my program starts and many stay alive always, where 
others are built and discarded.  It would be not nice if gc freed memory 
I had built for a structure, and then I need access to it.

/m





More information about the Gc mailing list