Analysing java heapdumps: why are enum instances kept in heap?
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:
inst开发者_如何学JAVAance 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?
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.
精彩评论