I have a large Java app that uses massive amounts of memory at times and I would like to monitor this at intervals to see if heap or permgen limits are reached or have gotten near to them.
How can I print out the most relevant pieces of mem usage information using java?
Here's a snippet from a piece of code we have that periodically logs the memory usage of our app:
import java.lang.management.GarbageCollectorMXBean
import java.lang.management.ManagementFactory
import java.lang.management.MemoryPoolMXBean
import java.lang.management.MemoryUsage
log("Heap", ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
log("NonHeap", ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage());
List<MemoryPoolMXBean> beans = ManagementFactory.getMemoryPoolMXBeans();
for (MemoryPoolMXBean bean: beans) {
log(bean.getName(), bean.getUsage());
}
for (GarbageCollectorMXBean bean: ManagementFactory.getGarbageCollectorMXBeans()) {
log(bean.getName(), bean.getCollectionCount(), bean.getCollectionTime());
}
You can try visualVM, a Java profiler that goes with every JDK. You will find it in the "bin" folder.
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