'How to get/calculate survivor space heap memory usage in Java?
I want to get the survivor space memory capacity within a Java application.
I have tried using ManagementFactory class to get survivor space capacity, but it only fetches the capacity of one of the survivor spaces.
When I use jstat, I get the following result (sizes are in MB, C -> capacity, U -> used):
| S0C | S1C | S0U | S1U |
|---|---|---|---|
| 56320.0 | 4608.0 | 0.0 | 4238.9 |
ManagementFactory.getMemoryPoolMXBeans() fetches only the capacity of the survivor space which is not-empty (in this case S1C).
Due to this, there's a difference between Xmx value and max heap calculated using ManagementFactory.getMemoryPoolMXBeans() method. And the difference is exactly the size of S0C.
This is the code for reference:
long survivorSpaceMax = 0L;
for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mpBean.getName().equals("Survivor Space") || mpBean.getName().equals("PS Survivor Space") {
survivorSpaceMax = mpBean.getUsage().getMax(); // Returns the capacity of the non-empty survivor space
}
}
Is the any other way to get the total of both the survivor space capacities?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
