views:

946

answers:

7

I am getting the following error on execution of a multi-threading program

java.lang.OutOfMemoryError: Java heap space

The above error occured in one of the threads.

  1. Upto my knowledge, Heap space is occupied by instance variables only. If this is correct, then why this error occurred after running fine for sometime as space for instance variables are alloted at the time of object creation.

  2. Is there any way to increase the heap space?

  3. What changes should I made to my program so that It will grab less heap space?

A: 

To increase the heap size you can use the -Xmx argument when starting Java; e.g.

-Xmx256M

Adamski
A: 

No, I think you are thinking of stack space. Heap space is occupied by objects. The way to increase it is -Xmx256m, replacing the 256 with the amount you need on the command line.

Yishai
+9  A: 

If you want to increase your heap space, you can use java -Xms<initial heap size> -Xmx<maximum heap size> on the command line. By default, the values are 32m and 128m. You can find out more about the VM options on the Java website.

However, I would recommend profiling your application to find out why your heap size is being eaten. NetBeans has a very good profiler included with it. I believe it uses the jvisualvm under the hood. With a profiler, you can try to find where many objects are being created, when objects get garbage collected, and more.

Thomas Owens
I am using Netbeans but I don't know how to use profiler. I would like to know more about the profiler so that I can use it to find memory leaks in my application.
Yatendra Goel
I added a link to a page on the NetBeans site (http://profiler.netbeans.org/) which has very good documentation about the profile, from the very basics to more advanced use.
Thomas Owens
+1  A: 

1.- Yes, but it pretty much refers to the whole memory used by your program.

2.- Yes see Java VM options

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size

Ie

java -Xmx2g assign 2 gigabytes of ram as maximum to your app

But you should see if you don't have a memory leak first.

3.- It depends on the program. Try spot memory leaks. This question would be to hard to answer. Lately you can profile using JConsole to try to find out where your memory is going to

OscarRyz
Why would `while (true);` give an out-of-memory error?
Michael Myers
which `while( true );` ??? ;)
OscarRyz
+2  A: 
  1. Local variables are located on the stack. Heap space is occupied by objects.

  2. You can use the -Xmx option.

  3. Basically heap space is used up everytime you allocate a new object with new and freed some time after the object is no longer referenced. So make sure that you don't keep references to objects that you no longer need.

sepp2k
A: 

You may want to look at this site to learn more about memory in the JVM: http://developer.streamezzo.com/content/learn/articles/optimization-heap-memory-usage

I have found it useful to use visualgc (http://java.sun.com/performance/jvmstat/visualgc.html) to watch how the different parts of the memory model is filling up, to determine what to change.

It is difficult to determine which part of memory was filled up, hence visualgc, as you may want to just change the part that is having a problem, rather than just say,

Fine! I will give 1G of RAM to the JVM.

Try to be more precise about what you are doing, in the long run you will probably find the program better for it.

To determine where the memory leak may be you can use unit tests for that, by testing what was the memory before the test, and after, and if there is too big a change then you may want to examine it, but, you need to do the check while your test is still running.

James Black
A: 
  1. In most of the cases, the code is not optimized. Release those objects which you think shall not be needed further. Avoid creation of objects in your loop each time. Try to use caches. I don't know how your application is doing. But In programming, one rule of normal life applies as well

    Prevention is better than cure. "Don't create unnecessary objects"

DKSRathore