Search this Site


More books @amazon.com

Home- Tutorial on Garbage Collection

What is garbage collection:
The process of automatically reclaiming the memory that is no longer in use is known as garbage collection. Garbage collection is a runtime component of Java which sits on top of the heap: memory area from which Java objects are created and periodically scans it for objects which are eligible to be reclaimed since there are no references to these objects in the program.

Object's life cycle:
In Java, life of an object is determined by the variable that references it. As long there is atleast one variable in the program accessing the object, that object will not become a candidate for garbage collection.

Finalizer:
Object class defines finalize method. The signature of finalize method is is as follows. This method is automatically called before an object is destroyed

protected void finalize() throws Throwable

Following are the important considerations you must be aware of.

  1. Java does not guarantee that any particular object will be garbage collected, but make promise to call the finalize on an object before reclaiming the memory it occupies.

  2. There is no guarantee what so ever as to when the finalize method is called. 

  3. There is no specific order in which the finalize method will be called. 

  4. Overriding finalize method in your class does not  prioritize your object for garbage collection over any other object which do not have finalize method overridden in it's class.

  5. Any exception thrown by the finalize method causes the finalization 
    of that object to be stopped, but is not notified to the application and that object is still considered as finalized.
  6. finalize can run only once on the object in it's entire life cycle.
  7. finalize method defined in Object class, as such does nothing. In order to get some meaningful thing done , you will have to override this method in the subclass.
  8. Unlike constructors, which are chained from the subclass to superclass, finalizers are not implicitly chained.  It is considered a good programming practice if you call the superclass finalizer like super.finalize() in your subclass. 
  9. By default garbage collector will not execute the finalizers of any objects left on heap when the application exists.

So how to get an object garbage collected:
There is simply no way to force garbage collection, but you can suggest your intention of getting the object gc'ed to Java Virtual Machine and then live rest to it and hope for the best !!. Java provide following two methods.

(1)

public static void runFinalization()

The above method defined in Runtime class, runs finalization methods of any objects that have not yet been finalized. This methods only suggests the JVM to run the finalize methods on the objects which have not run and in any case cannot force it. Following is from Java API

Calling this method suggests that the Java Virtual Machine expend effort toward running the finalize methods of objects that have been found to be discarded but whose finalize methods have not yet been run. When control returns from the method call, the Java Virtual Machine has made a best effort to complete all outstanding finalizations.

Run the following code and see what output you get !!

public class MyClass
{
  public static void main (String args[])
  {
    finObj myObj = new finObj("Obj");
    System.runFinalization();
  }
}

class finObj
{
  String myName;
  finObj (String aname)
  {
    myName = aname;
  }
  public void finalize()
  {
    System.out.println(myName);
  }
}

 

(2)

public static void gc()

The above method is also defined in Runtime class, runs garbage collector 
(if present) and tries to reclaim memory from unused objects. Following is from Java API

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Does Garbage collection a feature of every JVM:
There is no guarantee that every implementation of Java Virtual Machine(JVM) will have a garbage collection process. It is left to the implementers of JVM to decide whether to provide garbage collection mechanism or not, as well as what type of algorithm to achieve it. 

More Resources:
SUN's Tutorial

http://www.javaworld.com/javaworld/jw-06-1998/jw-06-techniques_p.html

Books:
Garbage Collection : Algorithms for Automatic Dynamic Memory Management
  - Advisable if you want pursue advance algorithms for garbage collection. Not required for SCJP exam !! 

 

[ TOP OF PAGE ]

 

MAIN MENU

BEFORE YOU BEGIN

Certification FAQ
Certification Objectives

TICKLE YOUR GRAY CELLS

Interactive Sample Java Test
Over 6000 students have  tested !

Object Equivalence

Threads

SCJP STUDY TOOLS

Study Tips

Certification Books

Other Certification Sites

Tutorials

SCJP Revision Tips 

MISCELLANEOUS LINKS

Java Magazines

Java IDE's

Career Related Websites


Garbage Collection : Algorithms for Automatic Dynamic Memory Management

 

 

 

Disclaimer     © 2000-01 Sandeep Nachane Feedback