Automated memory management is one of Java’s most valuable services. Many languages require programmers to specify the details of acquiring storage from the operating system and returning it when no longer needed. But, Java handles this for programmer. Since Java does automatic garbage collection, manual memory reclamation is not needed. The runtime system tracks each object you create, notices when the last reference to it has vanished, and frees the object for you. In some cases objects utilize a resource other than memory, such as a file or a handle to another object that uses system resources. In such cases it is important that the resource be reclaimed and recycled when it is no longer needed.
Garbage collection: some facts
  • It is assured that only those objects with no references will be garbage collected.
  • You can turn off garbage collection in your application by, java -noasyncgc …..
  • If you do this, it is almost guaranteed to fail with memory exhaustion at some point. It can be used only to experiment the performance difference it makes.
  • Alternatively, you can call the method “System.gc();” to execute garbage collector at any point of the source you choose.
  • “mark and sweep” garbage collection is one of the alogorithm which java uses for garbage collection, which search through all the references of an object, and which mark any objects that are referenced to, finally the garbage collect any referenced objects that are not marked.
  • In java we can add a factory method finalize() to any class. This factory method will be called before the objects are swept away by the garbage collector. In real, do not depend on the factory method finalize for recycling any resources that are in short supplied – you simply cannot make an assumption when this factorized method will be called. Java1.1 will guarantee that finalizers are run, if the method ‘System.runFinalizersOnExit()’ is called before exit.
  • If a finalize() method exists for an object it is guaranteed to be called once for the object, before the object is garbage collected.
  • Any class that includes a finalize method should invoke its super class’ finalize() method
  • Assuming that there is no compiler optimization, the earliest point that a local object becomes available for garbage collection will be when the runtime environment knows that there are no more references to the object in question. For local objects, this is when method ends, but there are situations when the developer might wish to give the runtime environment a hint that the object is no longer needed.

    For eg,

    public void method() {

    BigObject obj = new BigObject(2000);

    //do some codes

    //let the runtime environment know that ” obj ” is no longer needed

    obj = null;

    }
    //do some processing

  • The jre( java runtime environment) could collect the “BigObject” anytime after object of class BigObject “obj ” value set to null. Setting the object reference to null value doesn’t guarantee that the object will be garbage collected instantly, but it helps.
  • When the runtime environment exsits, there may be many objects that have not been collected, because garbage collection runs as a separate, low priority thread and there are objects that it may never collect.