53 Program on Garbage Collection in Java Part 1

Program explain concept like:

1) When jvm will skips low priority thread
2) Types of Object
3) Memory allocation type Used, Unused Memory

Code:

public class Garbage
{

@Override
public void finalize()
{
System.out.println(" Memory Cleaned: "+Thread.currentThread().getName());
}

public static void main( String args[] )
throws InterruptedException{
Garbage obj1 = new Garbage(); // Used Memory /  Referenced Object Creation / Named Memory

new Garbage(); // UnUsed Objecy / UnReferenced / Anonymous memory

obj1= null;
//Runtime.getRuntime().gc();
System.gc();  // Asking JVM please go and call Garbage Collector
Thread.sleep(1000);
System.out.println(" Program Ends ");
}
}


Output:



Previous
Next Post »