Java 基礎資料型別 (Elementary Data Type) sizeof 功能

來源:互聯網
上載者:User
Java基礎資料型別 (Elementary Data Type)
int     32bit
short   16bit
long    64bit
byte    8bitchar    16bit
float   32bit
double  64bitboolean 1bit,This data type represents one bit of information, but its "size" isn't something that's precisely defined.(ref)
Java基礎資料型別 (Elementary Data Type)大小
private static void calSize() {<br />System.out.println("Integer: " + Integer.SIZE/8);// 4<br />System.out.println("Short: " + Short.SIZE/8);// 2<br />System.out.println("Long: " + Long.SIZE/8);// 8<br />System.out.println("Byte: " + Byte.SIZE/8);// 1<br />System.out.println("Character: " + Character.SIZE/8);// 2<br />System.out.println("Float: " + Float.SIZE/8);// 4<br />System.out.println("Double: " + Double.SIZE/8);// 8<br />//System.out.println("Boolean: " + Boolean);<br />} Java中類比c中對sizeof的實現 思路:利用java中GC記憶體回收前後的heap size差別,得出每個object的大小

這是一個程式,java中沒有現成的sizeof的實現,原因主要是java中的基礎資料型別 (Elementary Data Type)的大小都是固定的,所以看上去沒有必要用sizeof這個關鍵字。實現的想法是這樣的:java.lang.Runtime類中有一些簡單的能涉及到記憶體管理的函數:Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
 long freeMemory()
Returns the amount of free memory in the Java Virtual Machine.
 void gc()
Runs the garbage collector.
static Runtime getRuntime()
Returns the runtime object associated with the current Java application.
 long maxMemory()
Returns the maximum amount of memory that the Java virtual machine will attempt to use.
 void runFinalization()
Runs the finalization methods of any objects pending finalization.
使用這些簡單的記憶體訪問,可以得到記憶體的一些情況,我們通過建立一個大的某個類的數組,來查看記憶體用了多少,進而可以求得類的大小。源碼:

private static void calSize2() {<br />runGC();</p><p>long heap1 = 0;<br />final int count = 100000;<br />Object[] objs = new Object[count];</p><p>for(int i=-1; i<count; i++) {<br />Object obj = null;<br />obj = new Object();// 8<br />//obj = new Integer( i );// 16<br />//obj = new Short( (short)i );// 16<br />//obj = new Long( i );// 16<br />//obj = new Byte( (byte)0 );// 16<br />//obj = new Character( (char)i );// 16<br />//obj = new Float( i );// 16<br />//obj = new Double( i );// 16<br />//obj = new Boolean( true );// 16<br />//obj = new String();// 40</p><p>if(i<0){<br />obj = null;<br />runGC();<br />heap1 = usedMemory();// before memory size<br />} else {<br />objs[i] = obj;<br />}<br />}</p><p>runGC();<br />long heap2 = usedMemory();// after memory size</p><p>final int size = (int)Math.round( (heap2 - heap1)/(double)count );<br />System.out.println("heap1 = " + heap1 + "; heap2 = " + heap2);<br />System.out.println("heap2-heap1 = " + (heap2 - heap1) + "; " + objs[0].getClass().getSimpleName() + " size = " + size);</p><p>for(int i=0; i<count; i++) {<br />objs[i] = null;<br />}<br />objs = null;<br />runGC();<br />}</p><p>private static void runGC() {<br />for(int i=0; i<4; i++) {<br />long usedMem1 = usedMemory();<br />long usedMem2 = Long.MAX_VALUE;</p><p>for(int j=0; (usedMem1<usedMem2) && (j<500); j++) {<br />rTime.runFinalization();<br />rTime.gc();<br />Thread.yield();</p><p>usedMem2 = usedMem1;<br />usedMem1 = usedMemory();<br />}<br />}<br />}</p><p>private static long usedMemory() {<br />return rTime.totalMemory() - rTime.freeMemory();<br />}

注意:Object[] objects = new Object[count];只是分配了數組空間,沒有指派至的空間。數組中只有引用而已。 結論:下代碼測試基本對象時,得出的結果象下面:   Object obj = null;
obj = new Object();// 8
obj = new Integer( i );// 16
obj = new Short( (short)i );// 16
obj = new Long( i );// 16
obj = new Byte( (byte)0 );// 16
obj = new Character( (char)i );// 16
obj = new Float( i );// 16
obj = new Double( i );// 16
obj = new Boolean( true );// 16
obj = new String();// 40
怎麼會這樣呢???解釋如下: 這個例子寫的很好,正好說明了java中基本類型封裝對象所佔記憶體的大小.   
1.簡單的Object對象要佔用8個位元組的記憶體空間,因為每個執行個體都至少必須包含一些最基本操作,比如:wait()/notify(),equals(),   hashCode()等   
2.使用Integer對象佔用了16個位元組,而int佔用4個位元組,說了封裝了之後記憶體消耗大了4倍   3.那麼Long看起來比Integer對象應該使用更多空間,結果Long所佔的空間也是16個位元組.   
那麼就正好說明了JVM的對於基本類型封裝對象的記憶體配置的規則是如下:   
Object所佔記憶體(8個位元組)+最大基本類型(long)所佔記憶體(8個位元組)   =   16位元組.   
JVM強制使用8個位元組作為邊界.   
所以所有基本類型封裝對象所佔記憶體的大小都是16位元組.但是還是有區別,比如:Integer對象雖然佔用了16個位元組的記憶體,但是只是利用了 Object所佔記憶體(8個位元組)+int所佔記憶體(4個位元組)   =   12位元組.還有4個位元組根本沒有被使用.呵呵,仔細分析了一晚,還是有很多收穫的測試源碼下載參考推薦:關於java中boolean佔用位元組的問題java中的 boolean 在記憶體中佔多少位元組?Primitive Data Types (SUN 官方文檔)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.