Monday, July 27, 2015

Force memory allocation for Java VM in Windows 7 32-bit


How can I start a Java program with large max heap?


I use a memory hog program (CrashPlan) written in Java, which sometimes, at peak use, needs some 1.5 GB of memory (this is controlled by the parameter -Xmx1500M).


Usually (and at least when starting) it needs very little memory, but with anything lower than -Xmx1500M it crashes at peak use. After start (with -Xmx1000M), Task Manager shows its memory usage about 100 MB in Working Set, Private Set, and Assigned Memory.


However, Java VM cannot start with -Xmx1500M, I guess because it fails to allocate this memory. (With -Xmx1000M it does start, but crashes later on.)


I use Windows 7 pro 32-bit (not 64-bit), with 32 GB memory installed (3 GB accessible, and pagefile on a large RAM disk in inaccessible memory).


I would expect that Windows would swap out other processes in order to allocate memory for Java, and since Java does not use it anyway, would swap Java's unused memory to pagefile. Say, right now I see mydefrag process using 1700 MB allocated set with no problem.


(It seems until last week it worked this way. Then something changed.)


How can I force Java to start with -Xmx1500M?


Answer



As you already realized the addressable memory of your user-process is limited to 2GB on 32-bit windows (half of the 4GB address-space is reserved for the OS). But you must also take into account that heap-memory is not the only kind of memory the Java-VM uses: there's also requirements for native memory-allocations, stack-space, perm-gen, code-cache etc. All of these have to come from the same 2GB that are available. So the exact maximum heap-size may vary between java-versions (maybe even between different computers), but according to Oracle



On most modern 32-bit Windows systems the maximum heap size will range from 1.4G to 1.6G.



So for anything that needs 1.5 GB up of heap you might be out of luck on a 32bit-JVM (and so on a 32-bit OS).


You might be able to gain some additional MB by reducing other memory-pools, e.g. by setting -XX:ReservedCodeCacheSize (default 32m) or -XX:MaxPermSize (default 64m), but that might lead to other problems while running your application.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...