EditText 导致内存泄漏 [英] EditText causing memory leak

查看:22
本文介绍了EditText 导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:

我有一个具有以下结构的应用程序:顶部操作栏 (ActionBarSherlock)下方的 ViewPagerIndicator(用于选项卡)ViewPager(托管片段)

I have an app which has the following structure: ActionBar up top (ActionBarSherlock) ViewPagerIndicator below that (for tabs) ViewPager (hosts Fragments)

我有一个问题,我的一个片段导致了相当大的内存泄漏.我将问题缩小到以下情况:

I have a problem that one of my fragments is causing a rather major memory leak. I narrowed the problem down to the following case:

导致泄漏的片段除了在其 onCreateView 方法中膨胀布局之外什么都不做.这是通过以下方式完成的:

The fragment which is causing the leak does nothing but inflating a layout in it's onCreateView method. This is done the following way:

return inflater.inflate(R.layout.filter_auctions_fragment, container, false);

这里没什么异常.

布局文件只包含一个 ScrollViewLinearLayout 和两个 EditText(包括更正常的东西,但我缩小了问题的范围仅使用这些视图以使其变得简单).

The layout file only includes a ScrollView, LinearLayout and two EditTexts in it (includes more normally stuff but i narrowed the problem down to just these views to make it simple).

现在用于添加片段的代码:mTabsAdapter.addTab(tabName, ProblematicFragment.class);

Now the code that's used to add the fragment: mTabsAdapter.addTab(tabName, ProblematicFragment.class);

mTabsAdapterTabsAdapter 的一个实例,该类扩展了支持库的FragmentPagerAdapter.这是相当标准的,所以我不包括来源,以使这个问题尽可能简短.

mTabsAdapter is an instance of TabsAdapter, a class that extends FragmentPagerAdapter of the support library. It's rather standard so I'm not including the source to keep this question as short as possible.

现在是有趣的部分:

当我来回旋转设备几次时,堆会发生这种情况:

This is what happens with the heap when i rotate my device back and forth a few times:

12-28 12:26:27.180: D/dalvikvm(18841): GC_CONCURRENT freed 530K, 7% free 10701K/11436K, paused 4ms+7ms, total 58ms
12-28 12:26:27.180: D/dalvikvm(18841): WAIT_FOR_CONCURRENT_GC blocked 24ms
12-28 12:26:28.270: D/dalvikvm(18841): GC_CONCURRENT freed 737K, 8% free 11048K/11964K, paused 4ms+5ms, total 53ms
12-28 12:26:29.510: D/dalvikvm(18841): GC_CONCURRENT freed 789K, 8% free 11464K/12436K, paused 5ms+5ms, total 42ms
12-28 12:26:30.640: D/dalvikvm(18841): GC_CONCURRENT freed 888K, 9% free 11919K/12984K, paused 4ms+5ms, total 52ms
12-28 12:26:31.810: D/dalvikvm(18841): GC_CONCURRENT freed 903K, 8% free 12421K/13500K, paused 3ms+8ms, total 58ms
12-28 12:26:33.800: D/dalvikvm(18841): GC_CONCURRENT freed 1092K, 9% free 13005K/14272K, paused 4ms+6ms, total 59ms
12-28 12:26:33.800: D/dalvikvm(18841): WAIT_FOR_CONCURRENT_GC blocked 20ms
12-28 12:26:36.000: D/dalvikvm(18841): GC_CONCURRENT freed 1355K, 11% free 13518K/15048K, paused 3ms+8ms, total 74ms
12-28 12:26:36.000: D/dalvikvm(18841): WAIT_FOR_CONCURRENT_GC blocked 19ms
12-28 12:26:38.110: D/dalvikvm(18841): GC_CONCURRENT freed 1450K, 11% free 14106K/15720K, paused 3ms+11ms, total 72ms
12-28 12:26:40.450: D/dalvikvm(18841): GC_CONCURRENT freed 1530K, 11% free 14807K/16516K, paused 2ms+15ms, total 75ms
12-28 12:26:40.450: D/dalvikvm(18841): WAIT_FOR_CONCURRENT_GC blocked 29ms
12-28 12:26:43.030: D/dalvikvm(18841): GC_CONCURRENT freed 1682K, 11% free 15591K/17452K, paused 3ms+10ms, total 66ms
12-28 12:26:43.030: D/dalvikvm(18841): WAIT_FOR_CONCURRENT_GC blocked 32ms

显然,内存泄漏.是的,我知道它会导致 Activity 从头开始​​重新创建,这就是我想要的,因为我对横向和纵向模式有不同的布局.尽管如此,它不应该导致内存泄漏.

Clearly, a memory leak. Yes, i know it causes the Activity to be recreated from the ground up and this is what I want because i have different layouts for landscape and portrait modes. Nontheless it shouldn't cause a memory leak.

我已经找到了这个问题的根源.这是我之前提到的两个 EditText.一旦我从布局中删除它们并进行相同的测试(来回旋转).这些是我收到的 GC 消息:

I've found the source of this problem. It's the two EditText's that i mentioned earlier. As soon as i remove them from the layout and do the same test (rotate back and forth). These are the GC messages i get:

12-28 12:21:41.270: D/dalvikvm(17934): GC_CONCURRENT freed 534K, 7% free 10853K/11576K, paused 3ms+7ms, total 44ms
12-28 12:21:42.560: D/dalvikvm(17934): GC_CONCURRENT freed 818K, 9% free 11113K/12108K, paused 11ms+9ms, total 95ms
12-28 12:21:44.680: D/dalvikvm(17934): GC_CONCURRENT freed 1036K, 10% free 11313K/12528K, paused 3ms+6ms, total 54ms
12-28 12:21:44.680: D/dalvikvm(17934): WAIT_FOR_CONCURRENT_GC blocked 15ms
12-28 12:21:47.420: D/dalvikvm(17934): GC_CONCURRENT freed 1089K, 10% free 11510K/12780K, paused 2ms+6ms, total 79ms
12-28 12:21:47.420: D/dalvikvm(17934): WAIT_FOR_CONCURRENT_GC blocked 39ms
12-28 12:21:50.200: D/dalvikvm(17934): GC_CONCURRENT freed 1317K, 12% free 11461K/12956K, paused 4ms+13ms, total 84ms
12-28 12:21:53.210: D/dalvikvm(17934): GC_CONCURRENT freed 1629K, 14% free 11148K/12956K, paused 3ms+7ms, total 47ms
12-28 12:21:55.580: D/dalvikvm(17934): GC_CONCURRENT freed 1056K, 13% free 11302K/12956K, paused 4ms+7ms, total 59ms
12-28 12:21:57.280: D/dalvikvm(17934): GC_CONCURRENT freed 1306K, 14% free 11200K/12956K, paused 5ms+5ms, total 82ms
12-28 12:21:59.420: D/dalvikvm(17934): GC_CONCURRENT freed 1035K, 12% free 11408K/12956K, paused 3ms+7ms, total 55ms
12-28 12:22:01.990: D/dalvikvm(17934): GC_CONCURRENT freed 1392K, 13% free 11352K/12956K, paused 4ms+9ms, total 54ms
12-28 12:22:01.990: D/dalvikvm(17934): WAIT_FOR_CONCURRENT_GC blocked 30ms

这就是我想看到的!

为什么!?

谁能告诉我为什么会这样?我想补充一点,我没有在我的应用程序的任何地方保留对这些 EditText 对象的引用(我通常会这样做,但即使我出于测试目的删除了所有这些对象,泄漏仍然会发生).

Can someone tell my why this is happening? I would like to add that I'm not keeping a reference to these EditText object anywhere in my app (I normally do, but even when i removed all of them for testing purposes the leak still happens).

奖励 - 泄漏的 MAT 屏幕截图:

如您所见,Fragment 和 Activity 有 16 个实例,而应该只有一个.

As you can see there are 16 instances of the Fragment AND the Activity, while there should be only one.

我注意到当我在不同的活动中手动添加片段时(使用 FragmentManager.beginTransaction())不会发生泄漏!!!我现在完全糊涂了...

I noticed that when i manually add the fragment in a different activity (using FragmentManager.beginTransaction()) the leak does not occur!!! I'm completely confused now...

删除 EditTextandroid:id 属性可以修复它...但现在它们非常无用...

Removing the android:id attribute of the EditTexts fixes it... But now they're pretty useless...

推荐答案

我找到了适合我需求的解决方案.

I have found a solution that fits my needs.

我将问题追溯到widget.EditableInputConnection.我认为在建议系统中进行查找.它还负责保持我的活动,从而导致内存泄漏.

I tracked the problem down to widget.EditableInputConnection. Which I think does the lookup in the suggestion system. It is also responsible for holding on to my Activity, hence causing a memory leak.

我不需要这些建议,所以我想关闭它.然而事实证明这很困难.EditText.setInputType 不起作用,无论是 xml 还是代码.

I do not need the suggestions so I wanted to turn it off. However it has proven to be difficult. EditText.setInputType did not work, neither in xml nor code.

我最终做了以下事情.onCreateInputConnection() 的神奇之处:

I ended up doing the following. The magic is happening in onCreateInputConnection():

public class MyEditText extends TextView {
    public MyEditText(Context context) {
        this(context, null);
    }

    public MyEditText(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        return null;
    }

    @Override
    protected boolean getDefaultEditable() {
        return true;
    }

    @Override
    protected MovementMethod getDefaultMovementMethod() {
        return ArrowKeyMovementMethod.getInstance();
    }

    @Override
    public Editable getText() {
        return (Editable) super.getText();
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text, BufferType.EDITABLE);
    }

    /**
     * Convenience for {@link Selection#setSelection(Spannable, int, int)}.
     */
    public void setSelection(int start, int stop) {
        Selection.setSelection(getText(), start, stop);
    }

    /**
     * Convenience for {@link Selection#setSelection(Spannable, int)}.
     */
    public void setSelection(int index) {
        Selection.setSelection(getText(), index);
    }

    /**
     * Convenience for {@link Selection#selectAll}.
     */
    public void selectAll() {
        Selection.selectAll(getText());
    }

    /**
     * Convenience for {@link Selection#extendSelection}.
     */
    public void extendSelection(int index) {
        Selection.extendSelection(getText(), index);
    }

    @Override
    public void setEllipsize(TextUtils.TruncateAt ellipsis) {
        if (ellipsis == TextUtils.TruncateAt.MARQUEE) {
            throw new IllegalArgumentException("EditText cannot use the ellipsize mode "
                    + "TextUtils.TruncateAt.MARQUEE");
        }
        super.setEllipsize(ellipsis);
    }
}

诀窍是拒绝 InputConnection.这将删除建议并消除内存泄漏.

Where the trick is to refuse the InputConnection. This removes the suggestions and removes the memory leak.

希望这对你有帮助..

这篇关于EditText 导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆