6

I'm struggling with Java heap space settings. The default Java on Windows is the 32-bit client regardless of OS version (that's what Oracle recommends to all users). It appears to set max heap size to 256 MB by default, and that is too little for me.

I use a custom launcher to start the application. I would like it to use more memory on computers with plenty RAM, and default to -Xmx512m on those with less RAM. As far as I'm aware, the only way is the static -Xmx setting (that has to be set on launch).

I have a user who has 8 GB RAM, 64-bit Windows and 32-bit Java 7. Maximum memory visible to the JVM is 4G (as returned by querying OperatingSystemMXBean). I understand why, no issue.

For some reason my application is unable to start for this user with -Xmx1300m, even though he has 2.3G free memory. He closed some applications (having 5G free memory), and still it would not launch. The error reported to me was:

error occured during init of vm
could not reserve enough space for object heap

What's going on? Could it be that the 32-bit JVM is only able to address the "first" 4G of memory and has to have a 1300M block available within those first 4 gigabytes?

How can I solve this problem, except for asking everyone to install 64-bit Java (what is unlikely to be acceptable)?

EDIT: In case it matters, it is a fat Swing client, not an applet.

11
  • A 32bit windows process cannot use more than 3-ish gigs of ram, no matter how much memory is in the box, or if you're using 64bit windows. It could be even less, depending on how much hardware you have that requires memory mapping, which sucks up a chunk of the "available" space. There's hacks like PAE, but your maximum addressable ram chunk is still 4gig, even if you have multiple 4gig blocks allocated via pae.
    – Marc B
    Jun 24, 2013 at 16:09
  • @MarcB I know it can't use more than 3-ish gigs, but does it have to be in the "first" 4 gigs of total address space available to the host? Jun 24, 2013 at 16:14
  • nope. the cpu (and windows) will take care of mapping a 32bit app's process space ANYWHERE in the available 64bit space. If they have to be in the "first" 4gig, performance would be absolutely miserable if you had multiple 32bit apps running - every task switch would require copying around the entire 4gig space for each app as it starts/stops running its timeslices.
    – Marc B
    Jun 24, 2013 at 16:15
  • @MarcB Why does the JVM fail to launch this way then, given 5G free memory? Jun 24, 2013 at 16:17
  • 1
    perhaps there isn't a large enough contiguous chunk that can handle a 1.3gig allocation. just because youv'e got "3 gig" free, doesn't mean it's 3gig in a row.
    – Marc B
    Jun 24, 2013 at 16:18

5 Answers 5

2

It is not a question of memory but a question of address space.

On those 4 GB (2^32) theoretically addressable by a 32bit process, one must take into account the fact that the OS kernel needs a part of that address space (which obviously the process cannot touch).

And when you use Java, the address space of the java process itself is split further, between the heap, the permgen, native resources, the JVM itself etc.

You are using a 64bit OS. Run a 64bit JVM. Your bytecode (ie, all your jars) will run all the same. There is just no reason to be using a 32bit JVM!

10
  • 1
    Does it mean that only the first 4 GB are addressable by 32-bit JVM? User has 5G free, but probably only in the "higher" addresses. I know that 64-bit JVM is one answer, but I'm not writing the application for myself and given that Oracle so strongly recommends and defaults to 32-bit, I can't assume people will have the 64-bit. Jun 24, 2013 at 16:12
  • 2
    This problem is actually down to memory space fragmentation. HotSpot must have contiguous heap. Jun 24, 2013 at 16:25
  • @KonradGarus actually, a 32bit process, due to its inability to address more than 2^32 bytes, will have, as I explained, less than 4 GB available because of the OS kernel itself. With a 64bit kernel behind the scenes, the situation is a little better, but not much. I don't know where you saw Oracle recommend a 32bit JVM... Link?
    – fge
    Jun 24, 2013 at 16:31
  • @fre Default download at java.com. Also java.com/en/download/faq/java_win64bit.xml: 64-bit Windows operating systems (which may be Windows 7, Vista or XP) come with a 32-bit Internet Explorer (IE) browser as the standard (default) for viewing web pages. These operating systems also include a 64-bit Internet Explorer browser, however using it is optional and it must be explicitly selected to view web pages. Note that because some web content may not work properly in a 64-bit browser, we recommend using the default 32-bit browser and downloading 32-bit Java. Jun 24, 2013 at 16:34
  • 1
    Keep in mind that if the program uses 32-bit native libraries, then it will not run on a 64-bit JVM without also switching to 64-bit versions of those libraries (if they are available).
    – rob
    Jun 24, 2013 at 19:00
2

Why won't it work?

As others have mentioned, this particular user's computer most likely does not have a large enough contiguous block of free memory for the JVM in a 32-bit address space. The maximum 32-bit heap space is system-dependent (note that both the OS and the exact JVM version make a difference) but is usually around 1100-1600 MB on Windows. For example, on my 64-bit Windows 7 system, these are my maximum -Xmx sizes for the specific 32-bit JVM versions I have installed:

  • Java 7: between 1100m and 1200m
  • Java 6: between 1400m and 1500m
  • Java 5: between 1500m and 1600m

The remaining memory allocated to the process is used by the OS (or emulator, in the case of a 32-bit process on a 64-bit host), the JVM itself, and other structures used by the JVM.

Recommended solution: bundle a 64-bit JVM with your application

If you cannot get the client to install a 64-bit JVM, bundle one with your application. The 64-bit address space will have a contiguous block of memory larger than 1300 MB free, even if there is not necessarily a large enough contiguous block of physical memory available.

If your software is a standalone application, it's a piece of cake to bundle the JVM. If the launcher is an applet, you might have to have your applet check for the 64-bit JVM in a known location (and download it if necessary) before launching your application.

Don't forget about dependencies

If your application uses 32-bit native libraries, make sure you can also get 64-bit versions of those native libraries.

What if you can't bundle a JVM or have 32-bit native dependencies?

There's really no reason why you shouldn't be able to bundle a JVM with your application, but you might have some 32-bit native dependencies that haven't been ported to 64-bit--in which case, it won't matter whether you bundle a JVM because you're still stuck with 32-bit. In that case, you can make your launcher perform a binary search to determine the maximum heap size by repeatedly executing java -Xmx####m -version and parsing the output (where #### is the Xmx value, of course). Once you've found the maximum Xmx, you can launch your program with that value. (Note: a slightly safer option would be to simply try to run your program and check for the heap space error, decreasing Xmx after each failed attempt to launch your program.)

You'll need to use a smaller Xmx value if you get an error message like one of the following:

Java 7:

Error occurred during initialization of VM
Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

Java 6:

Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

Java 5:

Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

But if you get something like the following, you know you've either found the maximum or you can try a larger Xmx value:

Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
Java HotSpot(TM) Client VM (build 23.21-b01, mixed mode)
1

I agree with the previous answers. Just a couple of additional comments. Yes 32-bits can theoretically access 4GB, however in Windows the top half of this reserved for the operating system and the bottom half is for all applications. Since Windows considers Java a "user" program, not part of the OS, the best you could ever do is 2GB. And in practice it's a lot less than that. 1.2GB sounds about right.

However, when running in 32-bit mode, I would actually recommend dropping down to 1024M. If you absolutely max out the heap space, you may run into a more serious problem where you run out of "native memory". And if you have never experienced this before it's a real treat - because instead of getting a nice Java stack trace, the whole JVM immediately crashes.

And I agree with everyone else that you need to bite the bullet and enhance your application to support the 64-bit JVM. In my case we have a service wrapper so we needed to redistribute both JVMs and then a 32-bit service wrapper and a 64-bit service wrapper. The user could then register either the 32-bit or 64-bit version as needed.

1

Windows XP is limited to 1.2 - 1.4 GB of continuous memory. Even if you have a 64-bit JVM, the 32-bit emulation works the same as it does for Windows XP for compatibility i.e. the also has the same limitations.

If you want to use more memory, run the 64-bit JVM. Unless you have 32-bit DLLs, there is littel reason not to.

0

The application I work on requires as much memory as I can give it. through trial and error I have found that under Windows the most I can reliably assign to a 32-bit JVM is about 1200 MB. It varies slightly but I've never known it drop below this. Under Linux running OpenJVM I can sometimes assign 1300MB. There are many reasons for this limit but from what I've read one of the main issues stopping the JVM acquiring a larger heap than this is the requirement that the heap be one contiguous block of memory.

As you are on a 64bit machine, running a 64bit operating system I'd strongly recommend just switching to a 64bit JVM. You can then assign essentially unlimited amounts of memory. My experimentation however indicates that over about 10GB of memory it's a serious case of diminishing returns as the JVM doesn't seem to use it very well and performance suffers. I believe Java 8 will have better management of large amounts of memory.

2
  • What's the point of switching to 64-bit JVM is the issue is that there is no contiguous free 1300-meg block? Jun 24, 2013 at 16:27
  • 1
    @KonradGarus Within 64-bit address space there will certainly be a countiguous 1300-meg block. Note that we are talking about virtual memory here. Jun 24, 2013 at 16:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.