Garbage Collection in C

garbage collection in C

What is Garbage Collection ?

Garbage Collection(GC) is an automatic mechanism in many of the program languages, that helps the programmer in freeing up extra blocks of memory, which will not be used by the program any more. Programmers may dynamically allocate memory, whenever they require it, but when a block is no longer needed, they do not have to return it to the system explicitly (with a free() call)

A GC has two goals:

  • Any unused memory should be freed, and
  • No memory should be freed unless the program will not use it anymore.

Although some languages allow memory to be manually freed as well, many do not.

Does C have a ?

If we talk about C and C++ having a Garbage Collector, one would say that it is of no use, but that would mean, that the person is living in the 90’s era. At that time the programs were not that complex, and programmers would have liked to do these things on there own. But, not today, Garbage Collector gives the ease of using memory to the programmer without being in the thought of freeing up unused memory.

Although C and C++, don’t have a garbage collector of their own, but a programmer can use a explicit garbage collector if he wants to.

Points to be remember while using a Superficial Garbage Collector:

    1. Instead of calling malloc() to acquire memory, callCollectMalloc(bytes, ptrCount).

    2. All structures that have pointers to other structures must have the pointers at the top of the structure. This permits the garbage collector to know which
elements of a structure are pointers:

typedef struct Node {
struct Node *next, *prev; //must be at the top
int value;
} Node;

    3. When acquiring memory with CollectMalloc(bytes,ptrCount), the number of pointers at the top of the structure must be specified in ptrCount.

    4. All fixed root pointers such as head and tail pointer smust be registered by calling CollectRoot(&head).

    5. The garbage collection function must be explicitly called.

With these limitations, a garbage collector can determine which data structures are no longer referenced so that they can be freed. When CollectMalloc() is called, the garbage collector places an OBJECTheader above each requested block of memory:

typedef struct OBJECT {
  unsigned char magic;   //Contains 0x47
  unsigned char referenced;   //Is this objectreferenced? 
  unsigned char ptrCount;   //Number of GC pointers in object
  unsigned char index;   //Number of followed GC pointers
  struct OBJECT *next;   //Next object in linked list of objects
  struct OBJECT *prev;   //Previous object in linked list
} OBJECT;

 

 

How does Garbage collection works in C ?

Garbage Collector uses heap data structure for storing the free memory blocks in the storage. The use of Garbage Collector helps the programmer in focusing more on the use of algorithms and data structures, rather than worrying about insignificant memory leaks.

How to use a GC in C ?

#define Prep_NODE_PTR_COUNT
  typedef struct PrepNode {
  struct PrepNode _gc   *left,  *right;   //pointers at top 
  int value; 
} PrepNode;

PrepNode *top, *n0, *n1, *n2;

int main()
    {
       n0 = (PrepNode*)CollectMalloc(sizeof(PrepNode),  Prep_NODE_PTR_COUNT);
       n1 = (PrepNode*)CollectMalloc(sizeof(PrepNode),  Prep_NODE_PTR_COUNT);
       n2 = (PrepNode*)CollectMalloc(sizeof(PrepNode),  Prep_NODE_PTR_COUNT);

         CollectRoot(&top);   //define a fixedroot node
         top = n0;
         n0->left = n2;

       CollectGarbage();   //frees unreferenced n1
       top = NULL;
       CollectGarbage();   //frees n0 and n2
       return 0;
}