Reputation: 6116
I am running a big Java application on a cluster and I have much memory available. Thus I plan to start my JVM with a huge maximal heap size and see that it works. However I need to run this multiple times and I want to know how big the heap actually became during the run. I know I could do this with a tool like VisualVM but what if I don't want to run a big ui based application? Is there some paramater I can send to the jvm to get some this sort of memory statistics after my application has terminated? Or perhaps a command line based application that can easily do it?
Upvotes: 1
Views: 131
Reputation: 18572
The total used / free memory of an program can be obtained in the program via java.lang.Runtime.getRuntime();
The runtime has several method which relates to the memory. The following coding example demonstrate its usage.
The runtime has several method which relates to the memory. The following coding example demonstrate its usage.
import java.util.ArrayList;
import java.util.List;
public class PerformanceTest {
private static final long MEGABYTE = 1024L * 1024L;
public static long bytesToMegabytes(long bytes) {
return bytes / MEGABYTE;
}
public static void main(String[] args) {
// I assume you will know how to create a object Person yourself...
List<Person> list = new ArrayList<Person>();
for (int i = 0; i <= 100000; i++) {
list.add(new Person("Jim", "Knopf"));
}
// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: "
+ bytesToMegabytes(memory));
}
}
Upvotes: 0
Reputation: 88707
You could do it from within the application:
ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
Then use the getCommitted()
, getUsed()
and getMax()
methods on the returned MemoryUsage
object.
Upvotes: 2
Reputation: 4795
U can make use of this below method to get the value of heap size
Runtime.getRuntime().totalMemory();
Upvotes: 1
Reputation: 23680
You need to profile your application with something like:
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html#LowMemoryDetection
Upvotes: 1
Reputation: 32407
jstat
is what you are after. It will print stats every second (or whatever you specify) then you can analyse the output to find the maximum.
Upvotes: 3