Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

freeMemory, totalMemory return unexpected values

843829Dec 12 2005 — edited Sep 18 2008
Hi,

Could anybody explain me freeMemory and totalMemory usage.
Here is a very simple program with output results.

public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
long freeMem = runtime.freeMemory() / 1024;
long totalMem = runtime.totalMemory() / 1024;
System.out.println("Memory at starting");
System.out.println("Free memory : " + freeMem + " KB");
System.out.println("Total memory : " + totalMem + " KB");

int[] alloc = new int[2000*1024];
System.out.println("Allocate 2000 KB");

freeMem = runtime.freeMemory() / 1024;
totalMem = runtime.totalMemory() / 1024;
System.out.println("Free memory : " + freeMem + " KB");
System.out.println("Total memory : " + totalMem + " KB");

}

output result:
Memory at starting
Free memory : 1813 KB
Total memory : 1984 KB
Allocate 2000 KB
Free memory : 1882 KB
Total memory : 9988 KB


I did run this program on 3 windows systems (WindowsXP SP2)
with 512Mb, 1Gb and 2Gb memory.
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

What value freeMemory returns? Why I can allocate more memory than I
have?

Thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 16 2008
Added on Dec 12 2005
6 comments
925 views