简体   繁体   English

当 object 在堆转储分析中仅被“this$0”引用时,这意味着什么?

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

I analyzed a heap dump to find out why some multiple instances of a certain class are still in memory even after their purpose is over.我分析了一个堆转储以找出为什么某个 class 的某些多个实例即使在它们的目的结束后仍然在 memory 中。 What I found out is that the only reference to each instance is the object itself.我发现对每个实例的唯一引用是 object 本身。

ScheduleWindow#1 has 'this$0 in ScheduleWindow$1#1' as the only reference. ScheduleWindow#1 将 'this$0 in ScheduleWindow$1#1' 作为唯一参考。

ScheduleWindow#2 has 'this$0 in ScheduleWindow$1#2' as the only reference. ScheduleWindow#2 有 'this$0 in ScheduleWindow$1#2' 作为唯一的参考。

ScheduleWindow#3 has 'this$0 in ScheduleWindow$1#3' as the only reference. ScheduleWindow#3 有 'this$0 in ScheduleWindow$1#3' 作为唯一的参考。

... ...

What does it mean to have references like this?有这样的引用意味着什么? ScheduleWindow is not an inner class either. ScheduleWindow 也不是内部 class 。

PS Please bear with me for not posting the actual code because I can't post the actual code due to legal reasons. PS 请原谅我没有发布实际代码,因为由于法律原因我无法发布实际代码。

An inner class always has an implicit reference to the outer class which shows up as "this$0".内部 class 始终隐含对外部 class 的引用,显示为“this$0”。 Take this code for example:以这段代码为例:

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();
  }
}

If you set a breakpoint in "bar", the call stack is: main -> foo -> bar.如果在“bar”中设置断点,调用堆栈为:main -> foo -> bar。 The debugger shows the following variables:调试器显示以下变量:

在此处输入图像描述

Bar is a method in the inner class (id 22), which points to the outer class (id 24) via this$0. Bar 是内部 class(id 22)中的一个方法,它通过 this$0 指向外部 class(id 24)。

There is also an anonymous inner class in Test which is referred to as Test$1. Test 中还有一个匿名的内部 class,称为 Test$1。 It also has the same pointer.它也有相同的指针。

If you define Inner as a static inner class, this pointer is not present.如果将 Inner 定义为 static 内部 class,则此指针不存在。

Seeing the reference "ScheduleWindow$1" You probably have an anonymous class defined somewhere in there (like the Runnable in the example above) and this instance has the this$0 reference to ScheduleWindow.看到引用“ScheduleWindow$1” 您可能在其中某处定义了一个匿名 class(如上例中的 Runnable),并且此实例具有对 ScheduleWindow 的 this$0 引用。

Please see any static fields in that class.请查看 class 中的任何 static 字段。 Also better to make the class stateless if you want it to easily release in the memory heap.如果您希望它在 memory 堆中轻松释放,最好使 class 无状态。 I encounter this type of problem before.我以前遇到过这类问题。 Having a bad design in a Singleton Util.在 Singleton Util 中有一个糟糕的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM