Java on-line application troubleshooting two: High memory footprint __java

Source: Internet
Author: User
Tags memory usage

The previous article introduced the online application of one of the troubleshooting: High CPU occupancy, this article mainly analyzes the high memory footprint troubleshooting.

Java developers often encounter the following two kinds of exceptions:

1, Java.lang.OutOfMemoryError:PermGen Space

2, Java.lang.OutOfMemoryError:Java heap space

To explain these two exceptions in detail, you need to simply revisit the Java memory model.

(Friendly tip: This blog article welcomes reprint, but please specify the source: Hankchen,

The Java memory model is a description of the relationships between variables (instance fields, static domains, and array elements) in a Java program, as well as low-level details such as storing variables in the actual computer system in memory and fetching variables from memory.

In the Java virtual machine, memory is divided into three generations: Cenozoic (New), Old generation(old), Permanent generation (Perm).

(1) New generation: New objects are stored here

(2) Old generation: storing objects migrating from Cenozoic new to longer life cycle. Cenozoic new and old generation old together constitute a heap of memory.

(3) Permanent generation Perm: is not a heap of memory components. The main store loaded classes class level objects such as class itself, Method,field, and so on.

If a Java.lang.OutOfMemoryError:Java heap space exception occurs, the Java virtual machine has insufficient heap memory. There are two reasons:

(1) The Java Virtual machine heap memory set is not enough, can pass parameter-xms,-xmx to adjust.

(2) A large number of large objects are created in the code and cannot be collected by the garbage collector for a long time (there is a reference).

If Java.lang.OutOfMemoryError:PermGen space appears, it means that the Java virtual machine does not have enough perm memory settings for the permanent generation.

This is generally the case when the program starts with a large number of Third-party jar packs that need to be loaded. For example: Too many applications were deployed under one tomcat.

From a code perspective, software developers are focused on Java.lang.OutOfMemoryError:Java heap space exceptions, reducing unnecessary object creation while avoiding memory leaks.

Now take a real example to analyze the troubleshooting of memory footprint.

The top command found that a Java process with a PID of 9004 had been consuming a higher memory release (24.7%) and a high memory footprint failure.

Remember one of the last online application troubleshooting: High CPU consumption of the introduction of the PS command, can find the specific which thread it.

PS-MP 9004-o Thread,tid,time,rss,size,%mem

Unfortunately, the PS command found the CPU footprint of the specific process , but could not find the memory footprint of a specific thread under a process .

Have to find another way, fortunately, Java provides a good memory monitoring tool: Jmap command

The Jmap command has several common uses:

Jmap [PID]

jmap-histo:live [PID] >a.log

jmap-dump:live,format=b, [PID]

Used up to the back two. where Jmap-histo:live [PID] can view the number of active objects created by the current Java process and the amount of memory consumed.

JMAP-DUMP:LIVE,FORMAT=B,FILE=XXX.XXX [PID] can export the memory footprint of the current Java process to facilitate analysis using specialized memory analysis tools such as: MAT.

This command is helpful for analyzing whether there is a memory leak. How to use to view another article in this blog: using Eclipse Memory Analyzer Tool (MAT) to analyze memory leaks

here is a detailed introduction to the jmap-histo:live [PID] Command:

As you can see from the diagram above, the array of int, Constmethodklass, Methodklass, Constantpoolklass all occupy a large amount of memory.

In particular, an array of int that consumes a large amount of memory requires careful examination of the relevant code.

Finally, summarize the methods and techniques for troubleshooting memory failures:

1, top command: Linux command. You can view real-time memory usage.

2, jmap-histo:live [PID], and then analyze the specific number of objects and memory size, so as to locate the code.

3, jmap-dump:live,format=b, [PID], and then use the Mat tool to analyze whether there is a memory leak and so on.

(Friendly tip: This blog article welcomes reprint, but please specify the source: Hankchen, )

Related Article

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