Java memory Analysis (2) Heap dump

Source: Internet
Author: User

Here, we use the memory analyzer tool of eclipse to achieve Intelligent Analysis convenience. Download and install the tool first.

We will equip the memory material below, starting from a simple one. Suppose there is a class that does not contain other information about the task. It is null:

public class Node{}
Then there is a mail function
public class Main{public static void main(String[] args){Node n = new Node();}}
Run the program and obtain a memory dump file as mentioned in the previous article. Run eclipse memory analyzer tool (MAT) and use it to open the dump file.

You will see the following content:

This page displays some summary information in the dump file, such as the memory usage of objects. Click these images to view more detailed reports.

But let's first take a look at the Memory organization diagram (in fact, there is no graph, more specifically a report style) and click the histogram button (in the red area ). Enter "Node" in the class name column and press enter to display the information of the class we just defined.

What does this information mean?

Objects refers to the number of objects of this type in memory.

Shallow heap refers to the amount of memory (in bytes) occupied by the object ). The reference of an object usually requires 32 or 64-bit (based on different platforms, and the value varies). The integer type occupies 4 bytes and the Length Integer is 8 bytes. These are defined in the JVM specification, but different JVM implementations can store data in their own way. In addition, these values may change based on different heap dump formats, but these values often reflect the memory usage more realistically.

Retained set refers to an object set. Assuming that some objects will be GC due to the garbage collection (GC) of object X, these objects belong to the retained set of object X. Note that the retained set contains x itself.

Retained heap refers to the total memory occupied by objects in the retained set of an object if the object is recycled.

Now let's take a look at the figure above. Cm. Demo. Mat. node has only one object, and the memory occupied is 16 bits (2 bytes). Its retained heap is16 (retained heap is not automatically calculated. You can right-click a retained item and choose calculate precise retained set). However, it should be noted that the actual value of shallow heap is related to the format of the dump file. At the same time, this value will be processed during the dump to better reflect the actual memory usage. But in any case, this value will always be a multiple of 8, that is, at least one byte.

We used the simplest object to describe some terms. Now let's try to add something to this empty class.

public class Node{private int fInt = 0;}

Add a Java primitive int type to check the changes:

 

Add a long integer:

public class Node{private int fInt = 0;private long fLong = 0L;}

It seems that each time we add a field, the node memory usage will increase by a byte.

Another way is to replace the int and long of primitive with Java. Lang. Integer and Java. Lang. Long. Will the result be the same ?! Try it!

public class NodeObj{private Integer fInt = 0;private Long fLong = 0L;}
public class Main{public static void main(String[] args){Node n = new Node();NodeObj no = new NodeObj();}}

Nodeobj's retained set is much larger than node's, but their shallow heap is the same. What are the elements in the retained set of nodeobj? [Select nodeobj-> right-click and choose show retained set]

It turns out that there are integer and long reasons.

Now, you should have some knowledge about the basic concepts and usage of mat. We will continue to explain the advanced usage of Mat later.

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