Java performance Optimization Combat record (3) analysis and cause tracing of--JVM oom

Source: Internet
Author: User
Tags constant definition

Objective:
C + + Programmers crave Java's freedom, and Java programmers expect the C + + constraints. Actually there is besieged, outside people want to come in, inside of people want to go out.

Background:
As a Java programmer, in addition to the convenience of the garbage collection mechanism, but also by the Oom (out of Memory) confusion and torture. This paper draws on << in-depth understanding of Java Virtual Machine >>, and combines the experience of small series and readers to face the plight of oom how to analyze and crack.

Preparatory work:
工欲善其事 its prerequisite, snapshot analysis of Java processes, is the reason that can help us locate errors quickly. Here we focus on memory analysis-related points.
Use the mat (memory Analyzer Tool) to analyze a snapshot of your memories.
Mat's:
http://www.eclipse.org/mat/downloads.php
Plugin URL for eclipse:
http://download.eclipse.org/mat/1.4/update-site/
Mat Display Effect:

Memory Snapshots:
How do I trigger a Java process to generate a memory snapshot?
1). Set the JVM parameters to allow the process to process memory dump under certain conditions.

-xx:+heapdumponoutofmemoryerror

Commentary: After setting the following virtual machine parameters, when the Java process is out of memory, it automatically dumps the memory so that the relevant person can analyze/interpret afterwards.
The program is very targeted, and in the crash point analysis can often locate the problem immediately. This parameter setting scheme is equivalent to the black box of the aircraft, recording all the information before the accident.
Of course, the biggest limitation of the scheme is that it cannot be analyzed at any time, how can it be broken?
2). Jmap tool to generate
Description of the use command for Jmap:

usage:jmap [option] <pid> (to connect to running process) jmap [option] <executable <core> (to con  Nect to a core file) jmap [option] [[E-mail protected]]<remote server IP or hostname> (to connect to remote debug Server) where <option> is one of:-dump:<dump-options> to dump Java heap in hprof binary format dump-optio Ns:livedump only Live objects;    If not specified, all objects in the heap is dumped. Format=b binary format file=<file> dump heap to <file> Example:jmap-dump:live,format=b,file=heap.bin < ;p id>

Commentary: Selected part of the option description, important is the rules of the dump
Use the command as follows:

Reasons for Oom Classification:
There are several factors that cause the program to be oom. Roughly, we can simply divide oom into heap overflow, stack overflow (stack), permanent overflow (Chang/method area), direct memory overflow.
First look at the memory distribution of Java
  1). Heap Overflow (heap)
Write the following routines:

public static void Main (string[] args) {list<byte[]> datas = new arraylist<byte[]> ();  while (true) {Datas.add (new byte[1024 * 1024]); }}

The virtual machine parameters are set at the same time, allowing the Java process to quickly generate heapdump.

-xms20m-xmx20m-xx:+heapdumponoutofmemoryerror

Commentary: Set the heap memory limit at 20m, Xms/xmx for the initial and maximum heap size of the heap, respectively.
The resulting exception information:

Mat for Memory analysis

Commentary: From this side can be seen, this side gathered a lot of objects, occupy 99% of the amount of memory.
  2). Stack Overflow (stack)
Stack overflow exception, or have a clear directivity. Temporary.
  3). Permanent generation Overflow (Chang/method area)
<< an in-depth understanding of Java Virtual Machines >> illustrates two examples of String.intern () and gclib generating dynamic proxy classes.
Its errors are also very directivity:

Exception in thread "main" Java.lang.OutOfMemoryError:PermGen space

Here is a little Mumuxinfei (a long time ago) to do a project.
It was a set-top box project (using the Ipanel browser, equivalent to a JVM virtual machine), using the J2ME specification (Java 1.1). But at that time the machine performance is not good, the memory is small, unlike now, some mobile device configuration starts to be like the PC to snap.
When I declare and define some of the initialization data in a class, the compiler is fine, but the JVM doesn't run at all, and it always goes wrong. But when I put this data in a file, I load it through the runtime, so that's fine. I was particularly puzzled at the time, not all memory? The same data, the same memory, so the difference is so big?
At that time I did not understand, then know the Java memory structure, I will understand. The loaded data is placed in the heap, and the defined constants are placed in the PermGen space . The original device limitations, Ipanel Browser Default permanent space size is relatively small, resulting in I just write some data to the constant definition, the run failed.
But the savage era, always passed, now the machine configuration, you will not encounter such a strange thing. Days in the day to get better, the past suffering of the day, now young, you will not understand ...
  4). Direct Memory Overflow
<< in-depth understanding of Java Virtual Machines >> describes Driectbytebuffer examples. Of course the most direct, is also the most easy to encounter, or the use of JNI, engaged in the development of Android Danale intentionally can encounter. This is a bit too small to say.

Postscript:
From a certain point of view, << in-depth understanding of the Java Virtual Machine >> has been the reason and example of oom is very specific, small make it difficult to go beyond it, do not want to simply copy the list of plagiarism. Simple description, right as a study note.

Java performance Optimization Combat record (3) analysis and cause tracing of--JVM oom

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.