Java Heap和Stack實驗

測試代碼:
java

public class TestHeapDumper {工具

//該屬性也從Heap上new出來測試

public Integer testInt= new Integer(1);spa

//測試方法對象

public static void main(String args[]) throws InterruptedException{ip

System.out.println("yes");文檔

//在Heap堆上,new一個對象出來get

TestHeapDumper testHeapDumper = new TestHeapDumper();it

//程序等待,不要退出io

Thread.sleep(1000000L);

//這裏再次使用到testHeapDumper,防止GC掉;

System.out.println(testHeapDumper.testInt);

}

}

p0爲dump文件的路徑,後綴名爲.hprof

而後再Memory Analyser工具中打開

能夠發如今TestHeapDumper實例中,有TestInt變量。

若是把testInt申明爲:

//測試是否可以在Heap堆上分配

public int testInt= 1;

能夠發現並無在Heap上分配。


Memory Analyser工具用起來很不錯,能夠經過outgoing reference 和incoming refence 引用來找到相互引用,如一張網; shallow Heap,Retained Heap表示對象本身大小和包括引用迭代出來的大小。具體能夠參見 Eclipse  Memory Analyser幫助文檔

參見:http://www.ibm.com/developerworks/cn/java/j-lo-langref/

有對StrongReference, SoftReference, WeakReference 以及 PhantomReference 的特性及用法

的講解。

相關文章
相關標籤/搜索