Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


adjusting heap to combat OutOfMemory

Posted on 2003-11-10
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 225 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:

jprofiler: http://www.jprofiler.com/

plugin for eclipse: http://jmechanic.sourceforge.net/

hope that helped...

Bye Tom
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

609 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