日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 基本数据类型 sizeof 功能

發布時間:2025/3/21 java 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 基本数据类型 sizeof 功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java基本數據類型
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基本數據類型大小
[java]?view plaincopy print?
  • ????private?static?void?calSize()?{??
  • ????????System.out.println("Integer:?"?+?Integer.SIZE/8);???????????//?4??
  • ????????System.out.println("Short:?"?+?Short.SIZE/8);???????????????//?2??????
  • ????????System.out.println("Long:?"?+?Long.SIZE/8);?????????????????//?8??
  • ????????System.out.println("Byte:?"?+?Byte.SIZE/8);?????????????????//?1??
  • ????????System.out.println("Character:?"?+?Character.SIZE/8);???????//?2??
  • ????????System.out.println("Float:?"?+?Float.SIZE/8);???????????????//?4??
  • ????????System.out.println("Double:?"?+?Double.SIZE/8);?????????????//?8??
  • //??????System.out.println("Boolean:?"?+?Boolean);??
  • ????}??

  • Java中模擬c中對sizeof的實現 思路:利用java中GC內存回收前后的heap size差別,得出每個object的大小


    這是一個程序,java中沒有現成的sizeof的實現,原因主要是java中的基本數據類型的大小都是固定的,所以看上去沒有必要用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.
    使用這些簡單的內存訪問,可以得到內存的一些情況,我們通過建立一個大的某個類的數組,來查看內存用了多少,進而可以求得類的大小。
    源碼:

    [java]?view plaincopy print?
  • ????private?static?void?calSize2()?{??
  • ????????runGC();??
  • ??
  • ????????long?heap1?=?0;??
  • ????????final?int?count?=?100000;??
  • ????????Object[]?objs?=?new?Object[count];??
  • ??
  • ????????for(int?i=-1;?i<count;?i++)?{??
  • ????????????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??
  • ??????????????
  • ??????????????
  • ????????????if(i<0){??
  • ????????????????obj?=?null;??
  • ????????????????runGC();??
  • ????????????????heap1?=?usedMemory();???//?before?memory?size??
  • ????????????}?else?{??
  • ????????????????objs[i]?=?obj;???
  • ????????????}??
  • ????????}??
  • ??
  • ????????runGC();??
  • ????????long?heap2?=?usedMemory();??????//?after?memory?size??
  • ??????????
  • ????????final?int?size?=?(int)Math.round(?(heap2?-?heap1)/(double)count?);??
  • ????????System.out.println("heap1?=?"?+?heap1?+?";?heap2?=?"?+?heap2);??
  • ????????System.out.println("heap2-heap1?=?"?+?(heap2?-?heap1)?+?";?"?+?objs[0].getClass().getSimpleName()?+?"?size?=?"?+?size);??
  • ??????????
  • ????????for(int?i=0;?i<count;?i++)?{??
  • ????????????objs[i]?=?null;??
  • ????????}??
  • ????????objs?=?null;??
  • ????????runGC();??
  • ????}??
  • ??????
  • ????private?static?void?runGC()?{??
  • ????????for(int?i=0;?i<4;?i++)?{??
  • ????????????long?usedMem1?=?usedMemory();??
  • ????????????long?usedMem2?=?Long.MAX_VALUE;??
  • ??????????????
  • ????????????for(int?j=0;?(usedMem1<usedMem2)?&&?(j<500);?j++)?{??
  • ????????????????rTime.runFinalization();??
  • ????????????????rTime.gc();??
  • ????????????????Thread.yield();??
  • ??????????????????
  • ????????????????usedMem2?=?usedMem1;??
  • ????????????????usedMem1?=?usedMemory();??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??????
  • ????private?static?long?usedMemory()?{??
  • ????????return?rTime.totalMemory()?-?rTime.freeMemory();??
  • ????}??

  • 注意: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 官方文檔)

    from:?http://blog.csdn.net/ithomer/article/details/7310008

    總結

    以上是生活随笔為你收集整理的Java 基本数据类型 sizeof 功能的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。