Inactive memory vs swapping

javamemoryvirtual-memory

I'm trying to process a large dataset in java, and so I set my -Xmx11G.

1 thing I observed is, when I use xmx and the Jvm starts, the amount of inactive memory jumps up by about that much. Later, I see my swap file jumps up, but my inactive memory still has about 7gb left. Why is the os not utilizing the inactive memory but insteads swap out to disk???

Best Answer

The general answer is that Java's pattern of memory usage differs from other apps as it uses garbage collection in a fixed size memory thus getting all the memory it might use up front rather than getting it when it is needed..

The start is the JVM allocating the memory, then I think its garbage collection tends to access the memory often or else you have allocated the large Java dataset and only accessing part of it. Thus the 11G of memory is accessed but not often enough to be marked as active. A C based program won't have a background thread looking at all the pages of memory so pages can be inactive long enough to be swapped out.

From experience albeit on other OSs Java really slows down if any of its memory is swapped and you adjust the size of its VM so it does not. If you need more memory then get more RAM or alter the algorithm to use less.