9.4.1 Overview of the instance statistical functionality
The instance statistical functionality is used to check memory leaks in applications.
The instance statistical functionality investigates the reference relation of instances, such as relation with instances of class A->member variable of class A(the class is classB)->instances of classB->..., and recurrently adds the size of the instances without referencing to other instances in the class that has the instances as the member. In other words, the instance statistical functionality outputs the total size of the instances referenced by the class instances.
To check the cause of memory leak, execute the instance statistical functionality as follows, before and after the application processing for which you want to check the memory leak, then take the difference between the number of instances and the total size of the instances in points 1 and 3, check the amount of the increase in the numbers, and then identify the classes that are causing the memory leak.
-
Execute the instance statistical functionality.
-
Execute the application processing for which you want to check the memory leak.
-
Execute the instance statistical functionality.
Note that because the instance statistical functionality recursively adds the size of all the instances referenced by each class instance, you cannot check the size of only the instances of each class (the size excludes the size of the instances being referenced).
In the instance statistical functionality, when using the Explicit Memory Management functionality, the output result of the class-wise statistical information changes according to whether to set the instances of the Explicit heap as a target for statistics. Furthermore, specify the -explicit option in the jheapprof command, and then execute the same command to set the instances of an Explicit heap as a target for statistics.
The following figure shows an example of an instance structure that describes the output result of each statistical target.
|
When the statistical target contains an Explicit heap, the instances a1, b1, b2, c1, and c2 becomes the target for the class-wise statistical information. When the statistical target does not contains an Explicit heap, the instances a1, b2, and c2 becomes the target for the class-wise statistical information.
The following table lists the instance count and the total size of instances for each statistical target.
Arguments of the jheapprof command |
Statistical target |
Class A |
Class B |
Class C |
|||
---|---|---|---|---|---|---|---|
Instance count |
Total size# |
Instance count |
Total size# |
Instance count# |
Total size# |
||
-explicit |
|
1 |
122 |
2 |
22 |
2 |
2 |
-noexplicit |
|
1 |
111 |
1 |
11 |
1 |
1 |
- #
-
The total size indicates the total size of the instances. The unit is bytes.
The formula for calculating the total size of the instances of each class is as follows:
-
When statistical target contains an Explicit heap
-
In class A: a1+b1+b2+c1+c2
-
In class B: b1+b2+c1+c2
-
In class C: c1+c2
-
-
When statistical target does not contains an Explicit heap
-
In class A: a1+b2+c2
-
In class B: b2+c2
(Though the instance b1 is not targeted, the instance b2 and c2 under b1 are targeted, and therefore, the size of those instances that exist in the reference relation is added to the class B)
-
In class C: c2
-
In the instance statistical functionality, investigate for the reference relation of the object referred in the following order from the object that is the base. The base objects are the objects those are not investigated in other reference relation. 1., 2., and 3. indicates the priority order for investigating reference relation.
-
Ascending order of the address within the Java heap
-
Ascending order of the address within an Explicit management heap
When the reference destination objects are investigated, return up to the branch point, and investigate for the reference relation.
Moreover, when the reference destination objects are the objects that is the base of other reference relation, handle it as a reference destination object. Investigate for the reference relation until all the objects that are the base are lost.
When using instance statistical functionality, instance count of each class is output for number of instances. The following contents are output for the total size of instances:
-
The size of the object that is the base is added to the corresponding class. The size of the reference destination object is added to the corresponding class, and also added to the object that is the base and to their corresponding class of all objects that exist in the reference relation extended up to the corresponding class.