java - Java中类的大小
问题描述
我已经阅读了这篇文章http://btoddb-java-sizing.blogspot.com/
作者解释了如何在 Java 中计算对象大小。例如:
import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;
public final class Perfomance {
static int number;
static int anotherNumber;
public static void main(String[] args) {
System.out.println(ObjectSizeCalculator.getObjectSize(new Integer(2)));
}
}
此代码将显示 16,因为新对象占用 12 个字节,而在 Integer 内部我们有 int(4 bytes)12+4 = 16.
但是考虑一下这段代码:
System.out.println(ObjectSizeCalculator.getObjectSize(Integer.class));
现在它的尺寸为 56 ,而且
System.out.println(ObjectSizeCalculator.getObjectSize(Object.class));
它的节目288。它是如何工作的。分配的内存如何取决于类?
解决方案
推荐阅读
- html - 如何使所有内容出现在下拉菜单中?
- python - 如何阅读动态参数气流运算符?
- node.js - 我如何捕获和处理错误:EEXIST:文件已存在,在nodejs中复制文件“somepath”“anotherpath”
- python - 使用 statsmodels 进行线性回归
- javascript - 无法从中获取 getTotalLength()
- python - py-spy:如何运行具有依赖项的脚本?
- c# - 当我打开使用 openXML 创建的 xlsx 文件时如何修复错误:“修复的记录:/xl/workbook.xml 中的工作表属性?
- javascript - 如何在函数中使用 keyup 函数?
- java - Play framework 2.7.* Java - HTTP Context Deprecated - Token Based Authentication
- php - in_array 语句的问题