java.lang.OutOfMemoryError is subclass of java.lang.VirtualMachineError. It throws when the JVM cannot allocate an object because of out of memory, and no more memory could be made available by the garbage collector. OutOfMemoryError objects may be constructed by the virtual machine as if suppression were disabled and/or the stack trace was not writable.
Types of OutOfMemoryError in Java
Mainly two types of java.lang.OutOfMemoryError in Java:
-
- The java.lang.OutOfMemoryError: Java heap space
- The java.lang.OutOfMemoryError: PermGen space
Though both of them occur because JVM ran out of memory they are quite different from each other and their solutions are independent of each other.
Constructors
- OutOfMemoryError() :Constructs an OutOfMemoryError with no detail message.
- OutOfMemoryError(String s): Constructs an OutOfMemoryError with the specified detail message.
OutOfMemoryError: Java heap space Example
In the below example try to create java.lang.OutOfMemoryError by adding the name “Saurabh Gupta” in an infinite loop. It will add to the point as long as not throw java.lang.OutOfMemoryError.
package com.exceptions.errors; public class OutOfMemoryErrorExample { public static void main(String[] args) { StringBuffer str=new StringBuffer("FacingIssuesOnIt"); int i=0; while(i==0) { str.append("Saurabh Gupta"); System.out.println(i); } } }OutOfMemoryError StackTrace
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source) at java.lang.AbstractStringBuilder.append(Unknown Source) at java.lang.StringBuffer.append(Unknown Source) at com.exceptions.errors.OutOfMemoryErrorExample.main(OutOfMemoryErrorExample.java:10)In the above example, you see a small program can also create OutOfMemoryError because of just small wrong steps. as it’s having an infinite loop and adding tests continuously on the same variable. You will get in-depth knowledge of this in the below paragraphs.
Reason for java.lang.OutOfMemoryError: PermGen space
PermGen can happen in two ways:
Reason 1:
If you are familiar with different generations of heap and garbage collection process, new, old and permanent generation of heap space. PermGen means the Permanent Generation of the heap is used to store the String pool and various Metadata required by JVM related classes, method and other java primitives.
Most JVM default size of Perm Space is around “64MB” which can reach easily by having too many classes and a huge number of Strings in the application.
Point to Remember: Setting heap size by -Xmx no impact on OutOfMemory in perm space. To increase the size of perm space specify a size for permanent generation in JVM options as below.
“-XX: PermSize” and “-XX: MaxPermSize”
export JVM_ARGS=”-Xmx1024m -XX:MaxPermSize=256m”
Reason 2:
Another reason for “java.lang.OutOfMemoryError: PermGen” is memory leak through Classloaders. Generally, it’s happening in webserver and application servers like Glassfish, Weblogic, WebSphere or tomcat.In application server used different class loaders are used to load different applications so that deploy and un-deploy of one application without affecting of others application on the same server. But during un-deployment, if the container somehow keeps a reference of any class loaded by application class loader then that class and all related class will not get garbage collected and quickly fill permGen space if you deploy and un-deploy application many times.
Solutions to Resolve java.lang.OutOfMemoryError
Java.lang.OutOfMemoryError is a kind of error from JVM because of memory leak or objects are consuming memory but not releasing it. To identify the root cause of the problem required lots of investigation, like which object is taking memory, how much memory it is taking or finding the dreaded memory leak.
Solve java.lang.OutOfMemoryError: Java heap space
- An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options. For increasing heap size in JVM better option to set -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.
export JVM_ARGS=”-Xms1024m -Xmx1024m”
- If still getting OutOfMemoryError after applying the above solution. In this case, you can use a profile tool to investigate memory leak and heap dump. For example :
- Eclipse Memory Analyzer(MAT) to examine your heap dump.
- Profiler like Netbeans or JProbe.
This is a tough solution and requires some time to analyze and find memory leaks.
Solve java.lang.OutOfMemoryError: PermGen space
- Easy way to solve OutOfMemoryError: PermSpace is to increase the heap size of Perm space by using JVM option “-XX: MaxPermSize“. You can also specify the initial size of Perm space by using “-XX: PermSize” and keeping both initial and maximum Perm Space you can prevent some full garbage collection which may occur when Perm Space gets re-sized. For Example export JVM_ARGS=”-XX:PermSize=64M -XX:MaxPermSize=256m”
- If still getting OutOfMemoryError after applying the above solution. In this case, you can use a profile tool to investigate memory leak and heap dump. For example :
- Eclipse Memory Analyzer(MAT) to examine your heap dump.
- Profiler like Netbeans or JProbe.
This is a tough solution and requires some time to analyze and find memory leaks.
Solve OutOfMemoryError in PermGen Space In Tomcat
Tomcat 6.0 onward provides memory leak detection feature which can detect many common memory leaks on web-app perspective For Example:
- ThreadLocal memory leaks
- JDBC driver registration
- RMI targes
- LogFactory
- Thread spawned by web-apps etc.
You can check complete details on http://wiki.apache.org/tomcat/MemoryLeakProtection
Below are a couple of free tools available in java space used to analyze heap and culprits of OutOfMemoryError.
Tools to investigate java.lang.OutOfMemoryError
- Eclipse Memory Analyzer(MAT): It helps to analyze classloader leaks and memory leaks by analyzing the java heap dump. It also helps to the consumption of less memory and identify the exact suspect of memory leak.
- Visualgc (Visual Garbage Collector Monitoring Tool): Attach this tool to your instrument hot spot JVM. It visually displays all key data graphically including garbage collection, class loader, and JVM compiler performance.
- Jhat (Heap Analyzer Tool): After JDK-6 it’s part of a new version of JDK now. We can use that command-line utility to analyze heap dump in heap dump file by using “jmap”. When you execute the below command and point your browser to port 7000 then you can start analyzing objects present in the heap dump.
Command: jthat -J-Xmx256m heapdump
References
https://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html
Know More
To know more about Java Exception Hierarchy, in-built exception, checked exception, unchecked exceptions, and solutions. You can learn about Exception Handling in override methods and lots more. You can follow the below links:
You must log in to post a comment.