Hi,
Below is the code snippet to examine the memory
public class TestFreeMemory {
public static void main(String ... args){
Runtime rt = Runtime.getRuntime();
System.out.println("Free Memory (Before GC): " + rt.freeMemory());
rt.gc();
System.out.println("Free Memory (After GC1): " + rt.freeMemory());
rt.gc(); // Second time to ensure results are consistent
// MAY BE has collected all non-reachable objects
System.out.println("Free Memory (After GC2): " + rt.freeMemory());
String s = new String("abcd");
Integer i = new Integer(12345);
System.out.println("Free Memory (After String Creation): " + rt.freeMemory());
// Why is freeMemory not reflecting the memory consumed by two objects
}
}
and the output is
Free Memory (Before GC): 1859672
Free Memory (After GC1): 1911768
Free Memory (After GC2): 1911768
Free Memory (After String Creation): 1911768
Why is freeMemory not reflecting the memory consumed by two objects ?
To be more clear, the question is about getMemory() call not showing up expected result even though two objects are created and not related to GC. Two GC calls are made just to try to make sure the numbers of getMemory() call are correct.. and BTW, there's no GC call after objects are created.. so pls Note that i am not trying for a GC after objects are created ..