Heap problems with different amounts of memory
Java and heap allocation isn’t really an issue when developing programs with known hardware limitations. If it’s a server application you know that there is probably enough RAM and it’s safe to set big values for -Xms and -Xmx.
When client applications come to play anything is possible. We initially gave -Xmx 1024 Mb, at the start and this wasn’t a problem as most computers nowadays have more than 1Gb memory (well at least in the field the program is used). We thought that we were safe but we were wrong. What we found out was that the amount of RAM isn’t the only factor that can give troubles. Major problems come from different OSes. For example I could give 1Gb as max heap in my Ubuntu box and yet I have 3.2 Gb available with ~10% usage (4 Gb of RAM but not all is available due to addressing issues). On one Win XP box with 2Gb of memory setting max heap to 1 Gb gave the usual:
Error occurred during initialization of VM
Could not reserve enough space for object heap
Now this leads to a difficult problem. How to set the limit so that those OS-es/computers could run with high max heap size that have it available and those with low max heap size could also do it. There are 3 different options:
- Simple configuration file where the user tells how much max heap he wants to use. The problem is that most of our users don’t know how or want to configure this, so this option is out.
- Second option would be running a little script that would test the limits of the system, but it’s really unreliable.
- Search a way to make heap allocation dynamic.
After some digging I found from Sun Java forums a thread that solved this problem. The solution would be using -XX:+AggressiveHeap option. It allowed the program to use ~1.5 Gb of heap. I don’t know what the Windows machines result would be, as I haven’t received the results yet (I’ll update later when the results come in).
But this solution isn’t perfect either. From one part of JVM hotspot documentation you can find following:
This option should be used with caution. Because it makes the JVM greedy with memory resources, it is not appropriate for many programs and makes it easier to run out of memory. For example, by using most of the 4GB address space for heap, some programs will run out of space for stack memory. In such cases -Xss may sometimes help by reducing stack requirements.
At the moment we will use this option and see if it causes some of the problems it mentioned earlier. But what I’d like to see is some flexibility in allocating heap. For example why can’t there be a soft maximum where we just tell that JVM would try to allocate our given maximum amount of heap but when it’s not available to use whats available. And when the hard limit is used then give the initialization error.
As I have no prior experience with this aggressive heap I don’t know what to expect. If anyone has some good suggestions I’d like to hear.
Hi,
I have tested -XX:+AggressiveHeap option on HP-UX with 4 CPUs, cca 6GB memory and 8GB swap. However, to my great surprise, the following error occurs:
Error occurred during initialization of VM
Could not reserve enough space for ParallelScavenge old gen heap
Does somebody have an idea where the problem is? Thanks in advance.
Hi,
Actually the problem I described was the first case I’ve dealt with memory issues like that, but maybe others have some ideas.
Was this problem got resolved? I’m facing ‘intialization’ issue after we have upgraded our server from Standard edition to Enterprise edition and we had installed 8GB RAM on the server. But we can’t allocated more than 1200MB.
Thanks,
Well we didn’t solve the memory assignment problem and later some optimizations and redesign reduced the memory footprint that it wasn’t an issue anymore, so unfortunately I can’t help.