问题描述
我有一个TabLayout,在每个选项卡下都有一个Fragment(我正在使用ArrayPagerAdapter).我注意到,当我从一个选项卡多次切换到另一个选项卡时,我的内存使用量增加了很多.从我的堆快照中,我可以看到有很多AutoCompleteTextView实例.
I have a TabLayout, under each tab there is a Fragment (I'm using ArrayPagerAdapter). I've noticed that when I switch many times from a tab to another, my memory usage increase a lot. From my heap snapshot, I can see there are a lot of AutoCompleteTextView instances.
因此,我确信问题可能出在这里:
So I am convinced that the problem could be here:
public class MyFragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
final MultiAutoCompleteTextView eInput = (MultiAutoCompleteTextView) v.findViewById(R.id.TextInput);
EditorListener mEditorListener = new EditorListener();
eInput.setOnEditorActionListener(mEditorListener);
eInput.addTextChangedListener(new WhitespaceWatcher());
eInput.setAdapter(mDictionaryAdapter);
eInput.setTokenizer(new SpaceTokenizer());
...
}
...
class EditorListener implements TextView.OnEditorActionListener
{
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
...
MultiAutoCompleteTextView input = (MultiAutoCompleteTextView) textView.findViewById(R.id.TextInput);
...
}
}
...
}
但是我不明白问题出在哪里.
But I can't understand where exactly the problem is.
推荐答案
对其他人的注意:张贴者和我就此问题进行了非官方的讨论,张贴者创建了能够重现此问题的示例应用程序.
Note to others: the poster and I had an off-SO discussion about this issue, and the poster created this sample app that was able to reproduce the problem.
经过一番挣扎,我得以使 LeakCanary 正常工作.与运输1.3.1
相比,它需要1.4-beta1
.我需要做的就是添加依赖项,并根据LeakCanary
文档设置一个Application
子类.然后,启动应用程序,并在活动出现后按BACK.
After some struggling, I was able to get LeakCanary to work. It required 1.4-beta1
versus the shipping 1.3.1
. All I needed to do was add the dependencies and set up an Application
subclass per the LeakCanary
docs. Then, launch the app and press BACK once the activity appears.
您得到:
这是框架错误还是appcompat-v7
及其特定子类MultiAutoCompleteTextView
引入的某些内容,我目前不能说.但是,这绝对不是代码中的错误.
Whether this is a framework bug or something introduced by appcompat-v7
and its specific subclass of MultiAutoCompleteTextView
, I can't say at present. However, it is definitely not a bug in your code.
从片段的onDestroyView()
中的MultiAutoCompleteTextView
(setAdapter(null)
)清除适配器应防止其泄漏活动,但是小部件本身仍会泄漏.快速浏览相关代码并没有给我太多希望,无需修改框架(对于MultiAutoCompleteTextView
)或appcompat-v7
(对于其子类),就可以解决泄漏本身.
Clearing the adapter from the MultiAutoCompleteTextView
(setAdapter(null)
) in onDestroyView()
of the fragment should prevent it from leaking the activity, but the widget itself will still leak. A quick scan of the relevant code does not give me much hope that the leak itself can be fixed without modifications to either the framework (for MultiAutoCompleteTextView
) or appcompat-v7
(for its subclass).
这篇关于在我的片段android代码上找不到泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!