java如何获取对象在内存中的大小
public class T
{
public static void main(String[] args) throws Exception
{
// 创建 1000 个 String
String strA[] = new String[1000];
long start = 0;
long end = 0;
// 先垃圾回收
System.gc();
start = Runtime.getRuntime().freeMemory();
for (int i = 0; i < 1000; i )
strA[i] = new String();
// 快要计算的时,再清理一次
System.gc();
end = Runtime.getRuntime().freeMemory();
System.out.println("一个String对象占内存:" (start - end)/1000.0);
}
}
{
public static void main(String[] args) throws Exception
{
// 创建 1000 个 String
String strA[] = new String[1000];
long start = 0;
long end = 0;
// 先垃圾回收
System.gc();
start = Runtime.getRuntime().freeMemory();
for (int i = 0; i < 1000; i )
strA[i] = new String();
// 快要计算的时,再清理一次
System.gc();
end = Runtime.getRuntime().freeMemory();
System.out.println("一个String对象占内存:" (start - end)/1000.0);
}
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 如何查看一个java对象的大小
- 下一篇: 类对象可以直接赋值,但数据成员包含指针时 慎重