Blog |

How to Fix OutOfMemoryError: Permgen Space Exceptions in Java

How to Fix OutOfMemoryError: Permgen Space Exceptions in Java
Table of Contents

A java.lang.OutOfMemoryError: PermGen Space is a runtime error in Java which occurs when the permanent generation (PermGen) area in memory is exhausted. The PermGen area of the Java heap is used to store metadata such as class declarations, methods and object arrays. Therefore, the PermGen size requirements depend on the number of classes and methods as well as their size.

Java memory is separated into different regions - Young, Tenured and PermGen. The sizes of these regions, including the PermGen area, are set when the JVM is launched. If these sizes are not specified, platform-specific defaults are used. Since Java applications are only allowed to use a limited amount of memory, the PermGen can easily be exhausted if there are a large number of classes, objects or methods in an application.

The size of the PermGen can be specified using the JVM arguments -XX:PermSize and -XX:MaxPermSize depending on the application needs.

What Causes java.lang.OutOfMemoryError: PermGen Space

The PermGen area of the Java heap mostly contains the following:

  • Class declarations, including class names and fields
  • Methods with the method bytecode
  • Constant pool information
  • Object arrays and type arrays associated with a class
  • Just In Time compiler optimizations

The PermGen size depends on both the number of classes loaded as well as the size of class declarations. Therefore, the main cause for java.lang.OutOfMemoryError: PermGen space is either too many classes or too large classes loaded in the PermGen.

java.lang.OutOfMemoryError: PermGen space Example

Here is an example of a java.lang.OutOfMemoryError: PermGen space thrown due to a large number of classes loaded into the JVM:

import javassist.ClassPool;

public class OutOfMemoryErrorPermGenExample {
    public static void main(String[] args) throws Exception {
        ClassPool pool = ClassPool.getDefault();
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            pool.makeClass("com.example.MyClass" + i);
        }
    }
}

In this example, a loop in the main() method generates a large number of classes at runtime using the javassist library. The definitions of these new classes are loaded into PermGen space until the space is fully exhausted and a java.lang.OutOfMemoryError: Permgen space is thrown.

How to Solve java.lang.OutOfMemoryError: PermGen space

The java.lang.OutOfMemoryError: PermGen space error in Java occurs when the PermGen area of the heap is exhausted. As such, the application needs more space to load the classes into the PermGen area. Therefore, to fix this error, the maximum size of the PermGen area can be increased using the -XX:MaxPermSize JVM attribute. The initial size of the PermGen can also be specified (or increased) using the -XX:PermSize attribute. As an example:

export JVM_ARGS="-XX:PermSize=64M -XX:MaxPermSize=256M"

The above configuration will set the initial size of the PermGen area to 64MB and allow it to grow up to a maximum 256MB.

Furthermore, heap dumps can also be taken at the time of the error and analyzed to find out which objects cause the java.lang.OutOfMemoryError: PermGen space. This information can then be used to reduce the usage of these objects (if possible) to avoid the error.

Track, Analyze and Manage Java Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates Java error monitoringand triaging, making fixing Java errors easier than ever. Try it today.

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape