Friday, June 7, 2013

Garbage Collection by Java Code

Garbage Collection by Java Coding. 

Sometimes for a large project, an exception named stack/memory overflow may generated, then we have to free the JVM(Java Virtual Machine) memory. here is the code.


/**
 *GarbageCollection.java
 *
 * @author 
 * Santanu Kar
 **/

import java.util.*;

public class GarbageCollection {

public static void main (String[] args) throws Exception {
Runtime rs = Runtime.getRuntime();
System.out.println("Free memory in Java Virtual Machine before Garbage Collection = "+rs.freeMemory());
rs.gc();
System.out.println("Free memory in Java Virtual Machine after Garbage Collection = "+rs.freeMemory());
}    
}

No comments:

Post a Comment