Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.OutOfMemoryError: Java heap space

 
Iman Paryudi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Iman Paryudi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic