What is a synthetic back reference to an inner cla

2019-05-10 20:29发布

I am looking for memory leaks in my application and the profiler I am using tells me to look for these types of references but I don't know what I'm looking for. Can some one please explain this?

Thanks,

Elliott

2条回答
闹够了就滚
2楼-- · 2019-05-10 21:20

I don't think there is such thing as synthetic references to an inner class. I think the profiler is talking about references from inner classes to their enclosing classes. These are created when you have code like this:

class Outer {
  class Inner {
  }
}

In the above code, every instance of Inner has an instance of Outer associated with it. The association is maintained through a hidden synthetic member field of Inner that contains a reference to Outer.

If the code were changes like so:

class Outer {
  static class Inner {
  }
}

there would be no such synthetic reference.

查看更多
叛逆
3楼-- · 2019-05-10 21:31

You can have synthetic back reference to an OUTER class, but not inner class instances.

e.g.

class Outer {

    class Inner {
    }

    static class Nested {
    }
}

In this example, Inner has a reference to the Outer class. Nested does not. If Outer is large this can mean you have can be holding onto an object you don't need.

In short, make inner classes static if you can.

查看更多
登录 后发表回答