package com.instanceofjava;
class GarbageCollectionTest1{
public static void main(String [] args){
String str="garbage collection interview questions";
// String object referenced by variable str and is not eligible for GC yet.
str=null;
//String object referenced by variable str is eligible for GC
}
}
package com.instanceofjava;
class GarbageCollectionTest2{
public static void main(String [] args){
String str1="garbage collection interview questions";
String str2="Top 15 garbage collection interview questions";
// String object referenced by variable str1 and str2 and is not eligible for GC yet.
str1=str2;
//String object referenced by variable str1 is eligible for GC
}
}
An object becomes eligible for garbage collection when no live thread can access it.
Only once.
No, we can not force garbage collector to destroy objects , but we can request it.
Runtime.getRuntime().runFinalizersOnExit(boolean value). passing the boolean value true and false will enable or disable the finalize() call.
The finalize() method should be overridden for an object to include the clean up code or to dispose of the system resources that should to be done before the object is garbage collected.
Heap.
"mark and swap" is the algorithm JVM internally uses.
The exception will be ignored and the garbage collection (finalization) of that object terminates