Check heapSize software package?

I am using Eclipse. The problem is that my application crashes if the allocated memory is less than 512 MB. Now, all the same, you need to check the available memory for the program before starting the full processing of the full memory? For example, I know that we can check the available JVM heap size as:

long heapSize = Runtime.getRuntime().totalMemory();
System.out.println("Heap Size = " + heapSize);

The problem is that this gives the JVM heap size. Even increasing it does not work using Eclipse. In Eclipse, if I change the arguments of a VM, then it works. However, the listing from previous statements is always the same. Is there any command through which I can find out exactly how much memory is allocated for a particular application?

+5
source share
1 answer

You can use JMX to collect heap memory usage at runtime.


Code example:

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.lang.management.MemoryUsage;

for (MemoryPoolMXBean mpBean: ManagementFactory.getMemoryPoolMXBeans()) {
    if (mpBean.getType() == MemoryType.HEAP) {
        System.out.printf(
            "Name: %s: %s\n",
            mpBean.getName(), mpBean.getUsage()
        );
    }
}

Output Example:

Name: Eden Space: init = 6619136(6464K) used = 3754304(3666K) committed = 6619136(6464K) max = 186253312(181888K)
Name: Survivor Space: init = 786432(768K) used = 0(0K) committed = 786432(768K) max = 23265280(22720K)
Name: Tenured Gen: init = 16449536(16064K) used = 0(0K) committed = 16449536(16064K) max = 465567744(454656K)

If you have a question about "Eden Space" or "Survivor Space", check How java memory pool is allocated

+13
source

All Articles