wood burning stoves*
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
Author

java.lang.OutOfMemoryError: Java heap space

Iman Paryudi
Greenhorn

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?

Environment:
8 GB RAM
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

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
Greenhorn

Joined: Mar 26, 2012
Posts: 11
Hi,
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: 296

Hi,

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.

Regards
Jatan

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

regards,
Vishal
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2341
    
  28

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
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.OutOfMemoryError: Java heap space