我的家園

          我的家園
          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中對sizeof的實現(xiàn)
          思路:利用java中GC內(nèi)存回收前后的heap size差別,得出每個object的大小


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

          源碼:


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

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

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




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



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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 温泉县| 巴彦淖尔市| 卢氏县| 秦安县| 长海县| 金山区| 新巴尔虎右旗| 镶黄旗| 尼玛县| 灵宝市| 石泉县| 乌鲁木齐市| 新巴尔虎右旗| 安岳县| 西乡县| 南通市| 来安县| 金门县| 东宁县| 蛟河市| 商城县| 江永县| 钦州市| 慈溪市| 板桥市| 达孜县| 星子县| 五常市| 阿克苏市| 子长县| 广饶县| 新安县| 宽甸| 定南县| 博客| 阳高县| 平乐县| 平山县| 泰和县| 阳谷县| 潢川县|