I'm trying to optimize the memory usage of my application. Unfortunately, running my application with -Dcom.sun.management.jmxremote and connecting it via VisualVM has quite a big impact on the heap usage. At first I thought that it is my application problem, until I wrote a very simple program to confirm that it is indeed the JMX's overhead. Below is the image of the activity.
After reading this, I come to understand that this is due to the way VisualVM retrieves the data, which is by polling the connected application continuously. I confirmed this by looking at the VisualVM's memory sampler. The RMI TCP Connection(n) thread is allocating 180kb / second.
My question is, is there a way to reduce the heap usage impact of VisualVM? Changing the polling interval might not be preferable as I'm interested with the heap information. If this is not possible, can we exclude the data used for VisualVM? At least I want to be more focus on my application.
Running Code
Object object = new Object();
synchronized (object) {
object.wait();
}
VisualVM's Heap Monitor activity
You cannot reduce the heap size beyond a point; the heap should be large enough for all objects that are live at the same time. Preferably, the heap must be at least twice the size of the total amount of live objects, or large enough so that the JVM spends less time garbage collecting the heap than running Java code.
High heap usage occurs when the garbage collection process cannot keep up. An indicator of high heap usage is when the garbage collection is incapable of reducing the heap usage to around 30%. In the image above you can see normal sawtooth of JVM heap.
The Heap and the Nursery When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
The simplest thing to do is to reduce the polling interval. The impact is proportional to the rate to get data.
What I do is use YourKit which does everything in native memory so has no impact on the heap.
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