java.lang.OutOfMemoryError PermGen space in Maven build
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding java.lang.OutOfMemoryError: PermGen space in Maven Builds
The java.lang.OutOfMemoryError: PermGen space error is a common issue faced by developers working with Java applications, particularly during build processes using Maven. To understand this error thoroughly, it's essential to delve into the concept of PermGen space and how it relates to JVM (Java Virtual Machine) and Maven.
What is PermGen Space?
PermGen stands for Permanent Generation. It's a part of the heap memory allocated by the JVM used to store metadata associated with classes and methods. This space stores:
- The structure of the classes being loaded (such as fields and methods).
- Information regarding methods, constant pool, and method data.
- Interned Strings (prior to Java 7).
- Static content and metadata.
Why Does the OutOfMemoryError Occur?
The OutOfMemoryError for PermGen space usually signals that the JVM has run out of the default allocated memory for this region, causing it to fail when trying to load more class data. Several scenarios may lead to this error:
- Large Projects: Projects with numerous classes may exceed the default PermGen space limit.
- Dynamic Class Loading: Applications like those using reflection or frameworks that generate proxy classes dynamically.
- Web Applications: Repeated deployment and undeployment of web applications without proper memory management can cause classloader leaks.
Example Scenario
Consider a Maven build script for a large enterprise project with numerous dependencies:
Running mvn clean install might yield java.lang.OutOfMemoryError: PermGen space, especially if the build involves testing phases where additional classes and methods are loaded.
Common Solutions
To address and mitigate the PermGen space issue, consider these strategies:
- Increase PermGen Space Size: Add JVM arguments to your Maven build to increase the PermGen size.Example:
This command sets the PermGen space to 256 MB.
- Use of Maven Plugins: Customize builds using Maven plugins like the Maven Compiler Plugin or Maven Surefire Plugin, which can include JVM options to control memory allocation.
- Upgrade to Java 8 or Above: PermGen space has been replaced by Metaspace in Java 8, which dynamically expands according to your application's needs.
- Memory Leak Diagnostics: Use profiling tools (like VisualVM, JProfiler, or Eclipse Memory Analyzer) to analyze and fix classloader memory leaks, which often contribute to this error.
Summary Table
Here's a quick summary:
| Cause | Solution | Description |
| High number of loaded classes | Increase PermGen size | Increase JVM's PermGen space via MAVEN_OPTS or plugin configuration. |
| Repeated deployments without cleanup | Memory Leak Diagnostics | Use profiling tools to detect and fix classloader memory leaks. |
| Use Java 8 or above | Metaspace Replacement | Upgrade to Java 8 to leverage dynamic Metaspace allocation. |
Troubleshooting Tips
- Consistently monitor JVM memory usage using tools like
jstatorjconsole. - Verify classloader hierarchies to ensure proper unloading between deployments.
- Check for excessive usage of third-party libraries that might lead to unnecessary class loading.
Conclusion
Understanding the intricacies of JVM memory management, including PermGen space, is crucial for any developer, especially when dealing with large projects and complex build processes. By employing strategic memory configurations and utilizing appropriate Java versions and tools, you can effectively manage and mitigate PermGen space errors during your Maven builds.
Related reading
- java.lang.OutOfMemoryError unable to create new native Thread
- java.lang.RuntimeException Can't create handler inside thread that has not called Looper.prepare;
- java.lang.RuntimeException Failed to resolve Oracle database version
- java.lang.RuntimeException Unable to instantiate activity ComponentInfo
- java.net.ConnectException Connection refused
- java.net.MalformedURLException no protocol
- java.net.SocketException Connection reset
- java.net.SocketException socket failed EPERM (Operation not permitted)

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.