Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch4j: HeapPercent for multiple applications

Say I have three Java applications; A and B are 32 bit, C is 64 bit. All of them are configured with

maxHeapPercent = 50
maxHeapSize = 1024

which reads "50 percent of available memory no less than 1024 MB", according to the documentation. Now my question is, what will happen if I run these applications on the same machine?

  1. Say the machine has 16GB RAM, will those "50%" add up to 3x8GB=24GB, which is more than available?
  2. Or do A and B (being 32 bit applications!) only allow 4GB each, using 2GB of it? Ending up at 2x2GB+1x8GB=12GB? Or 2x4GB+1x8GB=16GB, leaving nothing for the OS?
  3. Will any of this force a crash and/or an OutOfMemoryError? Or will it just force swapping?
like image 893
crusy Avatar asked Jun 25 '20 09:06

crusy


Video Answer


1 Answers

The memory usage of the heap of (Java) applications is only a part of the memory that the applications requires. Other typical uses of memory are:

  • Stack memory for each application Thread
  • application code, (shared) libraries
  • operating system purposes, including caches that can avoid repetitive disk I/O
  • for Java: native memory, e.g. for native implemented functions like ZIP

So besides crashing there is also a great risk of slowing down the system, even before the applications are swapped/paged out to disk, but just for lack of cache. And for Java with a small heap, very frequent Full GC cycles (scanning all object on the heap) will slow down your application long before OutOfMemoryError is thrown.

If you want to see for yourself what the MaxHeapSize becomes, try running it with PrintFlagsFinal like:

java -XX:+PrintFlagsFinal |find "MaxHeapSize"

on Windows or on Unices:

java -XX:+PrintFlagsFinal |grep "MaxHeapSize".

Maybe you can lower the memory usage by using a smaller stack size or even a different VM (Eclipse OpenJ9 donated by IBM, or GraalVM Native Image) while making the application start faster too, or look into CDS and other optimizations (although that won't work across 32/64 bit architectures)

like image 127
JohannesB Avatar answered Oct 01 '22 10:10

JohannesB