Possibility of unhandled memory leak

北慕城南 提交于 2019-12-23 13:25:09

问题


First of all, I come from iOS environment, so this is why this question might be obvious.

I know Android has Garbage Collector, but objects still reference (retain) other objects, and my understanding is the GC will only remove an object if it has no references (probably I'm wrong in this point). Looking at this code:

private void addDefaultTextWatcher(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                value = s.toString();
                if (getOnValidate() != null) {
                    getOnValidate().validate(editText, s.toString());
                }
            }
        });
    }

editTexthas a reference the TextWatcher anonymous class, and that class has a reference to editText as well. Is Garbage collector going to take care of this for me? If no... what is the suggested approach?


回答1:


Java Garbage Collector is smart enough to recognize cycle references. You should do nothing.



来源:https://stackoverflow.com/questions/17684852/possibility-of-unhandled-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!