java boolean占用內(nèi)存大小說明
1、如果boolean是單獨使用:boolean占4個字節(jié)。
2、如果boolean是以boolean數(shù)組形式使用:boolean占1個字節(jié)
解釋1、JVM沒有提供boolean類型專用的字節(jié)指令,而是使用int相關(guān)指令來代替。
2、對boolean數(shù)組的訪問與修改,會共用byte數(shù)組的baload和bastore指令。
分析結(jié)論上面的第一個結(jié)論是說:boolean在底層實際調(diào)用int,那么既然int占4個字節(jié),boolean頁自然占4個字節(jié)。即 boolean類型占4個字節(jié)。
上面的第2個結(jié)論是說:boolean數(shù)組在底層會用byte指令,那么既然byte占1個字節(jié),boolean數(shù)組中的boolean也就占1個字節(jié),即,boolean數(shù)組中的boolean占1個字節(jié)。
擴展1、因此,大多數(shù)對于boolean,byte,char和short類型數(shù)據(jù)的操作,實際都提升int,并使用int做為運算類型,所以他們占4個字節(jié),實際上,虛擬機規(guī)范也只有4字節(jié)和8字節(jié)類型(long,float),boolean,char,short都是占了4字節(jié)。
2、對于在棧上(局部變量)的byte,char,short類型的數(shù)據(jù),在內(nèi)存中的確會占4字節(jié),但這對于(數(shù)組)對象來說并不適用。
java各種類型對象占用內(nèi)存情況分析經(jīng)典篇,有圖有真相
為什么寫這篇文章?
其實一般的程序猿根本不用了解這么深,只有當(dāng)你到了一定層次,需要了解jvm內(nèi)部運行機制,或者高并發(fā)多線程下,你寫的代碼對內(nèi)存有影響,你想做性能優(yōu)化。。。等等等等,一句話,當(dāng)你想深入了解java對象在內(nèi)存中,如何存儲,或者每個對象占用多大空間時,你會感謝這篇文章
本文主要分析jvm中的情況,實驗環(huán)境為64位window10系統(tǒng)、JDK1.8,使用JProfiler進行結(jié)論驗證
很多描述以及 概念是基于你懂基本java知識的,如果你看起來有點吃力,要加油咯
基本數(shù)據(jù)類型占用類型 占用空間 boolean、byte 1byte short、char 2byte int、float 4byte long、double 8byte接下來用JProfiler驗證:
新建一個空對象,觀察空對象內(nèi)存占用
public class TestObject {}
對象占用內(nèi)存 16b,如圖
結(jié)論:一般自建空對象占用內(nèi)存 16b,16 = 12 + 4
在TestObj中新增一個 int 屬性,觀察對象內(nèi)存占用
public class TestObj { private int i;}
對象占用內(nèi)存 16b,如圖
結(jié)論:int 占用 4b, 4 = 16 -12
在TestObj中新增一個 long 屬性,觀察對象內(nèi)存占用
public class TestObj { private long i;}
對象占用內(nèi)存 24b,如圖
結(jié)論:long 占用 8b, 8 = 24 -12 - 4
其余基本類型可以參照以上自行驗證,原理一樣
包裝類型占用包裝類(Boolean/Byte/Short/Character/Integer/Long/Double/Float)占用內(nèi)存的大小等于對象頭大小加上底層基礎(chǔ)數(shù)據(jù)類型的大小。
類型占用空間Boolean、Byte16byteShort、Char16byteInteger、Float16byteLong、Double24byte 在TestObj中新增一個 Integer 屬性,觀察對象內(nèi)存占用
類型 占用空間 Boolean、Byte 16byte Short、Char 16byte Integer、Float 16byte Long、Double 24bytepublic class TestObj { private Integer i =128;}
對象占用內(nèi)存 32b,如圖
結(jié)論:Integer 占用 16b, 16 = 32 - 16
特別的:-128~127 之間的封裝類型,只占用 4b**
在TestObj中新增一個 Long 屬性,觀察對象內(nèi)存占用
public class TestObj { private Long l = new Long(1);}
對象占用內(nèi)存 40b,如圖
結(jié)論:Long 占用 24b, 16 = 40 - 16
其余包裝類型可以參照以上自行驗證,原理一樣
基本類型數(shù)組占用64位機器上,數(shù)組對象的對象頭占用24 bytes,啟用壓縮后占用16字節(jié)。比普通對象占用內(nèi)存多是因為需要額外的空間存儲數(shù)組的長度(普通16b-12b)。
對象數(shù)組本身的大小=數(shù)組對象頭 + length * 存放單個元素大小
在TestObj中新增一個 char[] 屬性,觀察對象內(nèi)存占用
public class TestObj { private char[] c = {’a’,’b’,’c’};}
對象占用內(nèi)存 40b,如圖
結(jié)論:char[3] 占用 24b, 24 = 40 - 16,24 = 16 + 3 * 2 + 2
封裝類型數(shù)組占用封裝類型數(shù)組比基本類型的數(shù)組,需要多管理元素的引用
對象數(shù)組本身的大小=數(shù)組對象頭+length 引用指針大小 + length 存放單個元素大小
在TestObj中新增一個 Integer[] 屬性,觀察對象內(nèi)存占用
public class TestObj { private Integer[] i = {128,129,130};}
對象占用內(nèi)存 80b,如圖
結(jié)論:Integer[3] 占用 80b, 80 = 96 - 16 , 80 = 16 + 3 4 + 3 16 +4
String占用內(nèi)存 在TestObj中新增一個空 String 屬性,觀察對象內(nèi)存占用
public class TestObj { private String s = new String('');}
對象占用內(nèi)存 40b,如圖
結(jié)論:String 本身占用 24b, 24 = 40 -16,另外,String的屬性value還需要 16b,也就是說空””也需要16b
注意:這里為什么要寫String s = new String(“”)?請自己思考,不寫會怎么樣?
答:如果寫成String s = “”,是不會再堆中開辟內(nèi)存的,也就看不到String占用的空間,你看到的將會是下面的,至于為什么,都是因為final
這些參考文章開頭提到的那篇文章,下面給出計算公式:
一個ArrayList實例本身的的大小為:
12(header) + 4(modCount) + 4(size) + 4(elementData reference) = 24 (bytes)
下面分析一個只有一個Integer(1)元素的ArrayList實例占用的內(nèi)存大小。
ArrayList<Integer> testList = Lists.newArrayList();testList.add(1);
根據(jù)上面對ArrayList原理的介紹,當(dāng)調(diào)用add方法時,ArrayList會初始化一個默認大小為10的數(shù)組,而數(shù)組中
保存的Integer(1)實例大小為16 bytes。
則testList占用的內(nèi)存大小為:
24(ArrayList itselft) + 16(elementData array header) + 10 * 4(elemetData reference) + 16(Integer) = 96 (bytes)
JProfiler中的結(jié)果驗證了上述分析:
2. HashMap內(nèi)存占用
這里分析一個只有一組鍵值對的HashMap, 結(jié)構(gòu)如下:
Map<Integer, Integer> testMap = Maps.newHashMap();testMap.put(1, 2);
首先分析HashMap本身的大小。HashMap對象擁有的屬性包括:
/** * The table, initialized on first use, and resized as * necessary. When allocated, length is always a power of two. * (We also tolerate length zero in some operations to allow * bootstrapping mechanics that are currently not needed.) */ transient Node<K,V>[] table; /** * Holds cached entrySet(). Note that AbstractMap fields are used * for keySet() and values(). */ transient Set<Map.Entry<K,V>> entrySet; /** * The number of key-value mappings contained in this map. */ transient int size; /** * The number of times this HashMap has been structurally modified * Structural modifications are those that change the number of mappings in * the HashMap or otherwise modify its internal structure (e.g., * rehash). This field is used to make iterators on Collection-views of * the HashMap fail-fast. (See ConcurrentModificationException). */ transient int modCount; /** * The next size value at which to resize (capacity * load factor). * * @serial */ // (The javadoc description is true upon serialization. // Additionally, if the table array has not been allocated, this // field holds the initial array capacity, or zero signifying // DEFAULT_INITIAL_CAPACITY.) int threshold; /** * The load factor for the hash table. * * @serial */ final float loadFactor;
HashMap繼承了AbstractMap<K,V>, AbstractMap有兩個屬性:
transient Set<K>keySet; transient Collection<V> values;
所以一個HashMap對象本身的大小為:
12(header) + 4(table reference) + 4(entrySet reference) + 4(size) + 4(modCount) + 4(threshold) + 8(loadFactor) + 4(keySet reference) + 4(values reference) = 48(bytes)
接著分析testMap實例在總共占用的內(nèi)存大小。
根據(jù)上面對HashMap原理的介紹,可知每對鍵值對對應(yīng)一個Node對象。根據(jù)上面的Node的數(shù)據(jù)結(jié)構(gòu),一個Node對象的大小為:
12(header) + 4(hash reference) + 4(key reference) + 4(value reference)+ 4(next pointer reference) = 28 (padding) -> 32(bytes)
加上Key和Value兩個Integer對象,一個Node占用內(nèi)存總大小為:32 + 2 * 16 = 64(bytes)
JProfiler中結(jié)果:
下面分析HashMap的Node數(shù)組的大小。
根據(jù)上面HashMap的原理可知,在不指定容量大小的情況下,HashMap初始容量為16,所以testMap的Node[]占用的內(nèi)存大小為:
16(header) + 16 * 4(Node reference) + 64(Node) = 144(bytes)
JProfile結(jié)果:
所以,testMap占用的內(nèi)存總大小為:
48(map itself) + 144(Node[]) = 192(bytes)
JProfile結(jié)果:
這里只用一個例子說明如何對HashMap進行占用內(nèi)存大小的計算,根據(jù)HashMap初始化容量的大小,以及擴容的影響,HashMap占用內(nèi)存大小要進行具體分析,不過思路都是一致的。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
