我的家園

          我的家園
          Java基本數(shù)據(jù)類型
          int     32bit

          short   16bit
          long    64bit
          byte    8bit
          char    16bit
          float   32bit
          double  64bit
          boolean 1bit,This data type represents one bit of information, but its "size" isn't something that's precisely defined.(ref

          Java基本數(shù)據(jù)類型大小

          Java中模擬c中對(duì)sizeof的實(shí)現(xiàn)
          思路:利用java中GC內(nèi)存回收前后的heap size差別,得出每個(gè)object的大小


          這是一個(gè)程序,java中沒有現(xiàn)成的sizeof的實(shí)現(xiàn),原因主要是java中的基本數(shù)據(jù)類型的大小都是固定的,所以看上去沒有必要用sizeof這個(gè)關(guān)鍵字。
          實(shí)現(xiàn)的想法是這樣的:java.lang.Runtime類中有一些簡(jiǎn)單的能涉及到內(nèi)存管理的函數(shù):
          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.
          使用這些簡(jiǎn)單的內(nèi)存訪問,可以得到內(nèi)存的一些情況,我們通過(guò)建立一個(gè)大的某個(gè)類的數(shù)組,來(lái)查看內(nèi)存用了多少,進(jìn)而可以求得類的大小。

          源碼:


          注意:Object[] objects = new Object[count];
          只是分配了數(shù)組空間,沒有分配對(duì)象的空間。數(shù)組中只有引用而已。
           
          結(jié)論:下代碼測(cè)試基本對(duì)象時(shí),得出的結(jié)果象下面:   
          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

          怎么會(huì)這樣呢???解釋如下:
           
          這個(gè)例子寫的很好,正好說(shuō)明了java中基本類型封裝對(duì)象所占內(nèi)存的大小.   
          1.簡(jiǎn)單的Object對(duì)象要占用8個(gè)字節(jié)的內(nèi)存空間,因?yàn)槊總€(gè)實(shí)例都至少必須包含一些最基本操作,比如:wait()/notify(),equals(),   hashCode()等   
          2.使用Integer對(duì)象占用了16個(gè)字節(jié),而int占用4個(gè)字節(jié),說(shuō)了封裝了之后內(nèi)存消耗大了4倍   

          3.那么Long看起來(lái)比Integer對(duì)象應(yīng)該使用更多空間,結(jié)果Long所占的空間也是16個(gè)字節(jié).   
          那么就正好說(shuō)明了JVM的對(duì)于基本類型封裝對(duì)象的內(nèi)存分配的規(guī)則是如下:   
          Object所占內(nèi)存(8個(gè)字節(jié))+最大基本類型(long)所占內(nèi)存(8個(gè)字節(jié))   =   16字節(jié).   
          JVM強(qiáng)制使用8個(gè)字節(jié)作為邊界.   
          所以所有基本類型封裝對(duì)象所占內(nèi)存的大小都是16字節(jié).
          但是還是有區(qū)別,比如:
          Integer對(duì)象雖然占用了16個(gè)字節(jié)的內(nèi)存,但是只是利用了 Object所占內(nèi)存(8個(gè)字節(jié))+int所占內(nèi)存(4個(gè)字節(jié))   =   12字節(jié).
          還有4個(gè)字節(jié)根本沒有被使用.呵呵,仔細(xì)分析了一晚,還是有很多收獲的




          參考推薦:
          Primitive Data Types (SUN 官方文檔)



          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 榆树市| 丘北县| 关岭| 广东省| 金川县| 会昌县| 全州县| 弋阳县| 元阳县| 青河县| 蒙阴县| 区。| 武清区| 丰宁| 麦盖提县| 清原| 民丰县| 柳林县| 洮南市| 霍邱县| 霍州市| 曲阳县| 广汉市| 电白县| 天镇县| 独山县| 乌海市| 建水县| 翁牛特旗| 桐梓县| 利津县| 丘北县| 西青区| 赤壁市| 收藏| 东乌珠穆沁旗| 阿坝县| 甘孜县| 安顺市| 比如县| 改则县|