Dynamically set the maximum heap size of the java process
I have a java program started by a batch file, and its line is as follows:
javaw -Xms64m -Xmx1024m com.acme.MyProgram
However, on some computers, the program will not start and display the following message:
The problem seems to be 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 1024m to 512M seems to solve the problem
Is there any way to determine in advance how much memory is available on the computer (from the batch file) and whether to use - xmx1024m or - xmx512m in the batch file call? Please note that this batch file only needs to run on windows
Solution
In fact, the Java VM has done something similar If - XMS or - Xmx is not specified, these values are inferred from the amount of physical memory on the computer Or at least this page
You can set - XMS to the minimum heap size useful to your application and have Java determine the appropriate value for - Xmx