Thursday, August 27, 2015

Garbage Collection in JAVA

1) When the garbage collector runs, its purpose is to find and delete objects that cannot be reached.
 
2) The garbage collector is under the control of the JVM. The JVM decides when to run the garbage 
     collector.
 
3) An object is eligible for garbage collection when no live thread can access it.
 
4) Garbage collection cannot ensure that there is enough memory, only that the memory that is
    available will be managed as efficiently as possible.
 
5) The simplest way to ask (request) for garbage collection in System.gc();
 
6) About the only thing you can guarantee is that if you are running very low on memory, the garbage
    collector will run before it throws an OutOfMemoryException.
 
7) JAVA provides you a mechanism to run some code just before your object is deleted by the
    garbage collector. This code is located in a method named finalize() that all classes inherit from
    class Object.
    - For any given object, finalize() will be called only once (at most) by the garbage collector.
    - Calling finalize() can actually result in saving an object from deletion.
 
    For example, in the finalize() method you could write code that passes a reference to the object in
    question back to another object, effectively uneligibilizing the object for garbage colleciton.
 

No comments:

Post a Comment

Home