Witscale Test Center

Chapter Summary


Chapter Summary

       Java has automatic memory management. Garbage collector is part of JVM responsible for recycling the allocated memory to java objects.

       Garbage Collection cannot be forced explicitly. JVM may do garbage collection if it is running short of memory.

       The call System.gc() or ( new Runtime().gc() ) does not force the garbage collection but only suggests that the JVM may make an effort to do garbage collection.

       Garbage Collection is hardwired in Java runtime system. Java runtime system keeps the track of memory allocated. Therefore, it is able to determine if memory is still usable by any live thread. If not, then garbage collector will eventually release the memory back to the heap.

       Garbage Collection usually adopts algorithm, which gives fair balance between responsiveness (how quickly garbage-collection completes) and speed of memory recovery. Responsiveness is especially important in real time systems whereas quick memory-recovery is important for memory-intensive operations.

       How an object becomes eligible for Garbage Collection

q      An object is eligible for garbage collection when no object refers to it.

q      An object also becomes eligible when you explicitly set all its references to null.

q      An object also becomes eligible when you explicitly reassign all its references to point to another object or null.

q      Object created in method and referenced by method variables are eligible for garbage collection when the method returns unless its reference is returned by method.

q You can not …

         Force garbage collection

         Know exactly when it will happen

         Know for sure which algorithms are used for garbage collection

q You can ….

         Tell when the object becomes eligible for garbage collection

         Explicitly make an object eligible for garbage collection

         Request garbage collection

q The finalize() method

         It is called only once by the garbage collector before deleting unreachable object.

         As garbage collector makes no guarantees, the finalize()method may never run

         You can make object ineligible for garbage collection in the finalize() method.