Java memory analysis (1) Basic Concepts

Source: Internet
Author: User
1. Heap dump

If you do not know what heap in Java means, this article may not be suitable for you to read.

A heap dump is a snapshot of the memory used by a Java Process at a certain time point. That is to say, at a certain time point, the memory of the Java Process is persisted to the disk in a certain format. There are many heap dump formats, and different formats may contain different information. But in general, heap dump generally contains basic information such as Java objects and class in a heap. At the same time, when you perform a dump operation, a GC is often triggered. Therefore, the information contained in the files you dump is usually valid (containing less information, or no spam objects)

We usually have the following basic information (one or more, related to the dump file format) in heap dump ):

  • All Object Information
    Class information, field information, native value (INT, long, etc.) and reference value of the object
  • All class information
    Classloader, class name, superclass, and static Fields
  • Root object of garbage collection
    Root objects are objects that can be directly touched by virtual machines.
  • Thread stack and local variables
    Contains the thread call stack information at the time of dumping and the local variable information in the stack frame.

A heap dump does not contain memory allocation information. That is to say, you cannot find out who created these objects and where they were created.

However, by analyzing the reference relationships between objects, we can often infer relevant information.

2. How to obtain heap dump files

The following describes three common methods to obtain heap dump.

When outofmemoryerror occurs

This is a very convenient parameter, because when you need to analyze the Java memory usage, it is often when OOM (outofmemoryerror) occurs. In your startup script, add the following parameters to the Java command to obtain the memory information:

Java-xmx1024m ....-XX: + heapdumponoutofmemoryerror......

Obtain heap dump interactively

To be honest, I have never used this method, but it looks very convenient. Similar to the preceding method, you can also add a Java command option. Then you can use the shortcut key Ctrl + break to obtain the current memory dump information.

Java-xmx1024m ....-XX: + heapdumponctrlbreak......

Obtain with tools

This is my commonly used method, through JDK comes some gadgets, you can refer to the http://blog.romebuilder.com/2011/06/393/

JPS and jmap are used here.

Get the Java Process PID through JPS, and then use jmap dump

Jmap-dump: format = B, file = heap. Bin PID

There are also basic GUI tools that can achieve similar results, such as jconsol and eclipse memory analyzer. You can find out for yourself, however, the methods mentioned above can already meet most application scenarios.

 

Subsequent articles will explain how to analyze the heap dump file......

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.