Dynamically setting the maximum heap size for a Java process

I have a Java program that starts with a batch file with this line:

javaw -Xms64m -Xmx1024m com.acme.MyProgram

However, on some computers, the program does not start and displays the following message:

Failed to reserve enough space for the heap of objects. Failed to create Java virtual machine.

The problem is that the maximum size of the memory allocation pool is larger than the computer can handle. Reducing the maximum size of the memory allocation pool from 1024 m to 512 m seems to solve the problem.

Is there a way to determine how much memory is available on a computer before (from within a batch file) and determine whether to use it -Xmx1024mor -Xmx512min a batch file call? Please note that this batch file should only work on Windows.

+3
source share
4 answers

In fact, the Java VM is already doing something similar. If you do not specify -Xmsor -Xmx, then these values ​​are derived from the amount of physical memory on the computer. Or at least that's what this page says .

-Xms , , Java -Xmx.

+6
+1

512 , .

, , , , .

0

, , . , - , , ... 10 .

, -

, 512, , , 1024 . GC .

If you are sure that you will use a certain number of bars (say, the heap will easily fill the selection 512), you should probably set min to this number. Setting both min and max to 512 is good if your program allocates a lot of material but is not situational (always uses about the same amount of RAM)

0
source

Source: https://habr.com/ru/post/1735990/


All Articles