/**
* Write a program which display the remaining memory. Before and
* after Garbage collector is called.
*/
public class MemoryCheck {
public static void calculateMemory() {
try {
// creating dummy objects
for (int i = 0; i < class="SpellE">i++)
new String("dummy string");
// store the free memory size before
long lngFreeMemoryBefore = Runtime.getRuntime().freeMemory();
// run the garbage collector
System.gc();
// store the available free memory after
long lngFreeMemoryAfter = Runtime.getRuntime().freeMemory();
// display the result
System.out.println("Free Memory Before : " + lngFreeMemoryBefore);
System.out.println("Free Memory After : " + lngFreeMemoryAfter);
System.out.println("Free Memory : "
+ (lngFreeMemoryAfter - lngFreeMemoryBefore));
} catch (Exception e) {
System.out.println("Error Ocurred!!");
e.printStackTrace();
}
}
/**
* execution begin from here
*
* @param args
*/
public static void main(String[] args) {
calculateMemory();
}
}
No comments:
Post a Comment