這是一個程式,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. |
使用這些簡單的記憶體訪問,可以得到記憶體的一些情況,我們通過建立一個大的某個類的數組,來查看記憶體用了多少,進而可以求得類的大小。程式原碼:/* * 建立日期 2007-4-19*/ public class Sizeof { public static void main(String[] args) throws Exception { // Warm up all classes/methods we will use runGC(); usedMemory(); // Array to keep strong references to allocated objects final int count = 100000; Object[] objects = new Object[count]; long heap1 = 0; // Allocate count+1 objects, discard the first one for (int i = -1; i < count; ++i) { Object object = null; /* Instantiate your data here and assign it to object*/ //object = new Object (); //object = new Integer (i); // ???? //object = new Long (i); //???? //object = new String (); //object = new Byte( (byte) 0); //???? //object = new Float( 0.2f); //???? //object = new Double( 0); //???? //object = new HelloWorld(); if (i >= 0) objects[i] = object; else { object = null; // Discard the warm up object runGC(); heap1 = usedMemory(); // Take a before heap snapshot } } runGC(); long heap2 = usedMemory(); // Take an after heap snapshot: final int size = (int) Math.round(((double) (heap2 - heap1)) / count); System.out.println("'before' heap: " + heap1 + ", 'after' heap: " + heap2); System.out .println("heap delta: " + (heap2 - heap1) + ", {" + objects[0].getClass() + "} size = " + size + " bytes"); for (int i = 0; i < count; ++i) objects[i] = null; objects = null; } private static void runGC() throws Exception { // It helps to call Runtime.gc() // using several method calls: for (int r = 0; r < 4; ++r) _runGC(); } private static void _runGC() throws Exception { long usedMem1 = usedMemory(), usedMem2 = Long.MAX_VALUE; for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++i) { s_runtime.runFinalization(); s_runtime.gc(); Thread.yield(); usedMem2 = usedMem1; usedMem1 = usedMemory(); } } private static long usedMemory() { return s_runtime.totalMemory() - s_runtime.freeMemory(); } private static final Runtime s_runtime = Runtime.getRuntime(); } // End of class 需要注意的是上面的一句: Object[] objects = new Object[count];只是分配了數組空間,沒有指派至的空間。數組中只有引用而已。 結論:下代碼測試基本對象時,得出的結果象下面:
Object類對象是 8位元組
Integer類對象是 16位元組
Long類對象是 16位元組
Byte類對象是 16位元組
怎麼會這樣呢???不解。 查看網上的文章:解釋如下: 這個例子寫的很好,正好說明了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個位元組根本沒有被使用.呵呵,仔細分析了一晚,還是有很多收穫的.