Determine the available memory in Java

This Java tutorial will give you a brief walk through on Java memory use in the sandbox and show how to use the Runtime class to discover the amount of memory available to your Java applet or application.

The Sandbox
Unlike a C++ application, a Java applet or application doesn't have access to all the memory on the computer. Instead, Java creates a sandbox that limits the amount of available memory available to your application. The sandbox was created to protect the user from Java code that could be harmful by using all of the available resources on a computer. By default, the JVM uses a percentage of the available system memory when your Java application or applet starts. This will vary be different versions of the JVM.

You can increase the default amount of memory available to the JVM by using the -Xmx command line option for Java applications. For Java applets, the command line setting can be set in the Java control panel. For more information on the -Xmx command line option, enter java -X on the command line.

The following example uses the Runtime class to get the maximum amount of memory (sandbox limit) that your Java application can use:

Runtime runtime = Runtime.getRuntime();
System.out.println("max memory: " + runtime.maxMemory() / 1024);

The fine print should read that this is the maximum amount of memory that your Java application might be able use. The sandbox only limits the amount of memory that can be used and doesn't guarantee that it will be available when you need it. If you need to guarantee that this memory will be available to your application, use the -Xms command line option to allocate the memory when the JVM starts.

Java Memory Allocation
The following example shows how much memory that JVM has allocated for your application:
Runtime runtime = Runtime.getRuntime();
System.out.println("allocated memory: " + runtime.totalMemory() / 1024);

The totalMemory() method returns the amount of memory the JVM has allocated from the operating system. Unless you specified the amount of memory to allocate using the -Xms command line option, the totalMemory() method will continually fluctuate as objects are allocated and the garbage collection cleans them up.

The following example shows how to discover how much memory is being used by your application:
Runtime runtime = Runtime.getRuntime();
System.out.println("free memory: " + runtime.freeMemory() / 1024);

The freeMemory() method will return the amount of free memory from the allocated memory. Since this value does not include any memory that hasn't yet been allocated by the JVM, you will need to get a complete picture of the free memory that your application would be able to use. To do this, you need to include the amount of memory that hasn't yet bee allocated by the JVM, but could be allocated if necessary. The following example shows how:
Runtime runtime = Runtime.getRuntime();

long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();

System.out.println("free memory: " + freeMemory / 1024);
System.out.println("allocated memory: " + allocatedMemory / 1024);
System.out.println("max memory: " + maxMemory /1024);
System.out.println("total free memory: " +
(freeMemory + (maxMemory - allocatedMemory)) / 1024);

Sample results from the test below show that Java only allocated a small amount of memory and a large amount of memory is still available to the application:
free memory: 294
total memory: 1984
max memory: 65088
total free memory: 63398


Anders said...

Hi there!
My program has a memory leak. I'm doing all the above mentioned printouts but all the values stays constant. Yet, the memory usage of the javaw.exe process in the task manager continues to grow at a constant rate. How can this be?

Kevin said...

Verify that the runtime.totalMemory() isn't growing. It maybe growing if you didn't specify the -Xmx memory setting.

If possible, try to use the latest version of Java. There have been dramatic improvements in memory management in 1.5 update 6 and again in 1.6.

Frank said...

Nice one, whoever wrote this! (Kevin, maybe)? This was the first hit that came up on Google for "Java Memory Available", and I see why.


Post a Comment