您知道避免内存泄漏的良好做法吗?
我目前正在开发一个几乎没有内存泄漏的应用程序,我很难修复它,主要是因为我不知道我需要采取哪些习惯才能避免它们。
例如,目前我遇到这个问题
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://github.com/square/leakcanary )
- 或者Android studio的内存监视器:https://developer.android.com/studio/profile/am-memory.html
- 使用库加载图像,例如 Picasso 或 Glide。我个人更喜欢 Glide。
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/