Testing the following code from a book
public class RunTime {
public static void main(String[] args) {
Runtime r = Runtime.getRuntime();
//MEMORY MANAGEMENT
long mem1, mem2;
Integer intmatrix[] = new Integer[1000];
System.out.println("Total memory is :" + r.totalMemory());
mem1 = r.freeMemory();
System.out.println("Initial free memory is :" + mem1);
r.gc();
mem1 = r.freeMemory();
System.out.println("free memory after garbage collection :" + mem1);
for (int i = 0; i < 1000; i++) {
intmatrix[i] = new Integer(i);
}
mem2 = r.freeMemory();
System.out.println("free memory after integer allocation :" + mem2);
System.out.println("memory used by allocation :" + (mem1 - mem2));
for (int i = 0; i < 1000; i++) {
intmatrix[i] = null;
}
r.gc();
mem2 = r.freeMemory();
System.out.println("free memory after collecting discarded integers :" + mem2);
}
}
gives the following results :
Total memory is :16252928
Initial free memory is :15956728
free memory after garbage collection :16153296
free memory after integer allocation :15969624
memory used by allocation :183672
free memory after collecting discarded integers :16152840
183672 bytes for 1000 integers?