More free memory?

I have a simple test in which I allocate big long array and then I free that array and call JVM garabage collector.

But I find out I have more free memory afterward (using the runtime.freeMemory() call)

Did I do something wrong? Or could some one please tell me why?

Thanks.

Sam

-

The result:

========

Before Allocate ...

total memory 2031616

free memory 1886232<-- free memory when I start

After Allocate ...

total memory 2031616

free memory 1094352

After gc ...

total memory 2031616

free memory 1894432<-- more free memory than I start

My program:

=========

public static void main(String args[])

{

System.out.println ("Before Allocate ...");

System.out.println ("total memory " + Runtime.getRuntime().totalMemory());

System.out.println ("free memory " + Runtime.getRuntime().freeMemory());

long[] testLongArray = new long[100000];

System.out.println ("After Allocate ...");

System.out.println ("total memory " + Runtime.getRuntime().totalMemory());

System.out.println ("free memory " + Runtime.getRuntime().freeMemory());

testLongArray = null;

System.gc();

System.out.println ("After gc ...");

System.out.println ("total memory " + Runtime.getRuntime().totalMemory());

System.out.println ("free memory " + Runtime.getRuntime().freeMemory());

}

[1483 byte] By [cheung_sy1] at [2007-9-26 11:32:12]
# 1

Before you get to far down this path you might want to search the forums for 'gc' and see what people say about calling it.

>Did I do something wrong? Or could some one please tell me why?

A guess...

Most of the what you think of the JVM is actually written in java itself. And a lot of java objects get loaded and created before your main() is ever called - there are something like 100+ classes that are loaded on start up. Some of those create objects which are no longer in use but have not been garbage collector.

By some oddity, everything was collected when you called gc(). Your stuff, and the JVM stuff. So you see a difference.

jschell at 2007-7-2 1:32:13 > top of Java-index,Java HotSpot Virtual Machine,Specifications...