【问题标题】:memory leak on EditText clickEditText 上的内存泄漏单击
【发布时间】:2018-06-03 07:49:27
【问题描述】:

我有一个以 ItemListActivity 类开头的 android 应用程序,在 onCreate() 方法中我检查用户是否登录到应用程序(基于 sharedPreferences 中保存的值),如果未登录,我将活动更改为content_main活动:

if (!SharedPreferencesUtils.sharedPrefHasString(getApplicationContext(),userIDKey,null)){
        Intent intent = new Intent(getApplicationContext(),content_main.class);
        startActivity(intent);
        finish();
        return;
    }

content_main 活动中,我只需 2 个 EditTexts 即可获取用户电子邮件和密码。

一切正常,直到我单击任何 Edittext,然后它开始耗尽内存并因以下日志而崩溃: java.lang.OutOfMemoryError: Failed to allocate a 35469582 byte allocation with 16769200 free bytes and 24MB until OOM.

我已经处理了几天,并且我读到它发生在您在 relativeLayout 中使用 edittext 时,所以我将布局更改为 LinearLayout 并且它也不起作用。 我的设备是三星galaxy s5,安卓版本是6.0.1

当我点击任何 EditText 时,我总是收到此错误。 这是我的 Android 分析器内存视图: 是否知道导致此问题的原因?

【问题讨论】:

  • 我猜这可能是个问题,所以我删除了所有图像(只有一个用于注册徽标和一个用于编辑文本的背景)。但它不会改变结果。
  • 在这一行中 Intent intent = new Intent(getApplicationContext(),content_main.class); 删除 getApplicationContext() 并改为写入 this 并且以后永远不会将 ApplicationContext() 传递给意图
  • @Devit951 感谢您的建议。但恐怕没有任何区别。

标签: java android memory-leaks


【解决方案1】:

Advance Android profiler 中存在一些问题。您需要按如下方式禁用它:

转到运行/调试配置 -> 分析 -> 取消选中启用高级分析

【讨论】: