File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes java.lang.OutOfMemoryError: Java heap space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "java.lang.OutOfMemoryError: Java heap space" Watch "java.lang.OutOfMemoryError: Java heap space" New topic

java.lang.OutOfMemoryError: Java heap space

Iman Paryudi

Joined: Mar 26, 2012
Posts: 11
I am creating a program that will do 67,108,864 calculations using Netbeans. But when I run it, there is an error message: java.lang.OutOfMemoryError: Java heap space.

I have tried several things to overcome this problem:
1. I changed the setting of VM Options of the Project Properties. I even set the VM Options to -Xmx1024m.
2. I changed the setting of VM Options of the Tomcat Server. Here, I also set the VM Options to -Xmx1024m.
3. I disabled the HTTP Monitor.

What I have not tried is changing the content of netbeans.conf file since my computer does not allow me to change it.

So, my questions are:
1. How can I fix this kind of problem in a servlet project?
2. What is the maximum value of Xmx? Is it the capacity of the RAM?
3. How can I change the content of netbeans.conf file?

Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179

Please check your code whether it is falling into a recursive loop or something like that. Sometimes small mistakes in code have a huge penultimate effect.

Programming is about thinking, NOT coding
Iman Paryudi

Joined: Mar 26, 2012
Posts: 11
I have checked with the code below in the loop and it worked well.

Meanwhile what I want to do is using PreparedStatement since the value I want to save into the table is in the form of variables.
When I found the error, I used the code below:

jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 297


you should check for the memory leaks which can be possible reason why you are getting this error.You can analyse your code with the free tools available to check for the memory leak in Java.
Another thing you can do it dereferenced objects once the work is over for them also make sure you have close the database connections and file after the work is done.

There can be many reason for memory leaks.


Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Iman Paryudi wrote:I am creating a program that will do 67,108,864 calculations using Netbeans.

I guess the calculation need more space then, provided memory leaking isn't the culprit

Jayesh A Lalwani
Saloon Keeper

Joined: Jan 17, 2008
Posts: 2748

If you have a loop that runs 67 million times, and every iteration is adding 100 bytes to memory consumption, you are looking at total memory consumption of 6G! You need to look at the code inside the loop. Are you putting any objects in a collection that was created outside of the loop? Are you putting data in a buffer and you don't cache? Are you using Hibernate? If yes, are you flushing once in a while? Zoinks, I hope this big loop is not in a single transaction! I haven't tried it, but I suppose if you have a transaction with 67 million inserts, something is going to break somewhere! The JDBC driver might be caching something. Even if the driver is fine, the database is going to not like a big transaction like that

You should first start by getting a heap dump and analyzing the heap dump. I use MAT which is available from eclipse to do memory analysis. You can use VisualVM too. You can get the heapdump by either using JConsole, jmap or VisualVM. Or you can add the -XX:+HeapDumpOnOutOfMemoryError switch the Java options, and Java will create an heap dump whenever there is out of memory
Prateek Kumar Singh

Joined: Jan 21, 2011
Posts: 29

The problem occurs because of the JVm has run out of memory ,so you should run garbage collector operation on your JVM until the VM gets deallocated of unused memory .

When you get OutOfMemoryError, it means that (so obvious) your application doesn't have enough memory to go on. But the rest of the message, wants to inform you that it's not the common object heap space that it's lacking, but only the memory space for the binary code of classes and methods is lacking.
you can increase your heap size by following example command

here you can easily define Permanent Size(Permsize) and Maximumum permanent size(maxpermsize).

you can also try a solution to increase the total Ram size of your machine.

hope it helps.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

You shouldn't need to manually run the garbage collector. When the JVM needs memory and doesn't have enough, it will automatically run a garbage collection cycle. So if you get an OOME that means that even after this garbage collection cycle there still isn't enough memory available.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: java.lang.OutOfMemoryError: Java heap space
It's not a secret anymore!