Summary of common scenarios and processing methods for Java memory overflow (java.lang.OutOfMemoryError)

Source: Internet
Author: User

From:http://outofmemory.cn/c/java-outofmemoryerror

Java.lang.OutOfMemoryError This error I believe most developers have encountered, the cause of this error is mostly due to the following reasons: The JVM memory is too small, the program is not tight, resulting in excessive garbage.

Common causes of outofmemoryerror abnormalities include the following:

    1. The amount of data loaded in memory is too large, such as fetching too much data from the database at once;
    2. The collection class has references to objects that are not emptied after use, making the JVM not recyclable;
    3. There is a dead loop or loop in the code that produces too many duplicate object entities;
    4. Bugs in the third-party software used;
    5. Boot parameter memory value setting is too small;

Common error hints for this error:

    1. Tomcat:java.lang.OutOfMemoryError:PermGen Space
    2. Tomcat:java.lang.OutOfMemoryError:Java Heap Space
    3. Weblogic:root cause of Servletexception java.lang.OutOfMemoryError
    4. Resin:java.lang.OutOfMemoryError
    5. Java:java.lang.OutOfMemoryError

There are several ways to solve Java.lang.OutOfMemoryError:

First, increase the size of the JVM's memory. Methods are: 1) when executing a class file, you can use the java-xmx256m aa.class to set the maximum memory allowed by the JVM to be 256M when running Aa.class. 2) on the Tomcat container, you can set the memory limit on the JVM at startup. For Tomcat, you can add it in Catalina.bat:

Set Catalina_opts=-xms128m-xmx256mset java_opts=-xms128m-xmx256m

or replace%catalina_opts% and%java_opts% for-xms128m-xmx256m.

3) for resin containers, you can also set memory limits on the JVM at startup. Under the Bin folder, create a Startup.bat file that reads as follows:

@echo Offcall "Httpd.exe"  "-xms128m" "-xmx256m": End

where "-xms128m" is the minimum memory, "-xmx256m" is the maximum memory.

Second, optimize the program, release garbage.

Mainly including avoiding the cycle of death, should be released in a timely manner of resources: memory, database of various connections, to prevent the loading of too much data at once. The root cause of Java.lang.OutOfMemoryError is that the program is not robust. Therefore, the only way to fundamentally solve the Java memory overflow is to modify the program, releasing the unused objects in a timely manner and freeing up the memory space. Encounter this error when you want to carefully check the program, Hey, if you encounter this problem more than once, will be more careful to write procedures.

Java code causes OutOfMemoryError errors to be resolved:

The following points need to be highlighted:

    1. Check the code for a dead loop or recursive call.
    2. Checks whether there are cycle duplicates that produce the new object entity.
    3. Check to see if there is a single query for all data in the database query. In general, if you take 100,000 records to memory at a time, you can cause a memory overflow. This problem is more covert, before the online, the database less data, not prone to problems, on-line, the database more data, a query may cause memory overflow. Therefore, query the database query as much as possible by paging.
    4. Check that the list, map, and other collection objects are not cleared after they have been used. Collection objects such as List, map, and so on will always have a reference to the object so that they cannot be reclaimed by GC.
Java.lang.OutOfMemoryError:PermGen space exception handling in Tomcat

The full name of PermGen space is permanent Generation space, which is the permanent storage area of memory, which is primarily stored by the JVM with class and meta information, Class is placed in PermGen space when it is loader, unlike the heap area where the class instance (Instance) is stored, the GC (garbage Collection) does not clean up permgen space during the main program run time. So if you have a lot of classes in your application, you are likely to have PermGen space errors, which are common when the Web server pre-compile the JSP. If you have a large number of third-party jars under your web app that are larger than the JVM's default size (4M), this error message will be generated. Workaround: Manually set the MaxPermSize size modification tomcat_home/bin/catalina.sh

echo "Using catalina_base:   $CATALINA _base"

Add the following line above:

Java_opts= "-server-xx:permsize=64m-xx:maxpermsize=128m

Recommendation: Move the same third-party jar files to the Tomcat/shared/lib directory, which can reduce the memory consumption of the jar document.

Java.lang.OutOfMemoryError exception Handling in WebLogic

Error tip: Root cause of ervletException java.lang.OutOfMemoryError Workaround: Adjust parameters in Commenv in Bea/weblogic/common

    : Sun if "%production_mode%" = = "true" goto Sun_prod_mode set java_vm=-client set Mem_args=-xms256m-xmx512m-xx:maxperm size=256m set java_options=%java_options%-xverify:none goto Continue:sun_prod_mode Set Java_vm=-server set MEM_ARGS =-xms256m-xmx512m-xx:maxpermsize=256m Goto Continue
Java.lang.OutOfMemoryError Exception handling under resin

Causes of memory overflow:

This error usually occurs because the JVM is too small in physical memory. The default Java virtual machine has a maximum memory of only 64 trillion, which may not be a problem during the development and debugging process, but is far from sufficient in the actual application environment, unless your application is very small and has little traffic. Otherwise you may find that the program is running for a period of time after the package Java.lang.OutOfMemoryError error. So we need to increase the size of the virtual machine memory available to resin.

Workaround:

Modifying the args option in/usr/local/resin/bin/httpd.sh to add parameters -Xms (initial memory) and -Xmx (maximum memory size) can be used to limit the amount of physical memory used by the JVM. For example:

Args= "-xms128m-xmx256m"

Once set, the JVM's initial physical memory is 128m and the maximum physical memory is 256m.

These two values should be set by the system administrator based on the actual situation of the server.

Summary of common scenarios and processing methods for Java memory overflow (java.lang.OutOfMemoryError)

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.