Java9垃圾回收方法finalize() 原理解析
1: finalize() 方法
finallize() 方法是Object類的方法, 用于在類被GC回收時(shí) 做一些處理操作, 但是JVM并不能保證finalize(0 ) 方法一定被執(zhí)行,由于finalize()方法的調(diào)用時(shí)機(jī)具有不確定性,從一個(gè)對(duì)象變得不可到達(dá)開(kāi)始,到finalize()方法被執(zhí)行,所花費(fèi)的時(shí)間這段時(shí)間是任意長(zhǎng)的。我們并不能依賴finalize()方法能及時(shí)的回收占用的資源,可能出現(xiàn)的情況是在我們耗盡資源之前,gc卻仍未觸發(fā),因而通常的做法是提供顯示的close()方法供客戶端手動(dòng)調(diào)用所以一般不建議使用finalize 方法, JDK9 開(kāi)始已久被廢除
總結(jié)缺點(diǎn)
1: finalize機(jī)制本身就是存在問(wèn)題的。
2:finalize機(jī)制可能會(huì)導(dǎo)致性能問(wèn)題,死鎖和線程掛起。
3:finalize中的錯(cuò)誤可能導(dǎo)致內(nèi)存泄漏;如果不在需要時(shí),也沒(méi)有辦法取消垃圾回收;并且沒(méi)有指定不同執(zhí)行finalize對(duì)象的執(zhí)行順序。此外,沒(méi)有辦法保證finlize的執(zhí)行時(shí)間。遇到這些情況,對(duì)象調(diào)用finalize方法只有被無(wú)限期延后
- 觀察finalize方法延長(zhǎng)類生命周期#
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)請(qǐng)求gc }}//輸出 Finalizer-->finalize()
2:Cleaner類的使用
簡(jiǎn)介:
在Java9 以后 提供了最終類Clear來(lái)代替實(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)建對(duì)象 static class State implements Runnable{ // 清理對(duì)象 下面說(shuō) 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); // 注冊(cè)清理容器中 并且需要清理對(duì)象的引用 } @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)部類 局部?jī)?nèi)部類 對(duì)于外部類有依賴(引用),無(wú)法真正實(shí)現(xiàn)內(nèi)存的釋放 ) 可以選擇直接定義外部類 (較為復(fù)雜,需要傳遞清理引用 Cleanable)什么時(shí)候被回收?
* 1. 注冊(cè)的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ù)請(qǐng)求 @Override public void run() { System.out.println('數(shù)據(jù)庫(kù) 海量 查詢請(qǐng)求 ................'); } // 清理模版 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è)字段,基本上明確了 他的基本原理
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. Ajax返回值類型與用法實(shí)例分析3. 如何在jsp界面中插入圖片4. 詳解盒子端CSS動(dòng)畫性能提升5. ajax請(qǐng)求后臺(tái)得到j(luò)son數(shù)據(jù)后動(dòng)態(tài)生成樹(shù)形下拉框的方法6. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)7. asp批量添加修改刪除操作示例代碼8. .NET6打包部署到Windows Service的全過(guò)程9. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼10. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題
