2 Scenarios and solutions for OutOfMemoryError in Java

Source: Internet
Author: User


Before solving the Java memory overflow problem, you need to have a certain understanding of the memory management of the JVM (Java Virtual machine). The memory that the JVM manages roughly includes three different types of memory areas: Permanent Generation Space (Permanent save area), heap space (heap area), Java Stacks (Java stack). Where the permanent save area mainly holds the class (class) and meta information, class is first loaded into the PermGen space area, the content of the class needs to be stored mainly including methods and static properties. The heap area is used to hold instances of class (that is, objects), and the content that the object needs to store is primarily non-static properties. Each time an object instance is created with new, the object instance is stored in the heap area, which is also managed by the JVM's garbage collection mechanism. The Java stack is similar to most programming languages, including assembly language stack functions, the main basic type variables and the input and output parameters of the method. There is a separate stack in each thread of the Java program. Memory spaces prone to memory overflow problems include: Permanent Generation space and heap space.

The first kind of outofmemoryerror:permgen space
The original intention of this problem is that the program uses a large number of jar or class, so that the Java Virtual machine load class space is not enough, and permanent Generation space. There are two ways to address this type of problem:
1. Increase the size of the xx:permsize and xx:maxpermsize parameters in the Java Virtual machine, where xx:permsize is the initial permanent save area size, Xx:maxpermsize is the maximum persistent area size. For tomcat6.0, add a line to the end of the list of environment variable names in the catalina.sh or Catalina.bat file (about 70 rows):
Java_opts= "-xx:permsize=64m-xx:maxpermsize=128m"
If you are a Windows server, you can also set it in the system environment variable. This memory overflow error can easily occur when using Tomcat to publish Sprint+struts+hibernate schema programs. Using the above method, I successfully solved the problem of frequent outages of the Tomcat server that deployed the SSH project.
2. Clean the jar under Web-inf/lib in the application, if Tomcat deploys multiple applications, many applications use the same jar and can move the common jar to the Tomcat common Lib, reducing the class's repeated loading. This method is recommended by some people on the Internet, I have not tried, but feel less space, the most reliable is the first method.

The second type of OutOfMemoryError:Java HeapSpace
This problem occurs because there are too many objects created by the Java Virtual machine, and the virtual machine allocates enough memory space to the heap for garbage collection, which is related to heap space. There are two ways to solve this kind of problem:
1. Check the program to see if there are dead loops or unnecessarily repeatedly creating a large number of objects. After you find the cause, modify the program and algorithm.
I used to write a K-means text clustering algorithm for tens of thousands of of text records (the eigenvector of each record about 10 or so) for text clustering, due to procedural details of the problem, led to the Java heap space memory overflow problem, later through the modification program was resolved.
2. Increase the size of the XMS (initial heap size) and XMX (maximum heap size) parameters in the Java Virtual machine. such as: Set java_opts=-xms256m-xmx1024
M

2 Scenarios and workarounds for OutOfMemoryError in Java

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.