Saturday 19 July 2014

Garbage Collection

Memory is crucial resource in Many Applications. So if you don't manage your memory properly in your Application, then your application will stop execution or performance may degrade.

What is Garbage ?
Garbage is not accessible and not freed memory. For Example, you allocated space for a linked list of 10 elements and after inserting 10 elements to the list, if you made 'head.next=null', then there is no way to reach the next 9 nodes in the list, and you can't deallocate the memory in use by the remaining 9 nodes also. This memory used by the remaining 9 nodes is not usable and not freed also. This is an example for garbage. Here Garbage collector came into picture, to manage memory on behalf of you.

What is Dangling Pointer ?
A dangling pointer points to memory that has already been freed. Dangling references are created by deallocating the memory for an object, even the references point to this object is in use.

Example
int *mem = malloc(sizeof(int));
free(mem);
*mem = 3; 

As you observe the above program, it used 'mem' variable after freeing the memory allocated also. This leads to segmentation fault.

What is Memory leak ?
Memory leak is a situation, where you can't free and use the memory allocated for your Application.

Example
void func(){
    char *ch;
    ch = (char*) malloc(10);
}

character pointer ch is a local variable that goes out of scope at the end of the function, leaking the dynamically allocated 10 bytes.

It is very tedious task for a developer, to reclaim the unused memory when the Application grows bigger. He may end up in facing dangling pointers or memory leaks, if the developer don't handle the memory Properly. So Some languages come up with Automatic Garbage collection methodology, which will take care of unused memory. With Garbage Collection, Programmer relieved from the memory management tasks, and his only concern is Application logic now.

Just like Java, Garbage collection is supported by many languages like Lisp, Modula-3 and Samlltalk etc., Garbage collectors vary depends on their implementations. Garbage collection implementations use different strategies to reclaim the memory of unreachable objects.

One drawback of Garbage collection is, programs written in Garbage collected languages runs slower than the programs written in non-garbage collected languages. Since Garbage collection won't come free, it runs in background, to find out unreachable objects. It consumes your system resources. But with the newer techniques evolved by Java (like JIT compilation) reduce this performance burden.

There are many approaches like Reference Counting, Mark-Sweep and copyingcollectors etc., are in use for Garbage Collection.

Related Links
Memory Leak
Object Creation and Destruction
Garbage Collection basic
Reference Counting Approach
Mark-Sweep Approach
Copying Collectors
Mark-Compacting Approach







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment