Java Garbage Collection
Java Garbage Collection
Java Garbage Collection
1) By nulling a reference:
1. Employee e=new Employee();
2. e=null;
2) By assigning a reference to another:
1. Employee e1=new Employee();
2. Employee e2=new Employee();
3. e1=e2;//now the first object referred by
e1 is available
for garbage collection
3) By annonymous object:
1. new Employee();
finalize() method
The finalize() method is invoked each time
before the object is garbage collected. This
method can be used to perform cleanup
processing. This method is defined in
Object class as:
1. protected void finalize(){}
gc() method
The gc() method is used to invoke the
garbage collector to perform cleanup
processing. The gc() is found in System
and Runtime classes.
1. public static void gc(){}