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

Source: Internet
Author: User

Source of the article:http://outofmemory.cn/c/java-outOfMemoryError

Summary of common scenarios and processing methods for Java memory overflow (java.lang.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; the collection class has references to objects that are not emptied after use so that the JVM cannot be recycled;

There are dead loops or loops in the code that produce too many duplicate object entities; Bugs in the third-party software used; startup parameters too small to set the memory value;

Common error hints for this error:

1.tomcat:java.lang.outofmemoryerror:

2.PermGen Space Tomcat:

3.java.lang.outofmemoryerror:

4.Java Heap space weblogic:root cause of servletexception java.lang.OutOfMemoryError resin:

5.java.lang.outofmemoryerror 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 amount of memory that the JVM allows to consume when running Aa.class 256M.

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-xmx256m set 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. Create a Startup.bat file under the Bin folder,

The contents are as follows: @echo off call "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. Check to see if there are cycle duplicate generation of new object entities.

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 use.

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 refers to the permanent storage area of memory,

This memory is primarily stored by the JVM with class and meta information, and class is placed in PermGen space when it is loader, unlike the heap area where the class instance (Instance) is stored, GC (garbage Collection) PermGen space will not be cleaned up during the main program run time, so if you have many 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

In 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 hint: 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:maxpermsize=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 resin under Java.lang.OutOfMemoryError exception handling

Causes of memory overflow: This error occurs because the JVM's physical memory is too small. 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: Modify 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.

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.