编程语言
首页 > 编程语言> > java – YourKit – 对象的保留大小不等于它引用的所有对象的保留大小

java – YourKit – 对象的保留大小不等于它引用的所有对象的保留大小

作者:互联网

对象的保留大小不等于其引用的所有对象的保留大小.

以下是发生的事情:

>使用YourKit捕获内存快照.
>点击一个对象&按类类型显示实例
>假设实例的保留内存是A字节(600mb)
>扩展并总结基础实例的保留大小,假设总和为B(300mb)

A>>乙

解决方法:

让我给你举个例子.

首先,您需要了解保留的大小.
从官方documentation

Retained size of an object is its shallow size plus the shallow sizes
of the objects that are accessible, directly or indirectly, only from
this object. In other words, the retained size represents the amount
of memory that will be freed by the garbage collector when this object
is collected.

简单来说,对象的保留大小确实是它所引用的对象的总和.在下图中,Obj1的保留大小是Obj1浅层大小和Obj2和Obj3保留大小的总和:
simple case

在更复杂的引用模型中不是这种情况.如果Obj6开始引用Obj5,则只能从Obj2访问Obj5.因此,Obj2的保留大小现在仅包括Obj4,并将排除Obj5.
retained size of Obj1 is not a sum of Obj2 and Obj3
Obj1的保留大小将保持不变.如果垃圾收集器释放Obj1,它将释放大小为41的整个引用图.
但是,如果垃圾收集器只释放Obj2,它将不会释放Obj5,因为它仍然会被Obj6引用.

标签:java,memory-management,performance-testing,yourkit
来源: https://codeday.me/bug/20190624/1278639.html