Java 基本数据类型 sizeof 功能
来源 https://blog.csdn.net/ithomer/article/details/7310008
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)
根据http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html官方文档的描述:
boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.
布尔类型:布尔数据类型只有两个可能的值:真和假。使用此数据类型为跟踪真/假条件的简单标记。这种数据类型就表示这一点信息,但是它的“大小”并不是精确定义的。
可以看出,boolean类型没有给出精确的定义,《Java虚拟机规范》给出了4个字节,和boolean数组1个字节的定义,具体还要看虚拟机实现是否按照规范来,所以1个字节、4个字节都是有可能的。这其实是运算效率和存储空间之间的博弈,两者都非常的重要。
基础类型和包装类型的对应
包装类型 基本类型
Integer int
Short short
Long long
Byte byte
Character char
Float float
Double double
Boolean boolean
Java基本数据类型大小
public class Calcsizeof { private void calcSize() { System.out.println("Integer:" + Integer.SIZE/8); // Integer:4 System.out.println("Short:" + Short.SIZE/8); // Short:2 System.out.println("Long:" + Long.SIZE/8); // Long:8 System.out.println("Byte:" + Byte.SIZE/8); // Byte:1 System.out.println("Character:" + Character.SIZE/8); // Character:2 System.out.println("Float:" + Float.SIZE/8); // Float:4 System.out.println("Double:" + Double.SIZE/8); // Double:8 // System.out.println("Boolean:" + Boolean.toString(false)); } public static void main(String[] args) { Calcsizeof calcsizeof = new Calcsizeof(); calcsizeof.calcSize(); } }
Java中模拟c中对sizeof的实现
思路:利用java中GC内存回收前后的heap size差别,得出每个object的大小
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. |
源码:
public class Calcsizeof { private static Runtime rTime = Runtime.getRuntime(); private static long usedMemory() { return rTime.totalMemory() - rTime.freeMemory(); } 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 void calcSize2() { 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(); // Object size = 8 // ? 16 // obj = new Integer(i); // Object size = 16 // ? 16 // obj = new Short((short)i); // Object size = 16 // ? 16 // obj = new Long(i); // Object size = 16 // ? 24 // obj = new Byte((byte)0); // Object size = 16 // ? 16 // obj = new Character((char)i); // Object size = 16 // ? 16 // obj = new Float(i); // Object size = 16 // ? 16 // obj = new Double(i); // Object size = 16 // ? 24 // obj = new Boolean(false); // Object size = 16 // ? 16 // obj = new String(); // Object size = 40 // ? 24 if (i < 0) { obj = null; runGC(); heap1 = usedMemory(); } else { objs[i] = obj; } } runGC(); long heap2 = usedMemory(); 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 void calcSize() { System.out.println("Integer:" + Integer.SIZE/8); System.out.println("Short:" + Short.SIZE/8); System.out.println("Long:" + Long.SIZE/8); System.out.println("Byte:" + Byte.SIZE/8); System.out.println("Character:" + Character.SIZE/8); System.out.println("Float:" + Float.SIZE/8); System.out.println("Double:" + Double.SIZE/8); // System.out.println("Boolean:" + Boolean.toString(false)); } public static void main(String[] args) { Calcsizeof calcsizeof = new Calcsizeof(); // calcsizeof.calcSize(); calcsizeof.calcSize2(); } }
控制台输出内容:
>java -cp outproductionTest Calcsizeof heap1 = 1099096; heap2 = 2700368 heap2 - heap1 = 1601272; Integer size = 16
注意:Object[] objects = new Object[count];
只是分配了数组空间,没有分配对象的空间。数组中只有引用而已。
结论:下代码测试基本对象时,得出的结果象下面:
Object obj = null; // obj = new Object(); // Object size = 8 // obj = new Integer(i); // Object size = 16 // obj = new Short(i); // Object size = 16 // obj = new Long(i); // Object size = 16 // obj = new Byte(i); // Object size = 16 // obj = new Character(i); // Object size = 16 // obj = new Float(i); // Object size = 16 // obj = new Double(i); // Object size = 16 // obj = new Boolean(false); // Object size = 16 // obj = new String(); // Object size = 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个字节根本没有被使用.呵呵,仔细分析了一晚,还是有很多收获的
====================== End