C++ Blog

Monday, April 02, 2007

"GARBAGE COLLECTOR" a Curse or a Bless?

What is "Manual Memory Management"?
In the manual memory management the user himself should take care of allocating memory for objects and releasing the memory when its not needed anymore. C++ supports manual memory management because it doesn't have any mechanism which collects memory automatically when it is not needed.

What is "Garbage Collector"?
Garbage collector is automated way doing memory management. In this case the user needs to allocate memory but he doesn't need to worry about the re-claiming memory becaues garbage collector does that automatically. C# and Java falls under this category.

Manual Memory Management
A Bless

1) We don't need to keep track of all active objects or periodically look for memory which can be re-claimed. (This is needed for Garbage Collector)
2) More control over memory allocation and de-allocation. You can reclaim memory when ever needed by using a "delete" operator. Which in turn calls the destructor so we can be sure that object releases all the resources before it is destroyed.

A Curse

1) Main problem with this approach is "Memory Leak" which degrades system performance and may lead to fatal errors. Failing to release allocated memory will result in memory leak.
2) Releasing memory which are pointed by other objects will result in a undefined behaviour when the other object tries to access its data. This is called as "Dangling Pointer"
3) Trying to release the same memory twice unknowingly can lead to serious error.


Garbage Collector
A Bless

1) It is very simple and safe. Simple in the sense the user doesn't need to worry about releasing object and keep track of all the objects. Safe because you don't need to release memory explicitly so you may not get in to situation of releasing same memory twice or releasing memory which is pointed by some other reference.

A Curse

1) Garbage collector needs CPU cycles for re-claiming all the unused memory. It also needs to keep track of all the objects that are in use with a datastructure or it needs to scan through the memory for finding it.
2) It makes calling the object destructor non-deterministic because of no control over the memory when it will be released. The memory is released only when the garbage collector runs and recyles the object. But it is not easy to predict when the garbage collector will run. In some systems it will start when the free memory limit threshold is reached, in some systems it is started when the CPU cycles are free.
3) Real-time applications can't use garbage collectors since allocating of CPU cycles to garbage collector could cause an event to be missed :-(.


As we can see the curse of Garbage collector is more than its blessings but this often overcomes in most of the programs, using garbage collector is a trade-off but most of the modern systems its a bless.

C++ is a mixed blessing, since you have to do manual memory management but you can also write code to do garbage collection with the power of C++.
Ref:

0 Comments:

Post a Comment

<< Home