Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Commandline parameter to release unused memory

In Bash, I use the commmand java -Xmx8192m -Xms512m -jar jarfile to start a Java process with an initial heap space of 512MB and maximum heap space of 8GB.

I like how the heap space increases based on demand, but once the heap space has been increased, it doesn't release although the process doesn't need the memory. How can I release the memory that isn't being used by the process?

Example: Process starts, and uses 600MB of memory. Heap space increases from 512MB to a little over 600MB. Process then drops down to 400MB RAM usage, but heap allocation stays at 600MB. How would I make the allocation stay near the RAM usage?

like image 664
hexacyanide Avatar asked Dec 21 '22 19:12

hexacyanide


2 Answers

You cannot; it's simply not designed to work that way. Note that unused memory pages will simply be mapped out by your hardware, and so won't consume any real memory.

like image 123
Ernest Friedman-Hill Avatar answered Dec 24 '22 03:12

Ernest Friedman-Hill


Generally you would not like JVM to return memory to the OS and later claim in back as both operations are not so cheap.

There are a couple XX parameters that may or may not work with your preferred garbage collector, namely

  • -XX:MaxHeapFreeRatio=70 Maximum percentage of heap free after GC to avoid shrinking.
  • -XX:MinHeapFreeRatio=40 Minimum percentage of heap free after GC to avoid expansion.

Source

I believe you'd need stop the world collector for them to be enforced. Other JVMs may have their own parameters.

I'd normally have not replied but the amount of negative/false info ain't cool.

like image 20
bestsss Avatar answered Dec 24 '22 02:12

bestsss