我试过了答案
但是当我尝试下面的代码时,它给了我16代码
public class MemoryTest {
public static void main(String[] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new ArrayList<>().add(new DummyObject())));
}
}
Run Code Online (Sandbox Code Playgroud)
而16不是我想要的,所以我再次问这个问题.这个问题仍然是重复的问题吗?
我想检查对象的内存使用情况,这是我尝试过的.(java 8)
// Dummy Object class
public class DummyObject {
int dummy;
}
// Separate class to check memory
public class MemoryTest {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
long before = runtime.totalMemory() - runtime.freeMemory();
DummyObject obj = new DummyObject();
long after = runtime.totalMemory() - runtime.freeMemory();
System.out.println(after - before);
}
}
Run Code Online (Sandbox Code Playgroud)
结果是"0"
任何人都可以告诉我
1.为什么结果是0
2.什么是测量对象的内存使用的正确方法
提前致谢 …