5回复
5年前
为什么内存中存在很多代码无关的int[]数组?
各位大佬,麻烦你们帮我看看,这个大对象区域为啥这么多的 int 数组?如下图
我点进去看了很久,没有发现有任何对象引用此类对象。同时我也仔细查了代码,并没有任何地方显示创建这些数组。代码简化之后如下:
public class MemTbmkBoot {
final static List<Method> mCache = new Vector<Method>();
public static void main(String[] args) throws Throwable {
final File f = new File("/Users/****/Documents/****/code/****/target/classes");
while (true) {
new Thread(new Runnable() {
@Override
public void run() {
try {
ClassLoader1 classLoader1 = new ClassLoader1(new URL[]{f.toURI().toURL()});
ClassLoader2 classLoader2 = new ClassLoader2(new URL[]{f.toURI().toURL()});
ClassLoader3 classLoader3 = new ClassLoader3(new URL[]{f.toURI().toURL()});
final Class<?> model1Clz = classLoader1.loadClass("demo.simulators.memtbmk.TbmkModel1");
final Class<?> model2Clz = classLoader2.loadClass("demo.simulators.memtbmk.TbmkModel2");
final Class<?> model3Clz = classLoader3.loadClass("demo.simulators.memtbmk.TbmkModel3");
final TbmkModel1 model1 = new TbmkModel1();
final TbmkModel2 model2 = new TbmkModel2();
final TbmkModel3 model3 = new TbmkModel3();
for (int i = 0; i < 1000; ++i) {
Method m = model1Clz.getMethod("method" + i);
m.invoke(model1);
mCache.add(m);
m = model2Clz.getMethod("method" + i);
m.invoke(model2);
mCache.add(m);
m = model3Clz.getMethod("method" + i);
m.invoke(model3);
mCache.add(m);
}
System.out.println("mCache size: " + mCache.size());
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
).start();
Thread.sleep(100);
}
}
}
关联了该问题的文章:
12909 阅读