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);
}
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了
