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

huangapple go评论55阅读模式
英文:

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

问题

我分析了一个堆转储文件,以找出为什么某个特定类的多个实例在它们的用途结束后仍然在内存中。我发现每个实例唯一的引用是对象本身。

ScheduleWindow#1的唯一引用是'ScheduleWindow$1#1中的this$0'。

ScheduleWindow#2的唯一引用是'ScheduleWindow$1#2中的this$0'。

ScheduleWindow#3的唯一引用是'ScheduleWindow$1#3中的this$0'。

...

这样的引用是什么意思?ScheduleWindow也不是内部类。

P.S. 请理解我没有发布实际代码,因为由于法律原因我不能发布实际代码。

英文:

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. What I found out is that the only reference to each instance is the object itself.

ScheduleWindow#1 has 'this$0 in ScheduleWindow$1#1' as the only reference.

ScheduleWindow#2 has 'this$0 in ScheduleWindow$1#2' as the only reference.

ScheduleWindow#3 has 'this$0 in ScheduleWindow$1#3' as the only reference.

...

What does it mean to have references like this? ScheduleWindow is not an inner class either.

P.S. Please bear with me for not posting the actual code because I can't post the actual code due to legal reasons.

答案1

得分: 1

内部类始终具有对外部类的隐式引用,它显示为"this$0"。以此代码为例:

public class Test {

  String test;

  // 内部类对外部/父类对象具有this$0引用
  public class Inner {
    String inner;

    void bar(Object anonymous) {
      // 调试器截图在这里被拍摄
      System.out.println();
    }
  }

  void foo() {
    // 创建Inner的实例并将匿名内部类传递给它
    new Inner().bar(new Runnable() {
      public void run() {}
    });
  }

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

如果在"bar"中设置断点,调用堆栈是:main -> foo -> bar。调试器显示以下变量:

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

"bar"是内部类中的一个方法(id 22),通过this$0指向外部类(id 24)。

Test中还有一个匿名内部类,称为Test$1。它也具有相同的指针。

如果将Inner定义为静态内部类,则不存在此指针。

看到引用"ScheduleWindow$1",可能在其中某处定义了匿名类(就像上面示例中的Runnable),并且此实例具有对ScheduleWindow的this$0引用。

英文:

An inner class always has an implicit reference to the outer class which shows up as "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. The debugger shows the following variables:

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

Bar is a method in the inner class (id 22), which points to the outer class (id 24) via this$0.

There is also an anonymous inner class in Test which is referred to as Test$1. It also has the same pointer.

If you define Inner as a static inner class, this pointer is not present.

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.

答案2

得分: 0

请查看该类中的任何静态字段。如果您希望它在内存堆中轻松释放,最好将该类设计为无状态。我之前遇到过这种问题。在单例工具中有糟糕的设计。

英文:

Please see any static fields in that class. Also better to make the class stateless if you want it to easily release in the memory heap. I encounter this type of problem before. Having a bad design in a Singleton Util.

huangapple
  • 本文由 发表于 2020年8月3日 14:24:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定