欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java9以后的垃圾回收的具體用法

 更新時(shí)間:2019年10月14日 08:29:26   作者:---dgw博客  
這篇文章主要介紹了Java9以后的垃圾回收的具體用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1: finalize() 方法

finallize() 方法是Object類的方法, 用于在類被GC回收時(shí) 做一些處理操作, 但是JVM并不能保證finalize(0 ) 方法一定被執(zhí)行,
由于finalize()方法的調(diào)用時(shí)機(jī)具有不確定性,從一個(gè)對(duì)象變得不可到達(dá)開始,到finalize()方法被執(zhí)行,所花費(fèi)的時(shí)間這段時(shí)間是任意長(zhǎng)的。我們并不能依賴finalize()方法能及時(shí)的回收占用的資源,可能出現(xiàn)的情況是在我們耗盡資源之前,gc卻仍未觸發(fā),因而通常的做法是提供顯示的close()方法供客戶端手動(dòng)調(diào)用

所以一般不建議使用finalize 方法, JDK9 開始已久被廢除

- 總結(jié)缺點(diǎn)

1: finalize機(jī)制本身就是存在問題的。
2:finalize機(jī)制可能會(huì)導(dǎo)致性能問題,死鎖和線程掛起。
3:finalize中的錯(cuò)誤可能導(dǎo)致內(nèi)存泄漏;如果不在需要時(shí),也沒有辦法取消垃圾回收;并且沒有指定不同執(zhí)行finalize對(duì)象的執(zhí)行順序。此外,沒有辦法保證finlize的執(zhí)行時(shí)間。 

遇到這些情況,對(duì)象調(diào)用finalize方法只有被無限期延后

-觀察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來代替實(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ì)象 下面說
    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ì)于外部類有依賴(引用),無法真正實(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論