Lao Li case sharing: locating Java memory overflow

Source: Internet
Author: User

Poptest is the only training institute for developing Test and development engineers in China, aiming at the ability of the trainees to be competent in automated testing, performance testing and testing tools development. In Poptest's LoadRunner training, in order to improve the students ' performance optimization experience, added language and server optimization knowledge, for performance tuning ability to lay the foundation. (You are interested in the course, please add qq:564202718)

Best practices in the project:
1. The code code is implemented carefully. Develop internal Java coding specifications to keep project team members compliant.
2. Unit tests to overwrite all branch and boundary conditions.
3. Code review. After the code is finished, look for the Senior Program ape-daytime code.
4. Take advantage of the testers ' initiative.

Problem Tip:
Java.lang.OutOfMemoryError:Java Heap Space
Java.lang.OutOfMemoryError:PermGen Space

Workaround:
A.java.lang.outofmemoryerror:permgen Space:

PermGen space is the full name of permanent Generation space, refers to the memory of the permanent storage area, the memory is mainly stored by the JVM class and meta information, class is load will be placed in the PermGen space , which differs from 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 many classes in your application, you are likely to appear PermGen Space error.

By the above description, it can be concluded that if the class and jar files to be loaded are larger than-xx:maxpermsize, it is possible to generate java.lang.OutOfMemoryError:PermGen space.

The size of the-xx:maxpermsize is larger than the size of the class and jar. Usually-xx:maxpermsize is 1/8 of-XMX.

B.java.lang.outofmemoryerror:java Heap Space:

While various Java virtual machines are implemented differently, the heap space memory overflow is caused by the same reason: heap memory is not enough for virtual machine allocation.

The memory allocation mechanism is associated with GC and the GC does not release heap memory when there is insufficient memory allocated. Memory is not released because memory is still in use. The heap memory consumption generated by the Java object, as long as it is no longer referenced by the GC is able to be recovered smoothly
The key to the problem is finding out that when a heap space memory overflow occurs, too many objects in the heap memory can be the source of the problem. The exception is that the program does require so much memory that it should consider increasing the heap memory.

Accessibility tools:
The JDK comes with two visualizer to locate the problem:
Jdk/jconsole.exe Jdk/jvisualvm.exe

A.jconsole.exe can view the current state of the Java Virtual machine on both local and remote hosts, which is useful for server health checks. Such as:

B. Jvisualvm.exe can be used to view the analysis memory dump file, or it can be used as the current state of Java Virtual Machine view, but Jvisualvm.exe is very intrusive, once used can seriously affect application performance. Such as:

Write some code below to illustrate the generation of memory overflow, the generation of heap dump files, and the analysis of heap memory.

First create the data holding object class:

Package com.zas.jvm.om;

/**

* Data Objects

* @author ZAs

*/

public class DataObject {

Data Object ID

Private String ID;

Data Object Content

Private String des;

Public DataObject (String ID, string des) {

Super ();

This.id = ID;

This.des = des;

}

Public String getId () {

return ID;

}

public void SetId (String id) {

This.id = ID;

}

Public String Getdes () {

Return des;

}

public void Setdes (String des) {

This.des = des;

}

@Override

Public String toString () {

Return "DataObject [id=" + ID + ", des=" + des + "]";

}

/**

* @param args

*/

public static void Main (string[] args) {

}

}

Overflow Demo Code

Package com.zas.jvm.om;

Import java.util.ArrayList;

Import java.util.List;

public class Outmemerytest {

list<dataobject> list = new arraylist<dataobject> ();

public void Testom () {

for (int i = 0; i < 100000; i++) {

DataObject data = new DataObject ("id&" +i, "des:" +i);

List.add (data);

}

}

/**

* @param args

*/

public static void Main (string[] args) {

Outmemerytest OMT = new Outmemerytest ();

for (int i = 0; i < 2; i++) {

Omt.testom ();

}

System.out.println ("done!");

try {

Thread.Sleep (100000000);

} catch (Interruptedexception e) {

E.printstacktrace ();

}

}

}

The run parameters are set as follows:-xms64m-xmx64m-xx:permsize=8m-xx:maxpermsize=8m
-xx:-heapdumponoutofmemoryerror
See:

JVISUALVM Analysis:

It is obvious from the binding code that the object of the Com.zas.jvm.om.DataObject class is out of the question.
The above is a demonstration problem generation and positioning process, production environment problems of strange and specific problems need specific analysis.
When the heap memory is large, you may want to adjust the-xms-xmx size in the jdk\lib\visualvm\etc\visualvm.conf file to import the dump file.
The production environment for Linux is more, you can use the JDK's own jmap to dump the heap memory files to analyze.

Lao Li case sharing: locating Java memory overflow

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.