Java.Lang.Outofmemoryerror: Permgen Space

Dealing with java.lang.OutOfMemoryError: PermGen space error

The solution was to add these flags to JVM command line when Tomcat is started:

-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

You can do that by shutting down the tomcat service, then going into the Tomcat/bin directory and running tomcat6w.exe. Under the "Java" tab, add the arguments to the "Java Options" box. Click "OK" and then restart the service.

If you get an error the specified service does not exist as an installed service you should run:

tomcat6w //ES//servicename

where servicename is the name of the server as viewed in services.msc

Source: orx's comment on Eric's Agile Answers.

java.lang.OutOfMemoryError: PermGen space Exception

Instead making changes in eclipse.ini, its better to install Java 8.

I Installed Java 8 and now I am not getting java.lang.OutOfMemoryError: PermGen space Exception.

getting Error [junit] java.lang.OutOfMemoryError: PermGen space

If you have memory allocation errors related to the "permGen" space, it means you run with a Java 7 or inferior. So, as a side note, if you run a Java 8 or superior, you shouldn't see this error anymore since that space doesn't exist per se anymore and is only limited to the available native memory.

The JVM option to manage the PermGen space is XX:MaxPermSize. So you should add to the JVM with runs your unit tests this command line argument: -XX:MaxPermSize=256m.

The ANT_OPTS environement variable will configure the JVM used by Ant itself. You should use this environement variable if the unit tests are not ran as forked.

If you run your unit tests in a JVM forked by Ant, then you should tell Ant to start the Junit task with some additionnal JVM arguments. The parameter you need to use is jvmarg.

See the official Ant documentation about the task: https://ant.apache.org/manual/Tasks/junit.html

Thus, here is the piece of XML to set the proper property:

<junit fork="yes">
<jvmarg value="-XX:MaxPermSize=256m" />
</junit>

How to deal with java.lang.OutOfMemoryError: Java heap space error?

Ultimately you always have a finite max of heap to use no matter what platform you are running on. In Windows 32 bit this is around 2GB (not specifically heap but total amount of memory per process). It just happens that Java chooses to make the default smaller (presumably so that the programmer can't create programs that have runaway memory allocation without running into this problem and having to examine exactly what they are doing).

So this given there are several approaches you could take to either determine what amount of memory you need or to reduce the amount of memory you are using. One common mistake with garbage collected languages such as Java or C# is to keep around references to objects that you no longer are using, or allocating many objects when you could reuse them instead. As long as objects have a reference to them they will continue to use heap space as the garbage collector will not delete them.

In this case you can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place. One option which I have used in the past is "JMP" http://www.khelekore.org/jmp/.

If you determine that you are allocating these objects for a reason and you need to keep around references (depending on what you are doing this might be the case), you will just need to increase the max heap size when you start the program. However, once you do the memory profiling and understand how your objects are getting allocated you should have a better idea about how much memory you need.

In general if you can't guarantee that your program will run in some finite amount of memory (perhaps depending on input size) you will always run into this problem. Only after exhausting all of this will you need to look into caching objects out to disk etc. At this point you should have a very good reason to say "I need Xgb of memory" for something and you can't work around it by improving your algorithms or memory allocation patterns. Generally this will only usually be the case for algorithms operating on large datasets (like a database or some scientific analysis program) and then techniques like caching and memory mapped IO become useful.

How to prevent java.lang.OutOfMemoryError: PermGen space?

This sometimes happens if you compile huge codebases - a lot of classes get loaded into the VM running sbt.

You need to increase the PermGen space for sbt - use the flag -XX:MaxPermSize=256m, where 256 you can change with the desired size of the permanent generation.

Run:

cat `which sbt`

to locate you sbt startup script. Then edit it to include the flag with the java command that runs the sbt launcher in the similar way as it is described here for modifying -Xmx and -Xms.

Adding the -XX:+CMSClassUnloadingEnabled flag should also enable sbt to unload the classloaders with classes from the previous compilation runs that are no longer being used.

EDIT:

Alternatively, you can set these options in the SBT_OPTS environment variable if you are using the extended script for running sbt.



Related Topics



Leave a reply



Submit