簡體   English   中英

當 object 在堆轉儲分析中僅被“this$0”引用時,這意味着什么?

[英]What does it mean when an object is only referenced by 'this$0' in a heap dump analysis?

我分析了一個堆轉儲以找出為什么某個 class 的某些多個實例即使在它們的目的結束后仍然在 memory 中。 我發現對每個實例的唯一引用是 object 本身。

ScheduleWindow#1 將 'this$0 in ScheduleWindow$1#1' 作為唯一參考。

ScheduleWindow#2 有 'this$0 in ScheduleWindow$1#2' 作為唯一的參考。

ScheduleWindow#3 有 'this$0 in ScheduleWindow$1#3' 作為唯一的參考。

...

有這樣的引用意味着什么? ScheduleWindow 也不是內部 class 。

PS 請原諒我沒有發布實際代碼,因為由於法律原因我無法發布實際代碼。

內部 class 始終隱含對外部 class 的引用,顯示為“this$0”。 以這段代碼為例:

public class Test {

  String test;

  // inner class has a this$0 reference to its outer / parent object
  public class Inner {
    String inner;

    void bar(Object anonymous) {
      // the debugger screenshot is taken here
      System.out.println();
    }
  }

  void foo() {
    // create an instance of Inner and pass an anonymous inner class to it
    new Inner().bar(new Runnable() {
      public void run() {}
    });
  }

  public static void main(String[] args) {
    new Test().foo();
  }
}

如果在“bar”中設置斷點,調用堆棧為:main -> foo -> bar。 調試器顯示以下變量:

在此處輸入圖像描述

Bar 是內部 class(id 22)中的一個方法,它通過 this$0 指向外部 class(id 24)。

Test 中還有一個匿名的內部 class,稱為 Test$1。 它也有相同的指針。

如果將 Inner 定義為 static 內部 class,則此指針不存在。

看到引用“ScheduleWindow$1” 您可能在其中某處定義了一個匿名 class(如上例中的 Runnable),並且此實例具有對 ScheduleWindow 的 this$0 引用。

請查看 class 中的任何 static 字段。 如果您希望它在 memory 堆中輕松釋放,最好使 class 無狀態。 我以前遇到過這類問題。 在 Singleton Util 中有一個糟糕的設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM