As we all know, the java -Xmx
option is used to set the maximum heap space available to a Java program. But is there a way for a Java program to read the value that has been set? Something like System.getMaximumHeapSpace()
(which does not exist).
// Get current size of heap in bytes long heapSize = Runtime. getRuntime(). totalMemory(); // Get maximum size of heap in bytes.
Maximum heap size is 1/4th of the computer's physical memory or 1 GB (whichever is smaller) by default. The maximum heap size can be overridden using -Xmx.
The easy way to monitor Heap usage is by using a commercial APM (Application Performance management tool) such as CA Wily APM, AppDynamics, New Relic, Riverbed, etc. APM tools not only monitor the heap usage, but you can also configure the tool to Alert you when Heap usage is not normal.
There is a Runtime.maxMemory
, which the documentation says will return the maximum amount of memory the Java Virtual Machine will attempt to use, but it is not specific whether it is the maximum heap memory set when launching the JVM.
Just as a test, I wrote the following program:
class MaxMemory {
public static void main(String[] args) {
System.out.println(Runtime.getRuntime().maxMemory());
}
}
The results of the execution was the following:
C:\coobird\>java -Xmx64m MaxMemory
66650112
C:\coobird\>java -Xmx128m MaxMemory
133234688
This is using Java SE 6 version 1.6.0_12 on Windows.
Although the values are close to 64 MB (67,108,864 bytes) and 128 MB (134,217,728 bytes), they aren't exactly so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With