对象大小如何计算
对象大小包括俩部分的内容,对象头和对象内容。(图片源于网络)
 
对象头
此处假设是64位的JVM
- 对象地址,占4个字节。
- 对象标记,占8个字节,包括锁标记,hashcode, age 等。
- 数组长度标记,占4哥字节。如果对象是一个数组,会有此标记,否则没有。
对象内容
- 对象内部属性。如果属性是对象的话,那么记录的是对象的地址,占用4个字节。
8字节对齐
- Java对象采用的是8字节对齐。对象大小必须是8的倍数,不足需要补齐。比如,计算一个对象只需要20字节,那么实际占用24字节。
计算对象大小
- 引入 如下的 pom文件
 <dependency>
         <groupId>org.apache.lucene</groupId>
         <artifactId>lucene-core</artifactId>
         <version>8.7.0</version>
 </dependency>
- 测试
 Integer i = new Integer(11);
 System.out.println(RamUsageEstimator.shallowSizeOf(i));
 
 output:
 16
 Map<String, String> map = new HashMap<>();
 System.out.println(RamUsageEstimator.shallowSizeOf(map));
 
 output:
 48
分析
Integer 对象大小分析
- 它是对象,占用4个字节
- 对象标记,占用8个字节
- 查看源码,发现Integr 内容只有以下一个非static的属性,是一个int的基本类型属性,占用4个字节.static 修饰的方法属性都是存储在方法区的,不占用对象空间。
    /**
     * The value of the {@code Integer}.
     *
     * @serial
     */
    private final int value;
- 故 total = 4 + 8 + 4 = 16
HashMap 对象大小分析
- 它是对象,占用4个字节
- 对象标记,占用8个字节
- 查看源码,HashMap 是继承了 AbstractMap 的,AbstractMap 中有以下的俩个属性,一共占用8个字节。因为只是存储了keySet, values 的地址
transient Set<K>        keySet;
transient Collection<V> values;
- HashMap 中有以下属性,共占用 6 * 4 = 24 个字节。
transient Node<K,V>[] table;
transient Set<Map.Entry<K,V>> entrySet;
transient int size;
transient int modCount;
int threshold;
final float loadFactor;
- total = 4 + 8 + 8 + 24 = 44, 由于java 是8字节对齐的,故一共是 48 字节。

![[oeasy]python0040_换行与回车的不同_通用换行符_universal_newlines](https://img-blog.csdnimg.cn/img_convert/eeb38b6ad7f6d57de559c2304bfbb0cf.png)
















![[多图,秒懂]如何训练一个“万亿大模型”?](https://img-blog.csdnimg.cn/img_convert/9227e1d92b838e14d6a10e28a20a01b9.png)
![[golang工作日记] for range 踩坑](https://img-blog.csdnimg.cn/59075971b33047fe8a7b6990f15e2d5b.png)