Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use garbage collector? [duplicate]

Possible Duplicate:
Garbage Collection in C++ — why?

Hi, I read few articles about Garbage Collectors, and still there is one thing I just don´t understand - why use garbage collection?

I will try to explain my thoughts:

Garbage Collector should release dynamically allocated memory back to system in case there is no need for it, right? So, if you write program in language C, you know wheather you need some piece of memory, so if don´t, you can simply destroy it.

So, why to use GC, when all you need to do is actually just be wise with memory allocation/deallocation? Or am I missing something? Thanks.

like image 683
B.Gen.Jack.O.Neill Avatar asked Jun 29 '10 14:06

B.Gen.Jack.O.Neill


People also ask

What is copying garbage collector?

Garbage collection is performed by copying live objects from one semispace (the from-space) to the other (the to-space), which then becomes the new heap. The entire old heap is then discarded in one piece. It is an improvement on the previous stop and copy technique.

Why do we use garbage collection?

Garbage Collection is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory. Benefits for the programmer is that garbage collection frees the programmer from manually dealing with memory allocation and deallocation.

What is the advantage of a copying garbage collector over a mark and sweep garbage collector?

It has often been argued that copying collection is superior to mark-sweep for two reasons. First, it compacts memory, and hence avoids any fragmentation. Second, it's running time is proportional to the amount of live memory, not the size of the heap.


2 Answers

To be more productive. In other words, the programmer can focus on writing the bits that is unique for his particular problem.

like image 106
Martin Wickman Avatar answered Nov 06 '22 20:11

Martin Wickman


To avoid errors. No matter how careful you are about deallocating memory, either you will eventually make a mistake or you will eventually code a program that requires a complex memory reference pattern which will make the likelihood of error much greater.

Any possibility that exists for given enough time will become a reality, eventually you will leak memory with manual methods, unless extra effort is specifically put into monitoring memory consumption. This extra effort steals time from coding toward the primary purpose of the program, which probably isn't to manage memory.

In addition, even if your program doesn't leak memory, garbage collection often tends to handle memory more efficiently than many non-garbage collection methods. Most people don't new blocks of objects to avoid multiple new calls, nor would they revisit and clean up the cache of unused newed objects afterwards. Most manual garbage collection methods concentrate on freeing memory at block boundaries, which might let garbage linger a bit longer than it needed.

Each added benefit and feature you pile onto manual garbage collection takes you one step closer to automatic garbage collection. Using no utilities to collect garbage beyond the manual calls to free it will not scale easily. Either you will spend a lot of your time checking memory allocation / reclimation, or you will not spend enough to avoid a memory leak.

Either way, automatic garbage collection solves this problem for you, allowing you to get back to the main point of your program.

like image 31
Edwin Buck Avatar answered Nov 06 '22 20:11

Edwin Buck