Slight difference of Non-heap memory usage as a Java program runs

1.1k Views Asked by At

I have a simple code for a multi-threaded echo server in Java (it returns whatever received back to the clients). I'm profiling the various resources including the non-heap memory usage. I understand the non-heap memory of JVM is also created at the JVM start-up, however, stores per-class structures and includes call stacks, memory allocated by native code for instance for off-heap caching, the Metaspace as well as memory used by the JIT compiler (compiled native code). But I have two main questions:

1) The non-heap memory usage slightly increases as time passes. The increase is tiny, but what causes this slight increase in the memory usage?

2) While still the non-heap memory usage is almost identical as the number of communicating clients increases, if we zoom in a lot, similarly we notice a small increase as the number of clients increases. What is the main reason for that? I was guessing it might be due to more space required for new threads, but what do these threads do to cause this slight increase?

3) We notice the same difference as in 2 above for Heap memory as well. However, the difference is much more. What is the reasons for that? My guess was increased buffer size required for storing messages.

enter image description here

1

There are 1 best solutions below

3
On

You said that your server is multi-threaded - I assume that you create a new Thread for each accepted socket connection.

In JVM each Thread consumes some memory called "Stack" where it stores all active execution frames. The default stack size on 64-bit JVMs is 1024K. I think that on this chart of non-heap memory you can see stacks allocated for socket threads