Fix: Error occurred during initialization of VM could not reserve enough space for object heap

Even though Java applets aren’t a popular web technology these days, there are countless reasons to deploy a Java virtual machine directly on a Linux server. If you try to run the Linux java command outright either on discrete hardware or inside of its own VM, then you might get an “error occurred during initialization of VM could not reserve enough space for object heap” message.

This probably looks rather odd because you more than likely have enough RAM to run the command, but it’s largely due to a specific quirk in the way that physical and virtual memory pages get used. Specifying some relatively large sizes should allow you to completely bypass this message and run the java command the way you would any other.

Method 1: Using Command Line Options

If you’ve tried to run java and gotten this message, then you’ve probably already run the free command to make sure that there’s ample supplies of memory to run the program in.

java & free commands

Notice that on our test machine we had some 2.3 GB of physical RAM and not a single page of virtual memory had gotten used yet. If you notice that you have a memory crunch, then you’ll want to close other things that you have running before trying it again. On the other hand, those who found that they have plenty of free memory can try to specify a size directly.

For example, on our machine we were able to run the command as java -Xms256m -Xmx512M and it worked like it would have otherwise been expected to. This constrains the heap size that the Java virtual machine attempts to reserve on startup. Since an unrestrained virtual machine could hypothetically do unusual things, it might throw error messages on an otherwise free system. You may also want to play around with those two values before you find the right combination.

This can be an issue regardless of what you’re running it on since the JVM has nothing to do with the type of VM you might be using to run GNU/Linux.

Method 2: Exporting the Variables to Make the Change Permanent

When you find a value that works you can export it to make it permanent for that session. For instance, we used export _JAVA_OPTIONS=’-Xms256M -Xmx512M’ from the bash command prompt and it allowed us to run the java command by itself without any other options until we logged out of our server.

It needed to be run again when we logged in another session, so you might want to add it to any relevant startup scripts if you plan to be using the java command quite often. We added the line to our .bash_login file and it seemed to work each time we used a login prompt without having to run it again, though you might have to find another location for it if you’re working with a different shell.

You may have noticed that only certain hardware configurations trigger this error message. That’s because it usually happens on machines with a great deal of physical RAM but lower ulimits for how to use it. Java will try to allocate a huge block only to be told it can’t, which it interprets as running out of memory.

Method 3: Printing Current Java Options

If you’ve been working at the command line and want a quick reference to what you’ve currently set the _JAVA_OPTIONS value to, then simply run echo $_JAVA_OPTIONS and it will immediately print out the current values. This is useful for troubleshooting when you’re trying to figure out the right numerals to try.

Keep in mind that while this fix shouldn’t require any other playing around, Java will throw out the “could not reserve enough space for object heap” message if you ever find yourself genuinely on the short end of virtual memory. If this is the case, then you’ll want to double check what processes are currently running and possibly restart the server if that’s an option. You could also create more swap space, but if this is an issue it’s generally better to try and correct it in some other way.

In the rare case that your settings seem to be right but it still isn’t working, make sure you’ve installed the 64-bit Java package since it should be immune to this problem. Contiguous memory requirements only apply to the 32-bit version of Java. We found in a handful of cases the 64-bit version tried to create a 32-bit virtual machine, so specifying the -d64 option on the command line fixed it for us.

ABOUT THE AUTHOR

Kevin Arrows


Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.