分析java堆转储:为什么枚举实例保存在堆中?
我一直在我的应用程序中寻找内存泄漏,并发现 jhat 由于某种原因枚举实例被保留在堆中。
为了模拟它,我在 tomcat 中部署了我的应用程序,对其进行了一些处理,停止它并在 tomcat 上执行 GC(通过 jconsole)。然后进行了堆转储并发现了这一点:
instance of x.mgbean.quote.Input$Tab@0xe0887c0 (16 bytes)
Class:
class x.mgbean.quote.Input$Tab
Instance data members:
name (L) : VEHICLE (24 bytes)
ordinal (I) : 0
References to this object:
java.util.HashMap$Entry@0xe15f7c0 (24 bytes) : field value
class x.mgbean.quote.Input$Tab (84 bytes) : static field VEHICLE
[Lx.mgbean.quote.Input$Tab;@0xe0887f0 (20 bytes) : Element 0 of [Lx.mgbean.quote.Input$Tab;@0xe0887f0
[Lx.mgbean.quote.Input$Tab;@0xe1541e8 (20 bytes) : Element 0 of [Lx.mgbean.quote.Input$Tab;@0xe1541e8
知道这是怎么发生的吗?
I've been looking for memory leaks in my app and foud with jhat that for some reason instances of enumerations are kept in the heap.
To simulate it, I have deployed my app in tomcat, worked a bit with it, stopped it and performed GC on tomcat (through jconsole). Then took a heapdump and found this:
instance of x.mgbean.quote.Input$Tab@0xe0887c0 (16 bytes)
Class:
class x.mgbean.quote.Input$Tab
Instance data members:
name (L) : VEHICLE (24 bytes)
ordinal (I) : 0
References to this object:
java.util.HashMap$Entry@0xe15f7c0 (24 bytes) : field value
class x.mgbean.quote.Input$Tab (84 bytes) : static field VEHICLE
[Lx.mgbean.quote.Input$Tab;@0xe0887f0 (20 bytes) : Element 0 of [Lx.mgbean.quote.Input$Tab;@0xe0887f0
[Lx.mgbean.quote.Input$Tab;@0xe1541e8 (20 bytes) : Element 0 of [Lx.mgbean.quote.Input$Tab;@0xe1541e8
Any idea how this can happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么它们不在堆中?与所有其他对象一样,它们是类(枚举类)的实例。这并不意味着它们可以被垃圾收集。他们的类保存对每个实例的引用。
Why wouldn't they be in the heap? They're instances of a class (the enum class), as all other objects. That doesn't mean they could be garbage-collected. Their class holds a reference to each of the instances.
我相信枚举实例的处理方式与普通类的静态字段类似 - 也就是说,只要类被加载,它们就保持强可达性。这对于使用枚举带来的易用性是必要的。
I believe enum instances are treated along the lines of
static
fields for a normal class - that is, as long as the class is loaded they are kept strongly reachable. This is necessary for the ease of use that comes with using enums.