Q:

Memory sampling with jvisualvm

I am using memory sampling feature of Java Visual VM and I can't answer the questions:

Why are there so many instances created?
Why does these numbers of instances and bytes keep increasing?

For instance, I created a very simple Java program as follow:
public class TestMemory{
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Hello");
        Thread.sleep(60 * 60 * 1000);
    }
} 

And I can see in Java Visual VM that there are 611 classes. The number of instances keep increasing from 25,000 up to near 50,000 and then fall back to around 25,000. The reason for the decreasing of instances might be caused by GC, but I don't know why those numbers are so dynamic even though I am not doing anything with my program.

A:

The number of instances and bytes that you are looking at is the cost of monitoring an app from VisualVM. VisualVM is polling various JVM MBeans, continuously to analyze the value change from your application.
To confirm this, you can go to the Sampler tab. Click on the Per thread allocation sub tab. See if the busy thread is RMI TCP Connection(n). This thread should have a high bytes allocation per second.
I was curious on how to optimize this too, hence posting a more specific question.

