Garbage Collector

We are hosting this site in another domain. Checkout

http://www.techpitcher.com


What is Garbage Collection ?
Memory for the new object created in java is allocated in heap at runtime. When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects.

Garbage collection is the process of automatically freeing objects that are no longer referenced by the program.

During object cleaning, free memory in the heap may not be continuous, free memory may be between live objects. It’s job of GC to fragment heap memory.

There are many advantages of having garbage collector

1. It increases productivity. One need not worry about memory issues.
2. It ensures program integrity. Garbage collection is an important part of Java's security strategy. Java programmers are unable to accidentally (or purposely) crash the Java virtual machine by incorrectly freeing memory.

A potential disadvantage of a garbage-collected heap is that it adds an overhead that can affect program performance. The Java virtual machine has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects on the fly. This activity will likely require more CPU time than would have been required if the program explicitly freed unnecessary memory. In addition, programmers in a garbage-collected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed.


What are the advantages and disadvantages of reference counting in garbage collection over tracing algorithm?
Reference counting and tracing algorithm are both techniques used for garbage collection. Reference counting is a garbage-collection technique that maintains a per-object count of the number of pointers to that object. When the count reaches zero, the object must be dead and can be collected. When an object is garbage collected, the other object's count referring to this is decremented by 1. In this way, garbage collection of one object can lead to subsequent garbage collection of other objects.

The main advantage of reference counting over tracing is that an object is reclaimed as soon as it is dead and without long pauses or delays. This is well suited for smaller applications with limited memory and in chunks. This characteristic makes it suitable for real time environment where the responsiveness needs to be high. They are typically run in conjunction with other methods to increase over all precision.

There are two main disadvantages for it over tracing. First one being that it can not detect cycles. This method totally relies on reference count and an object which is directly or indirectly(by parent child relationship) refers to itself will never be garbage collected because their count is destined to stay as non-zero. The other disadvantage is the frequent updates that needs to happen to reference count of all the objects every time an object is garbage collected or a new assignment has happened. This is typically inefficient and includes a lot of extra storage traffic too.

You can also find links related to these topics on ads section provided by Google.
Please provide your comment to make this forum better.

No comments: