import java.util.ArrayList; import java.util.List; public class StaticFieldClass { public static List<Team> staticField1 = new ArrayList<Team>(); static { staticField1.add(new Team()); staticField1.add(new Team()); } }
staticField1 字段引用Team的对象,Team对象确定不会被GC回收,可是这是为何?java
将代码跑起来,并将堆dump下来,借助MAT分析。
在Histogram视图找到Team实例:
code
而后 右键找到的Team对象-> List Objects -> With incoming references
对象
而后 右键找到的Team对象-> Path TO GC Roots -> exclude All Phantom...
ssl
不难看出,静态字段不是GC ROOT,GC ROOT是Thread...
Thread持有contextClassLoader,Classloader再持有静态字段...it
同时MAT还提供了直接查看GC ROOT的功能,咱们也能够顺着GC ROOT往下找到咱们的对象。
class