1

I've set the options for a java process to use 80% of 1g max ram. But when I use 'ps -o vsz', I see it is using 3.5g (starting from 2.5g). This causes a lot of swap and thus freezing the device. Why is the discrepancy?

UPDATE: The options to the JVM are now: -Xmx256m -Xshare:off -XX:+UseSerialGC -XX:NativeMemoryTracking=summary -XX:MaxRAM=768m -XX:MaxRAMPercentage=60. They don't seem to change anything. The process starts at 2.4g and grows to 3.5g

UPDATE 2:

openjdk version "14" 2020-03-16
OpenJDK Runtime Environment (build 14+36)
OpenJDK 64-Bit Server VM (build 14+36, mixed mode)
IttayD
  • 28,271
  • 28
  • 124
  • 178
  • What does the Java process do? Does it allocate off-heap memory? – Kayaman Mar 20 '20 at 15:04
  • It reads files. It's 3rd party, so not really sure. How can I tell if it allocates off-heap memory (maybe mmap a file?) , and is there a way to limit this? – IttayD Mar 20 '20 at 15:09
  • And you're passing `-XX:MaxRAM=800m`? – Kayaman Mar 20 '20 at 15:16
  • I started with setting it to 1g. My options now: -Xmx256m -Xshare:off -XX:+UseSerialGC -XX:NativeMemoryTracking=summary -XX:MaxRAM=768m -XX:MaxRAMPercentage=60 – IttayD Mar 20 '20 at 15:20
  • Include your JDK version details, OS versions, and any other technical details you can think of. You also seem to be including multiple conflicting parameters (`Xmx`, `MaxRAM` and `MaxRAMPercentage`) so that's probably not going to help. – Kayaman Mar 20 '20 at 15:24
  • @Kayaman, I tried all combinations. None helps – IttayD Mar 20 '20 at 15:26
  • You're not supposed to try **all** the *combinations*, you're supposed to set the correct parameters. Which parameter are you trying to use? `Xmx`, `MaxRAM` or `MaxRAMPercentage`? You can't have 3 maximums at the same time. Also note that `MaxRAM` was deprecated in Java 10, so it's probably removed in 14. – Kayaman Mar 20 '20 at 15:28
  • I tried to use Xmx. Then MaxRam, then MaxRAM and MaxRAMPercentage, then all. – IttayD Mar 20 '20 at 15:29
  • Then let's assume you're stuck with `Xmx` and the process is allocating off-heap memory. Run a profiler (e.g. `visualvm`) on it and see what it's allocating. – Kayaman Mar 20 '20 at 15:30

2 Answers2

0

The right option is -Xmx1g:

  • -X: Option specific to this implementation of java.
  • mx: max heap memory
  • 1g: 1 gigabyte.

    You may want to also apply -Xms1g which sets the minimum. Now the RAM Load of your java app is stable.

Note that the memload of your VM can still be more than 1GB (though 3.5 sounds excessive): Heap isn't the only memory the VM uses; every thread takes '1 stack' worth, which is also configurable (via -Xss128k for example), so if you have a ton of threads, memory load goes up (with half a meg of stack per thread, 4000 threads imply 1 GB worth of stack memory!). The VM's own runtime stuff also exists outside of heap.

Also, the memory taken by shared libraries needs to be 'bookkept' by the OS; I believe usually OSes just add the full memory load of them to each and every process that uses the shared library, and java tends to claim that it 'uses' most of the ones that are already loaded regardless, which inflates the number as well.

Turns out measuring how much memory an app takes is surprisingly difficult sometimes.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
-1

-XX:MaxRam isn't the correct options use -Xmx

Ryan
  • 1,762
  • 6
  • 11