支付寶一面:一個 Java 對象到底有多大?被問傻眼了!!
推薦閱讀
1、【地址已更新】8月26日最新免費ChatGPT,附視頻號下載方法 2、【私活必備】35款 SpringBoot/SpringCloud 開源項目,用來接私活掙錢真爽! 3、程序員段子:有哪些話一聽就知道對方很水!
1
2
-
Object Header -
Class Pointer -
Fields
hash(25)+age(4)+lock(3)=32bit
64位中:
unused(25+1)+hash(31)+age(4)+lock(3)=64bit
/**
* The value of the <code>Integer</code>.
*
* @serial
*/
private final int value;
4(object header)+4(pointer)+4(length)+4*10(10個int大小)=52byte 由于需要8位對齊,所以最終大小為`56byte`。
3
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
數(shù)組中是可以使用基本類型的,但是集合中只能放包裝類型!
-
時間用long/int表示,不用Date或者String。 -
短字符串如果能窮舉或者轉(zhuǎn)換成ascii表示,可以用long或者int表示。
4
性能和可讀性向來就有些矛盾,在這里也是,為了節(jié)約內(nèi)存,不得不進(jìn)行取舍,代碼丑陋了一些,可讀性差了一些,還好能省下一些內(nèi)存。上面的原則在確實需要節(jié)約內(nèi)存的時候,不妨可以試試!
來源:zhuanlan.zhihu.com/p/67609763

資源,怎么領(lǐng)取?
掃二維碼,加我微信,備注:編程合集
一定要備注:編程合集,不要急哦,工作忙完后就會通過!
!鏈接發(fā)夸克網(wǎng)盤!
評論
圖片
表情
