How/when is a Handler garbage collected?

后端 未结 3 1805
闹比i
闹比i 2021-02-12 16:26

Inside a class of mine I have the following code:

mHandler = createHandler();

private Handler createHandler() {
    return new Handler() {
        public void h         


        
3条回答
  •  执念已碎
    2021-02-12 16:58

    In my specific example since the Handler is an anonymous inner class it has an implicit reference to the enclosing Object and the whole hierarchy of objects that is pointed by it.

    You could reduce the impact of the potential leak to almost nothing by using a static nested class instead of an anonymous inner class.

提交回复
热议问题