Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Heap memory usage fluctuation

Hello dear fellow developers,

I am facing issue with heap memory usage in my java application. Application itself is simply accepts Socket connections

Main Thread I have following script nothing more (except 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 Application is running, it should block loop until someone gets connected... Which means my Main thread is on waiting state most of the time...

The problem is: When no one is connected to my Server, Heap memory usage is fluctuating (see screenshot below) Which means there is a memory leak? no? Or it is the natural way how java application behaves?

JConsole

Thanks in advance...

like image 469
JavaMachine Avatar asked Jun 17 '26 23:06

JavaMachine


2 Answers

If you are using visualvm or jconsole or jmc to monitor heap usage it uses RMI and JMX which creates quite a lot of garbage. i.e. it is your monitoring which is doing this.

BTW even if you have a simple program

System.in.read();

these tools will show that garbage is being created, but not by your program.

I suggest you try instead use

jps -lvm

to get the process id or pid

jstat -gccause {pid} 10s

to monitor the memory usage. This also creates a small amount of garbage much far less.

like image 185
Peter Lawrey Avatar answered Jun 19 '26 12:06

Peter Lawrey


Your memory usage is fluctuating between 0 and 20MB and not increasing. There is neither a memory leak or any reason why you should pay any attention to this.

like image 42
Kayaman Avatar answered Jun 19 '26 13:06

Kayaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!