I have two linux machines (both are VMs), one having 12GB memory and other having 8GB memory.
I tried to start the same java program on both machines, with maximum max heap size possible (using -Xmx flag). Following are the results I got.
12GB machine: 9460MB
8GB machine: 4790MB
If I specify a max heap size beyond above limits, I get below error.
Error occurred during initialization of VM
Could not allocate metaspace: 1073741824 bytes
I checked the free memory in two systems (using free command), and I got following.
12GB machine: approximately 3GB free.
8GB machine: approximately 4GB free.
My question is, what determines the maximum max heap size a java program can be started with, which would not result in above mentioned error? (System had sufficient memory to allocate 1073741824 bytes of memory when the program gave above error)
解决方案
I did some experiments with the clues given by ravindra and found that the maximum max heap size has a direct relationship with the total virtual memory available in the system.
Total virtual memory in the system can be found (in KB) with:
ulimit-v
Total virtual memory can be altered with:
ulimit -v
The maximum max heap size possible was approximately 2GB less than the virtual memory. If you specify unlimited virtual memory using ulimit -v unlimited, You can specify any large value for the max heap size.