Garbage Collection in JVM

Harsh Gupta - Tech Writer
Garbage collection is a good way to free programmers from the burden of having to explicitly free the allocated memory. On execution, a Java program creates and uses different objects. Each object within program uses some amount of system resources, such as the system memory during the run time. Garbage collection is the process that is used to free the memory of the objects that are no longer in use. When a program stops referencing an object, it is not required anymore and can be deleted. The space that is used by the object is released for use by another object. In this way, garbage collection serves as a mode for memory recycling where the space occupied toy one object is recycled available for subsequent new objects. The garbage collection feature implies that the new objects are created and after the unused objects are de-allocated from the memory. The different approaches used for detecting garbage objects are:

Reference-Counting Collectors

Tracing Collectors

Compacting Collectors

Reference-Counting Collectors

Reference-Counting garbage collectors store the references of the objects used within a program. The concept works as follows: You create a reference A that points to a particular object ABC. You create one more reference B that points to object XYZ. Next, you assign one more reference C to first object ABC. As you make references to object(s), the reference count is incremented by one and when you remove references for the same object, the reference count is decremented by one. When you remove all references to ABC object, then reference count for this object is set to ZERO. All objects with ZERO references are marked for garbage collection.

An object that has a reference count of zero is not referenced in a program and is a garbage object. When the object is identified as garbage, the reference count of ail the objects that it refers to is decremented. Therefore, garbage collection of one object can lead to creation of more garbage objects.

The garbage collection method can be executed in small parts within a program and the program need not be interrupted for a long time. An overhead occurs whenever the counters are Incremented or decremented.

Tracing Collectors

In this technique, a set of roots is defined from the location where the objects are traced. An abject is reachable if there are objects that reference it. The objects that are not reachable are considered garbage objects since they are not referenced and are not accessed within a program. The tracing collectors mark the objects that are reachable. At the end of the trace, all unmarked objects are identified as garbage.

Tracing Collector is also known as the mark-and-sweep algorithm. The mark phase marks al the referenced objects. The sweep phase garbage collects the unreferenced objects.

Compacting Collectors

Compacting collectors reduce the fragmentation of memory by moving the free space to one side during garbage collection. The free memory is then available to be used by other objects. All references to the shifted objects are then updated to point to new memory locations

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • Garbage collection is the process that is used to free the memory of the objects that are no longer
  • When a program stops referencing an object, it is not required anymore and can be deleted. The
The garbage collection feature implies that the new objects are created and after the unused objects are de-allocated from the memory.

1 Comments

Post a Comment
  • Louie Jerome4/8/2009

    Interesting item.

To comment, please sign in to your Yahoo! account, or sign up for a new account.