Java heap memory usage fluctuates
Dear developer,
I encountered heap memory usage problems in Java applications The application itself only accepts socket connections
The main thread does not have the following script (except for the static executorservice instance):
ServerSocketFactory serverFactory = ServerSocketFactory.getDefault(); ServerSocket server = serverFactory.createServerSocket(Configuration.port); for(;;) { Socket client = server.accept(); Configuration.getExecutor().submit(new Client(client)); }
When the application is running, it should block the loop until someone connects... Which means that my main thread is waiting most of the time
The question is: when no one is connected to my server, the heap memory usage fluctuates (see the screenshot below). Does this mean a memory leak? No, Or is this the natural way Java applications behave?
Thank you in advance
Solution
If you use visual VM or jconsole or JMC to monitor heap usage, it will use RMI and JMX, which will generate a lot of garbage In other words, this is your surveillance
By the way, even if you have a simple program
system.in.read();
These tools will show that you are creating garbage, but will not show your program
I suggest you try it instead
jps -lvm
Get process ID or PID
jstat -gccause {pid} 10s
Monitor memory usage This will also produce a small amount of waste