Android - 避免内存泄漏的良好实践

标签 android performance memory memory-leaks imageview

您知道避免内存泄漏的良好做法吗?

我目前正在开发一个几乎没有内存泄漏的应用程序,我很难修复它,主要是因为我不知道我需要采取哪些习惯才能避免它们。

例如,目前我遇到这个问题

    Fatal Exception: java.lang.OutOfMemoryError
Failed to allocate a 1627572 byte allocation with 1293760 free bytes and 1263KB until OOM
 Raw Text
dalvik.system.VMRuntime.newNonMovableArray (VMRuntime.java)
android.view.LayoutInflater.inflate (LayoutInflater.java:429)
com.XX.Dialog.PopupDialog.onCreateView (PopupDialog.java:50)

来自onCreateView中的inflater.inflate(R.layout.dialog_popup,container, false);!

Here is the DialogFragment concerned : 

public final class PopupDialog extends DialogFragment {

    public PopupDialog()
    {
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        View view = inflater.inflate(R.layout.dialog_popup, container, false);
        return view;
    }

布局相当长,但基本上有 5 个 ImageView 。这是一个示例:

    <ImageView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:id="@+id/popup_top_bg_iv"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="false"
    android:layout_alignParentStart="false"
    android:src="@drawable/popup_top_bg"
    android:scaleType="fitXY"
    android:background="@color/transparent" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/popup_top_bg_iv"
    android:background="#ffffff"
    android:layout_alignBottom="@+id/popup_bottom_ll" />

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/popup_top_iv"
    android:src="@drawable/popup_0_top"
    android:scaleType="fitCenter"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp" />

...

所以......我想知道你是否知道:

<强>1。什么可能导致此dialogFragment出现内存泄漏错误?

<强>2。 android 中有什么好的做法可以避免内存泄漏吗?

例如对于该文件,我应该调用

imageView.setImageDrawable(null);

在每个 Imageview 上,当弹出窗口关闭/Activity 关闭时 (R.drawable.* ) ?或者只是当我从 URL(例如使用 Glide)动态加载图像时?

我应该始终将图像大小调整为 Imageview 的尺寸吗? fragment/Activity 关闭后我到底需要清理什么?

你觉得怎么样?

最佳答案

经过一番挖掘后,我得出了以下解决方案:

https://medium.com/@multidots/glide-vs-picasso-930eed42b81d

  • 切勿将上下文 Activity 引用保持为静态。绝不! 否则,即使您需要,您的引用资料可能仍然存在。

  • 避免保留 ImageView 的引用。销毁 View 时回收/清除内部位图。

  • 如果您需要上下文的引用,请使用context.getApplicationContext();

  • 不要将 context.getApplicationContext(); 用于 LayoutInflater.from(context);,而是直接使用上下文。

(如果您需要膨胀 View 或需要某些内容的上下文)。 如果您使用 context.getApplicationContext() 动态创建 View ,可能会导致一些设计问题。

  • 如果您遇到内存不足错误,请考虑压缩您的图像!它实际上为我节省了很多内存不足错误。您可以使用https://tinypng.com/
  • 默认情况下,不要将图像放入 drawable-xhdpi 文件夹中,而是放入 drawable-mdpi 文件夹中,因为 drawable-mdpi 是默认文件夹一个。

因此,对于我的问题,我只需要压缩图像并将它们放回drawable-mdpi文件夹中!现在一切都好!

关于Android - 避免内存泄漏的良好实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41252385/

相关文章:

android - 在 Android 应用程序中与服务器同步

jquery - JQM phonegap.js android 应用程序破坏了应用程序 View ?为什么?

java - 快速排序-枢轴选择策略如何影响快速排序的整体Big-oh行为?

jquery - 为什么 CSS Hover 在 IE8 中很慢?

android - 使用具有大量透明区域的 png 时内存消耗大幅上升

perl - 什么会导致 Perl 转储核心?

java - 错误 ebay [CDATA[ 应用程序内部错误 ]]

java - 更新 SQLite 数据

mysql - 定期更换正在运行的 MySQL 数据库

linux - meminfo 中的 DirectMapXX 字段表示什么?