看板 java 關於我們 聯絡資訊
建議你養成好習慣 method 名稱開頭都小寫 只有 class 名稱才開頭大寫 這樣看起來真的比較順 ※ 引述《icetofux ()》之銘言: : ※ 引述《kaneson (Lance)》之銘言: : : private void Insert( Node nowNode, int data ) { : : if ( data < nowNode.data ) : : if ( nowNode.left == null ) : : nowNode.left = new Node( data ) ; : ^^^^^^^^^^^^^^^^^^ : 離開 Insert( Node, int) 後 nowNode 應該就會消失,這裡建立的物件不就會被gc回收 : 掉嗎? : nowNode 在 stack 上,然後藉由 nowNode.left = new Node( data ); 連結到 heap 上 : ,當從 Insert( Node, int) 之中 return 後, nowNode 被從 stack 推出, heap 上 : 的空間就沒人指向它了,所以被 gc 回收。 : : 請問我的觀念是哪個地方開始錯了呢? 先別管什麼 stack 還是 heap 了,你聽過 reference 嗎? 我說真的,你不用管什麼 statck 還是 heap(至少我從來都沒想過這個) GC 基本上只在意 instance 有沒有被 reference 到 你跳開 method 呼叫的例子來看會比較簡單 Foo foo = new Foo(); Object refA = instance; refA = null; //在這裡,Foo 的 instance 還是被 foo 給 reference 到 //所以那個 instance 不會被 GC 掉 foo = null; //現在沒人指到那個 instance 了,所以有可能被 GC 掉 所以說 callee 的 nowNode 沒有被 reference 不代表說 caller 沒人 reference 到他 用反面例子就是,除非你這樣寫,才會是你說的那個劇本 //假設 method 什麼都不做 public static void method(Node n, int v){} //somewhere method(new Node(), 1) 不然另一個極端的例子,理論上永遠不會被 GC 掉 public class AlwaysNode { public static node = new Node(); } //somewhere method(AlwaysNode.node, 1); -- 錢鍾書: 說出來的話 http://www.psmonkey.org 比不上不說出來的話 Java 版 cookcomic 版 只影射著說不出來的話 and more...... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.202.140
LaPass:static的話好像要整個class被classloader「丟掉」才會GC的 02/05 10:12
LaPass:樣子 02/05 10:53
bleed1979:可以告訴我node的type是什麼嗎? 這算是猴王的尾巴? 02/05 14:53
PsMonkey:樓上,我不太懂你的明白..... 02/05 15:16
tkcn:在說這句吧: public static node = new Node(); 02/05 21:24
bleed1979:樓上是對的。 02/06 23:00