Java9垃圾回收方法finalize() 原理解析
1: finalize() 方法
finallize() 方法是Object類的方法, 用于在類被GC回收時(shí) 做一些處理操作, 但是JVM并不能保證finalize(0 ) 方法一定被執(zhí)行,由于finalize()方法的調(diào)用時(shí)機(jī)具有不確定性,從一個(gè)對象變得不可到達(dá)開始,到finalize()方法被執(zhí)行,所花費(fèi)的時(shí)間這段時(shí)間是任意長的。我們并不能依賴finalize()方法能及時(shí)的回收占用的資源,可能出現(xiàn)的情況是在我們耗盡資源之前,gc卻仍未觸發(fā),因而通常的做法是提供顯示的close()方法供客戶端手動(dòng)調(diào)用所以一般不建議使用finalize 方法, JDK9 開始已久被廢除
總結(jié)缺點(diǎn)
1: finalize機(jī)制本身就是存在問題的。
2:finalize機(jī)制可能會導(dǎo)致性能問題,死鎖和線程掛起。
3:finalize中的錯(cuò)誤可能導(dǎo)致內(nèi)存泄漏;如果不在需要時(shí),也沒有辦法取消垃圾回收;并且沒有指定不同執(zhí)行finalize對象的執(zhí)行順序。此外,沒有辦法保證finlize的執(zhí)行時(shí)間。遇到這些情況,對象調(diào)用finalize方法只有被無限期延后
- 觀察finalize方法延長類生命周期#
class User{ public static User user = null; @Override protected void finalize() throws Throwable { System.out.println('User-->finalize()'); user = this; } } public class FinalizerTest { public static void main(String[] args) throws InterruptedException { User user = new User(); user = null; System.gc(); Thread.sleep(1000); user = User.user; System.out.println(user != null);//true user = null; System.gc(); Thread.sleep(1000); System.out.println(user != null);//false }}
- JDk9 以前的垃圾回收代碼
public class Finalizer { @Override protected void finalize() throws Throwable { System.out.println('Finalizer-->finalize()'); } public static void main(String[] args) { Finalizer f = new Finalizer(); f = null;System.gc();//手動(dòng)請求gc }}//輸出 Finalizer-->finalize()
2:Cleaner類的使用
簡介:
在Java9 以后 提供了最終類Clear來代替實(shí)現(xiàn),下面看一下官方例子
package Thread;import java.lang.ref.Cleaner;public class CleaningExample implements AutoCloseable{ private final static Cleaner CLEANER=Cleaner.create();// 創(chuàng)建者模式創(chuàng)建對象 static class State implements Runnable{ // 清理對象 下面說 State() { System.out.println('init'); } @Override public void run() { System.out.println('close'); } } private final State state; private final Cleaner.Cleanable cleanable; // clearner 中的接口 實(shí)現(xiàn)唯一的清理方法 public CleaningExample() { super(); this.state = new State(); this.cleanable=CLEANER.register(this, state); // 注冊清理容器中 并且需要清理對象的引用 } @Override public void close() throws Exception { cleanable.clean(); //進(jìn)行清理操作 } public static void main(String[] args) { while(true) { new CleaningExample(); } }}
上面可以看出:
Cleaner 是最終類 不能被重寫, 內(nèi)部方法基本以靜態(tài)方法提供 掌握例子上面的方法即可
重點(diǎn)指出
static class State implements Runnable
如果直接在類中直接定義實(shí)現(xiàn), 必須提供一個(gè)靜態(tài)內(nèi)部類 (強(qiáng)制),否者不能進(jìn)行回收 原因(: 普通內(nèi)部類 局部內(nèi)部類 對于外部類有依賴(引用),無法真正實(shí)現(xiàn)內(nèi)存的釋放 ) 可以選擇直接定義外部類 (較為復(fù)雜,需要傳遞清理引用 Cleanable)什么時(shí)候被回收?
* 1. 注冊的Object處于幻象引用狀態(tài)
* 2. 顯式調(diào)用 clean 方法
實(shí)際例子(模版)
public class CleaningExample extends Thread implements AutoCloseable { private final static Cleaner CLEANER = Cleaner.create(); private final State state; private final Cleaner.Cleanable cleanable; public CleaningExample() { this.state = new State(); this.cleanable = CLEANER.register(this, state); } @Override public void close() throws Exception { cleanable.clean(); } @SuppressWarnings('resource') public static void main(String[] args) { while (true) { CleaningExample example = new CleaningExample(); } } // 模擬業(yè)務(wù)請求 @Override public void run() { System.out.println('數(shù)據(jù)庫 海量 查詢請求 ................'); } // 清理模版 class State implements Runnable { State() { System.out.println('<--- init --->'); } @Override public void run() { System.out.println('<--- close --->'); } }}
實(shí)現(xiàn)基礎(chǔ)
/** * Heads of a CleanableList for each reference type. */ final PhantomCleanable<?> phantomCleanableList; final WeakCleanable<?> weakCleanableList; final SoftCleanable<?> softCleanableList; // The ReferenceQueue of pending cleaning actions final ReferenceQueue<Object> queue;
在CleanerImpl 類進(jìn)行clearner類的最終實(shí)現(xiàn),看以看到定義的這些個(gè)字段,基本上明確了 他的基本原理
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析2. 教你如何寫出可維護(hù)的JS代碼3. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 使用Python和百度語音識別生成視頻字幕的實(shí)現(xiàn)6. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算7. Vue的Options用法說明8. css代碼優(yōu)化的12個(gè)技巧9. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法10. IDEA版最新MyBatis程序配置教程詳解
