QSpiders - Garbage collection (GC) memory management

6
Garbage Collection (G C)

Transcript of QSpiders - Garbage collection (GC) memory management

Page 1: QSpiders - Garbage collection (GC) memory management

Garbage Collection (GC)

Page 2: QSpiders - Garbage collection (GC) memory management

What is GC ?“Garbage Collection (GC) is a form of automatic memory management.”

Page 3: QSpiders - Garbage collection (GC) memory management

GC Process Object is determined to be unreachable. If object has a finalize method.

- Object is added to a finalization queue.

- At some point it’s finalize method is invoked so the object can free associated resources.Object memory is reclaimed .

Page 4: QSpiders - Garbage collection (GC) memory management

How to use gc ? The garbage collector is under the

control of JVM. The JVM decides when to run the garbage

collector. From the Java program we can tell JVM to run garbage collector.

We can write code to make objects eligible for garbage collection.

Setting reference variable to null.

Page 5: QSpiders - Garbage collection (GC) memory management

finalize() method – This is a protected method of Object class that's run before the object is deleted by the garbage collector.

Page 6: QSpiders - Garbage collection (GC) memory management

Example’s - public class Demo {

public static void main(String[] args) {

StringBuilder sb=new StringBuilder(“Java”); System.out.println(sb); sb=null; //eligible for collection

String s1=new String(“Hello”);String s2=new String (“Java”);

System.out.println(s1); s1=s2; //Hello object is eligible for collection }}

public class FinalizeDemo{public static void main(String[] args) {

}

protected void finalize() throws Throwable{

super.finalize();System.out.println("our

cleaning");}

}