adjusting heap to combat OutOfMemory

Posted on 2003-11-10
Last Modified: 2013-11-23
I'm writing an application with huge memory requirements for WinXP (developing using JCreator Pro). I routinely get OutOfMemory errors when running this application and tried to adjust the heap sizes to avoid such errors. The following is the command line i use:

-Xms256m -Xmx512m

to set the initial and maximum heap sizes. When I run the application, the Windows Task Manager indicates that the application uses up to about 72 MB memory before I get the OutOfMemory error.

What am I doing wrong, since I want java to use up to 512 MB to run the application, and it only appears to use about 72MB before returning the OutOfMemory error?

Any thoughts?
Question by:zacbell
  • 3
  • 2
  • 2
  • +2
LVL 24

Expert Comment

ID: 9720664
I think it means that suddenly "something" asks for at least 512-72 Mb more.
LVL 35

Expert Comment

ID: 9720669
have you tried:

-vmargs -Xmx512m


Accepted Solution

OptivoTom earned 75 total points
ID: 9723828
First of all (from my experience) the Task Manager does not correctyl display memory usage after the garbage collection starts to collect objects for the first time... espacilly if the garbage collector has to run heavily (because you are running out of memory). Try to add columns to the display of the task manager which display virtual memory, max. memory etc. to get a better picture of what you system is doing. Most of the time windows starts to swap heavily and thereby suggesting (by the task manager) it only uses a fraction of the available memory. If you experience a dramatic (erratic) change of memory usage (lets say jumping from 72 to 500 and back to 120 in just a  couple of seconds) it is a clear indication that the garbage collector is freeing memory which the VM is consuming instantly and as the system does not have the amount of required memory installed it swaps...

In sumary ... don't trust the taskmanager :-( Most certainly the VM really uses all the memory and then just quits. You could counter that by increasing the initial and max heap size of your VM as you allready did... other than that more RAM would be an option ;-)

To really get to the source of the problem would be to profile your application as to how much memory is allocated to your object instances. Some good tools to start with would be:


plugin for eclipse:

hope that helped...

Bye Tom
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 9769635
Per Tom's suggestion, I installed an evaluation version of JProfiler. Here's what's interesting: when I run my program using JProfiler, it uses up to about 300 MB memory and successfully runs to completion. However, when I run the program directly using java.exe, it only uses about 75 MB of memory and fails with an OutOfMemory error. There's no indication from JProfiler that there is erratic memory usage (i.e. jumping from 50MB to 500MB) - just smooth memory growth as the program executes.

Here are the command line arguments for running with and without JProfiler:

C:\j2sdk1.4.2_02\bin\java.exe "-Xint" "-Xrunjprofiler:port=31769" "-Xbootcla
sspath/a:C:\Program Files\jprofiler\bin\agent.jar" "-classpath" "my path" "class_file"

C:\j2sdk1.4.2_02\bin\java.exe -classpath "my path" class_file -vmargs -Xms256m -Xmx512m

Any ideas why the occurance of a memory error depends on whether JProfiler is running or not? Or perhaps there's something else different between running with and without JProfiler?
LVL 24

Expert Comment

ID: 9769646
I do hope it's not another bug in 1.4.2_02
Can you try to run it somewhere 1.4.1 or 1.4.2?

Expert Comment

ID: 9769757
What JProfiler does is: load itself and than invoke your main class. It might be possible, that it changes the settings for initial and maximum heap size but I doubt it. So what is a difference is, that by running your application "through" JProfiler you effectively ommit the arguments for inital and max heap.

Speaking of that... I would strongly recommend to set the inital heap size to either 1/4 of the max heapsize, the same as the max heap size or (best) not set it at all. Because setting the minimum heapsize prevents the JVM also from shrinking the heap as it would see fit.

My guess would be to ommit the -Xms setting and set the -Xmx setting to approcimately 1 1/2 of your physical memory.

Bye Tom

Author Comment

ID: 9776508
I think I figured out the problem, although it turned out to be terribly unsatisfying. I found that I was specifying the heap size command line options after the class file, instead of before it - so Java had no idea I was specifying the heap size. Everything worked as expected when I put the options where they should have gone. Thanks for everyone's help - I do apologize that the problem was such a simple mistake.
LVL 24

Expert Comment

ID: 9776682
You're wrong: the simplest mistakes are the most important.
I just read that in 1995 a Peruvian airplane went down because of a piece of adhesive tape
on a control.
And we all learn from this question too!
LVL 14

Expert Comment

ID: 10232789
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

[paq'ed and points refunded]

Please leave any comments here within the next seven days.
EE Cleanup Volunteer
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to track down Infection in a Server 2008 domain user profile 7 51
diffSum example 4 36
java stored proc example 9 30
jdbc error in jsp application 20 45
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question