Java.lang.OutOfMemoryError and Solutions

Source: Internet
Author: User

There are 3 more common outofmemory Error types:

    • Java.lang.OutOfMemoryError:Java Heap Space
    • Java.lang.OutOfMemoryError:PermGen Space
    • Java.lang.OutOfMemoryError:GC Overhead limit exceeded
1. Java.lang.OutOfMemoryError:Java Heap Space

Objects created by the Java heap Space,java application are stored in this area, and garbage collection (garbage Collection) also occurs in this area. Typically, some of the more "heavy" operations can cause this exception, such as the need to create a large number of objects, a deeper level of recursion, and more.
There are two solutions, one is to optimize the application, find a place that consumes a lot of memory, and then optimize the code or algorithm. This approach is recommended, but the difficulty is relatively large, especially in the product environment, this problem occurs, developers can not be very good to reproduce the problem. The second option is to raise the Java heap size, which, while feeling a little palliative, is highly feasible and easy to operate.
For the general application, the following methods can be used (the number according to their own needs to adjust):

1-xms<size>-Set initial Java heap size2-xmx<size>-set maximum Java heap size3  4 java-xms512m-xmx10 24m Javaapp

If this problem occurs in Tomcat, the workaround is to find the following lines in {tomcat_dir}/bin/catalina.bat:

1 set MAINCLASS=ORG.APACHE.CATALINA.STARTUP.BOOTSTRAP2 set ACTION=START3 set security_policy_file=4 set DEBUG_OPTS=5 Set jpda=

Add a line to the back (the number adjusts to your needs):

1 set catalina_opts=-xms512m   -xmx512m
2. Java.lang.OutOfMemoryError:PermGen Space

Perm Gen Size (Permanent Generation size), used to store the definition (class definition) and metadata (metadata) of the class being loaded, such as Class object and method object. This is a permanently saved area in memory, and the garbage collection of the JVM does not touch the area. This exception usually occurs when a very large project is loaded.
For the general application, the following methods can be used (the number according to their own needs to adjust):

1-xx:permsize<size>-Set initial PermGen size.2-xx:maxpermsize<size>-set the maximum PermGen size.3  4 java-xx:permsize=64m-xx:maxpermsize=128m Javaapp

If this problem occurs in Tomcat, the workaround is to add the following line in {tomcat_dir}/bin/catalina.bat:

1 Set catalina_opts=-server-xms256m-xmx1024m-xx:permsize=512m-xx:maxpermsize=512m
3. Java.lang.OutOfMemoryError:GC Overhead limit exceeded

This error occurs in this scenario: the GC consumes more than 98% (the default) CPU time and only reclaims less than 2% (the default) of the heap space. The goal is to end the application and give developers the opportunity to diagnose the problem. This exception is typically caused by an application that creates a large number of temporary objects or weak reference objects on a limited amount of memory. While larger memory can temporarily solve this problem, it is strongly recommended to optimize the code, which is more efficient.
First, you can turn off the JVM as the default policy:

1 Java-xx:-usegcoverheadlimit Javaapp

Second, you can also try to increase the heap Size:

1 java-xmx512m Javaapp

Note: When modifying the contents of Tomcat's Catalina.bat (*.sh), there are a lot of things on the internet that are said to directly modify Java_opts, according to Apache official saying:
Note:do java_opts to specify memory limits. You don't need much memory for a small process, that's used to stop Tomcat. Those settings belong to catalina_opts.

    • Reference
      1. Http://wiki.apache.org/tomcat/OutOfMemory
      2. http://www.mkyong.com/java/find-out-your-java-heap-memory-size/

Java.lang.OutOfMemoryError and Solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.