This section provides a tutorial example on how to use freeMemory(), totalMemory(), and maxMemory() of the Runtime class to get JVM total and free memory information.
There a couple of values used in Java related to memory management:
Free Memory: Number of bytes available for the application program.
Total Memory: Number of total bytes allocated to the application program.
Maximum Memory: Maximum number of bytes that can be allocated to the application
program.
There is one method in the Runtime class that you can use to get the each of those values.
Here is a program showing you how to use those methods properly:
/**
* FreeMemory.java
* Copyright (c) 2002 by Dr. Herong Yang
*/
public class FreeMemory {
private static long[] fm = new long[5];
private static long[] tm = new long[5];
private static long[] mm = new long[5];
private static long[] um = new long[5];
public static void main(String[] a) {
Runtime rt = Runtime.getRuntime();
getMemoryInfo(rt,0);
getMemoryInfo(rt,1);
getMemoryInfo(rt,2);
getMemoryInfo(rt,3);
getMemoryInfo(rt,4);
printMemoryInfo(rt,0);
printMemoryInfo(rt,1);
printMemoryInfo(rt,2);
printMemoryInfo(rt,3);
printMemoryInfo(rt,4);
for (int i=0; i<5; i++) {
System.out.println("Get memory info - step = "+i);
System.out.println(" Free memory = "+fm[i]);
System.out.println(" Total memory = "+tm[i]);
System.out.println(" Maximum memory = "+mm[i]);
System.out.println(" Memory used = "+um[i]);
}
}
public static void getMemoryInfo(Runtime rt, int i) {
fm[i] = rt.freeMemory();
tm[i] = rt.totalMemory();
mm[i] = rt.maxMemory();
um[i] = tm[i]-fm[i];
}
public static void printMemoryInfo(Runtime rt, int i) {
long fm = rt.freeMemory();
long tm = rt.totalMemory();
long mm = rt.maxMemory();
System.out.println("Print memory info - step = "+i);
System.out.println(" Free memory = "+fm);
System.out.println(" Total memory = "+tm);
System.out.println(" Maximum memory = "+mm);
System.out.println(" Memory used = "+(tm-fm));
}
}
This program seems to be confusing:
Why do I need two different methods to retrieve the memory information?
Why are the methods called repeatedly 5 times?
Why are the printing statements for the getMemoryInfo() method not following
the method call statements immediately?
You need to read the output of this program to answer those questions:
Print memory info - step = 0
Free memory = 1777568
Total memory = 2031616
Maximum memory = 134217728
Memory used = 254048
Print memory info - step = 1
Free memory = 1774712
Total memory = 2031616
Maximum memory = 134217728
Memory used = 256904
Print memory info - step = 2
Free memory = 1772496
Total memory = 2031616
Maximum memory = 134217728
Memory used = 259120
Print memory info - step = 3
Free memory = 1770280
Total memory = 2031616
Maximum memory = 134217728
Memory used = 261336
Print memory info - step = 4
Free memory = 1768064
Total memory = 2031616
Maximum memory = 134217728
Memory used = 263552
Get memory info - step = 0
Free memory = 1777568
Total memory = 2031616
Maximum memory = 134217728
Memory used = 254048
Get memory info - step = 1
Free memory = 1777568
Total memory = 2031616
Maximum memory = 134217728
Memory used = 254048
Get memory info - step = 2
Free memory = 1777568
Total memory = 2031616
Maximum memory = 134217728
Memory used = 254048
Get memory info - step = 3
Free memory = 1777568
Total memory = 2031616
Maximum memory = 134217728
Memory used = 254048
Get memory info - step = 4
Free memory = 1777568
Total memory = 2031616
Maximum memory = 134217728
Memory used = 254048
Note that:
The difference between the getMemoryInfo() method and printMemoryInfo() method is
that the getMemoryInfo() method saves the memory usage into arrays, while
printMemoryInfo() method prints the memory usage values on the console. This is to show
the impact of the printing statements, System.out.println(), on the memory usage.
Calling the methods repeatedly is to see if there are any thread other than
the application program thread running that might affect the memory usage. As you
can see from the output, the memory usage was un-changed during the 5 calls to
getMomeryInfo(). However, after each call to printMemoryInfo(), the free memory decreased
by more than 2000 bytes. This suggests that the System.out.println() method was leaving
dead objects in memory for the garbage collector to remove them.
Calling the getMemoryInfo() at the beginning and putting the related printing
statements at the end is to avoid any impact of printing statements on the printMemoryInfo()
calls.