Java Program To

Perform Garbage
Collection

http://freepdf-books.com

} } Download Garbage collection program class file. class GarbageCollection { public static void main(String s[]) throws Exception { Runtime rs = Runtime. freeMemory method returns amount of free memory in jvm.out. getRunTime method is used to get reference of current RunTime object. System.println("Free memory in JVM before Garbage Collection = "+rs. http://freepdf-books.out.This program performs garbage collection.gc(). System.util.freeMemory()).getRuntime(). rs.println("Free memory in JVM after Garbage Collection = "+rs.freeMemory()). Java programming source code import java. Free memory in java virtual machine is printed and then garbage collection is done using gc method of RunTime class.*.com .

what is important is that amount of memory available is more than before. Numbers are not important.Output of program: Obviously the amount of available after garbage collection will be different on your computer.com . http://freepdf-books. You can use this code in your program or projects which uses large amount of memory or where frequently new objects are created but are required for a short span of time.