您好,登錄后才能下訂單哦!
這篇文章主要介紹了Java對象銷毀和finalize方法怎么使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Java對象銷毀和finalize方法怎么使用文章都會有所收獲,下面我們一起來看看吧。
在C++中析構方法用于釋放資源并且銷毀對象本身。
在Java中,由于GC的存在,我們不需要手動回收內存,這大大減少了工作量,也提高了程序的安全性。但是Java也確實存在一個類似于C++中析構的函數。
重載該方法,用于在類被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來清理內存,而如果將其注釋掉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無法回收
修改后的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方法只會被系統調用一次。
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方法被執行的次數也就越來越少。
關于“Java對象銷毀和finalize方法怎么使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Java對象銷毀和finalize方法怎么使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。