It is not uncommon that you need to watch the runtime performance of a Java application on a server. In this post, I'll show you how to set up a target JVM so you can monitor its cpu, heap memory, threads usage from a specific JMX client - VisualVM. VisualVM is similar to JConsole, a JMX-compliant monitoring tool, but is more advanced.
To use a JMX client, the setup you need to do first are:
Enable JMX Agent for JVM
Your java runtime is not enabled with JMX by default unless you explicitly specify to turn it on. With JMX agent enabled, you can use a JMX Client to tap into your JVM to monitor its memory usage and performance at runtime. To enable JMX for a JVM, please refer to Enable JMX Agent for JVM.
VirtualVM is like JConsole, but it has more advanced monitoring features for developers:
Connect to JMX Agent from VisualVM
To connect to a target server where JMX Agent is turned on with username and password required, from within VisualVM on your local machine, do these:
1. Select File > Add JMX Connection ...
If your target server where JMX Agent does not require username/password, just leave those blank when connecting.
VisualVM Quick Guide
To see what you usually see (cpu, classes, memory, etc.) as if you were using JConsole, double click on display name (e.g. controlRole or monitorRole) on the left panel as seen below:
To generate Heap Dump on the Target JVM server,
Select Monitor tab
To analyze threads and profile CPU & memory:
Access JMX Agent as controlRole also allows you analyze threads and profile CPU & memory. To learn more about profiling/sampling using VisualVM, please see this article.
To analyze garbage collection (GC)
If you just want to visually get an idea about how often the major GC or minor GC took place, use JConsole which should be available under $JAVA_HOME/bin directory. On your mac, you can simply launch JConsole:
The following JConsole screen shows the app has been using up memory that major GC keeps happening.
Similarly, you can use Visual GC plug-in for VisualVM. It requires jstatd on the server side. First install Visual GC plugin:
Alternatively, you may simply fall back on using JConsole to monitor memory usage, or consider to install GCViewer plug-in for VisualVM.