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?
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.
Joined: Aug 09, 2012
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
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
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.
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.