Java之對象銷毀和finalize方法的使用
對象的銷毀
在C++中析構方法用於釋放資源並且銷毀對象本身。
在Java中,由於GC的存在,我們不需要手動回收內存,這大大減少瞭工作量,也提高瞭程序的安全性。但是Java也確實存在一個類似於C++中析構的函數。
finalize方法
重載該方法,用於在類被GC回收的時候執行一些操作。
下面是一個類實現finalize的示例。
Aoo類具有一個int 一個String屬性,重載瞭toString並且在構造其中打印這個對象及其創建時間,在finalize中打印這個對象及調用時間。
Aoo類
public class Aoo { private int id; private String name; public Aoo(){ this(0, null); } public Aoo(int id, String name){ this.id = id; this.name = name; System.out.println(this.toString() + " now create:" + System.currentTimeMillis()); } /* * 省略get/set/toString */ protected void finalize() throws Throwable{ super.finalize(); System.out.println(this.toString() + "now finalize:" + System.currentTimeMillis()); } }
首先,一個簡單的測試
main方法
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo a = new Aoo(1, "a"); a = null; System.gc() Thread.sleep(2000); System.exit(0); } }
打印結果:
id:1 name:a now create:1497547723036
id:1 name:anow finalize:1497547724059
GC對對象的回收
這裡手動調用瞭GC來清理內存,而如果將其註釋掉System.gc();,打印結果是這樣的:
id:1 name:a now create:1497547846923
也就是說,在沒有特意調用GC的情況下,finalize方法根本沒有被調用,也就是說這個對象根本沒有被主動回收。
和想象中的不同,GC的運行方式是惰性的,也就是說,在內存沒有一處的情況下,GC不會去主動回收對象,為瞭驗證這個想法,我創建瞭一個線程,用於不斷的消耗內存,並且不主動調用GC。
ThreadA類
public class ThreadA implements Runnable{ public void run() { List<Integer> list = new ArrayList<Integer>(); int i = 0; while(true){ list.add(i); i++; } } }
main方法
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo a = new Aoo(1, "a"); a = null; ThreadA ta = new ThreadA(); Thread t = new Thread(ta); t.start(); Thread.sleep(2000); System.exit(0); } }
打印結果:
id:1 name:a now create:1497548135268
id:1 name:anow finalize:1497548135386
這一次盡管沒有手動調用GC,但是finalize方法仍然運行瞭,也就是說,隻有在內存被消耗、需要GC出面清理內存的時候,GC才會運行。
這樣的finalize方法確實不靠譜,連能不能被調用都不一定,更不用說完成什麼特定的操作瞭,如果需要關流等回收資源,不如手動調用一個方法,或者在final塊中統一釋放資源。
在finalize方法中,是否重新給自己指定一個引用來避免被GC回收?
嘗試在finalize方法中重新引用來讓GC無法回收
修改後的Aoo如下
public class Aoo { public static Aoo SAVE = null; private int id; private String name; public Aoo(){ this(0, null); } public Aoo(int id, String name){ this.id = id; this.name = name; System.out.println(this.toString() + " now create:" + System.currentTimeMillis()); } /* * 省略get/set/toString */ protected void finalize() throws Throwable{ super.finalize(); System.out.println(this.toString() + "now finalize:" + System.currentTimeMillis()); SAVE = this; } }
main方法
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo.SAVE = new Aoo(1, "a"); Aoo.SAVE = null; System.gc(); Thread.sleep(500); System.out.println(Aoo.SAVE == null? "a is dead" : "a is alive" ); System.exit(0); } }
打印結果:
id:1 name:a now create:1497551409195
id:1 name:anow finalize:1497551409201
a is alive
這裡看出,Aoo.SAVE對象確實“復活瞭”,不過這樣的操作是有限制的,如果故技重施不會再一次“復活”該對象。
main方法
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo.SAVE = new Aoo(1, "a"); Aoo.SAVE = null; System.gc(); Thread.sleep(500); System.out.println(Aoo.SAVE == null? "a is dead" : "a is alive" ); Aoo.SAVE = null; System.gc(); Thread.sleep(500); System.out.println(Aoo.SAVE == null? "a is dead" : "a is alive" ); System.exit(0); } }
打印結果:
id:1 name:a now create:1497551587715
id:1 name:anow finalize:1497551587721
a is alive
a is dead
這裡註意到,兩次的操作是相同的,而finalize方法隻會被系統調用一次。
如果finalze方法中出現死循環會發生什麼?
Aoo類
public class Aoo { private int id; private String name; public Aoo(){ this(0, null); } public Aoo(int id, String name){ this.id = id; this.name = name; System.out.println(this.toString() + " now create:" + System.currentTimeMillis()); } /* * 省略get/set/toString */ protected void finalize() throws Throwable{ super.finalize(); while(true){ System.out.println(this.toString() + "now finalize:" + System.currentTimeMillis()); Thread.sleep(100); } } }
main方法
public class FinalizeTest { public static void main(String[] args) throws Exception { Aoo a1 = new Aoo(1 , "a1"); Aoo a2 = new Aoo(2 , "a2"); a1 = null; a2 = null; ThreadA ta = new ThreadA(); Thread t = new Thread(ta); t.start(); Thread.sleep(5000); System.exit(0); } }
打印結果:
id:1 name:a1 now create:1497552024252
id:2 name:a2 now create:1497552024252
id:1 name:a1now finalize:1497552024373
id:1 name:a1now finalize:1497552024503
id:1 name:a1now finalize:1497552026848
id:1 name:a1now finalize:1497552028960
id:1 name:a1now finalize:1497552032363
結果是隨機的,有時候是執行的a1的finalize,有的時候執行的是a2的。
這個結果說明瞭兩點:
1.finalze方法在的線程優先級很低,時間間隔相當的不確定並且明顯大於100毫秒。
2.這個死循環導致瞭別的對象的finalize方法無法執行。
如果對象的創建出現這種死循環,會不會導致對象無法銷毀進而導致內存溢出?
我們大量創建Aoo對象,並且等待GC自己回收內存。
為瞭直觀的觀看finalize方法的調用情況,刪除掉瞭Aoo對象初始化的時候的打印代碼。
main方法
public class FinalizeTest { public static void main(String[] args) throws Exception { int i = 1; while(true){ Aoo a = new Aoo(i , "a" + i); i++; } } }
讓程序執行瞭約兩分鐘,然後手動終止,查看輸出
1497554225913
id:269614 name:a269614now finalize:1497554226151
id:269614 name:a269614now finalize:1497554227635
id:269614 name:a269614now finalize:1497554227735
id:269614 name:a269614now finalize:1497554227836
id:269614 name:a269614now finalize:1497554229586
id:269614 name:a269614now finalize:1497554229686
id:269614 name:a269614now finalize:1497554229951
id:269614 name:a269614now finalize:1497554230051
id:269614 name:a269614now finalize:1497554230152
id:269614 name:a269614now finalize:1497554233699
id:269614 name:a269614now finalize:1497554233800
id:269614 name:a269614now finalize:1497554233900
id:269614 name:a269614now finalize:1497554234308
id:269614 name:a269614now finalize:1497554234408
id:269614 name:a269614now finalize:1497554234508
id:269614 name:a269614now finalize:1497554235053
id:269614 name:a269614now finalize:1497554235153
id:269614 name:a269614now finalize:1497554235253
id:269614 name:a269614now finalize:1497554235823
id:269614 name:a269614now finalize:1497554235923
id:269614 name:a269614now finalize:1497554236023
id:269614 name:a269614now finalize:1497554240324
id:269614 name:a269614now finalize:1497554240424
id:269614 name:a269614now finalize:1497554240525
id:269614 name:a269614now finalize:1497554241146
id:269614 name:a269614now finalize:1497554241247
id:269614 name:a269614now finalize:1497554241347
id:269614 name:a269614now finalize:1497554241448
id:269614 name:a269614now finalize:1497554242020
id:269614 name:a269614now finalize:1497554242120
id:269614 name:a269614now finalize:1497554242220
id:269614 name:a269614now finalize:1497554242321
id:269614 name:a269614now finalize:1497554242421
id:269614 name:a269614now finalize:1497554242521
id:269614 name:a269614now finalize:1497554248367
id:269614 name:a269614now finalize:1497554248467
id:269614 name:a269614now finalize:1497554248567
id:269614 name:a269614now finalize:1497554248667
id:269614 name:a269614now finalize:1497554249534
id:269614 name:a269614now finalize:1497554249634
id:269614 name:a269614now finalize:1497554249734
id:269614 name:a269614now finalize:1497554249835
id:269614 name:a269614now finalize:1497554255954
id:269614 name:a269614now finalize:1497554256055
id:269614 name:a269614now finalize:1497554256155
id:269614 name:a269614now finalize:1497554256255
id:269614 name:a269614now finalize:1497554256356
id:269614 name:a269614now finalize:1497554257285
id:269614 name:a269614now finalize:1497554257386
id:269614 name:a269614now finalize:1497554257486
id:269614 name:a269614now finalize:1497554257586
id:269614 name:a269614now finalize:1497554257686
id:269614 name:a269614now finalize:1497554268652
id:269614 name:a269614now finalize:1497554268753
id:269614 name:a269614now finalize:1497554268853
id:269614 name:a269614now finalize:1497554268953
id:269614 name:a269614now finalize:1497554269054
id:269614 name:a269614now finalize:1497554269154
id:269614 name:a269614now finalize:1497554277474
id:269614 name:a269614now finalize:1497554292852
id:269614 name:a269614now finalize:1497554301062
可以發現兩個情況:
1.隻有一個對象的finalize方法被執行瞭,也就是說這個死循環的finalize方法阻止瞭其他對象執行finalize方法
2.程序執行很快的一段時間後,finalize方法就開始執行,但是隨著內存消耗的不斷增加,finalize方法被執行的次數也就越來越少。至於為什麼這樣,我不知道= =#
總結
至此,我嘗試瞭finalize方法的一些用法和特殊情況。可以看出,GC調用finalize方法存在巨大的不確定性,確實很不靠譜,不過通過這個方法,瞭解瞭一些關於GC的知識,也讓我明白,雖然Java語言雖然具有高度的一致性等特點使之很容易上手,但是要做到對Java的精通,路還很遠呢~~
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。