检查并避免在应用程序内存泄漏 [英] Check and Avoid Memory Leaks in Application

查看:193
本文介绍了检查并避免在应用程序内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我做了一个项目,我现在面临的主要问题是在内存泄漏在应用程序的(泄密,意思是你保持一个参考的活性,从而$ P $从收集它pventing的GC)

So, I am done with a project, now the main issue I am facing is the Memory Leakage in the application ("leak" meaning you keep a reference to an activity, thus preventing the GC from collecting it)

一些我发现的情况下,其中存储器泄漏发生是:

Some of the cases I found, where the memory leakage occurs are:

这是因为长寿命参考活动场景。

It occurs because of long lived reference to the activity context.

它的一个很好的例子,我发现这里

A very good example of it i found here,

private static Drawable sBackground;

@Override
protected void onCreate(Bundle state) {
  super.onCreate(state);

  TextView label = new TextView(this);
  label.setText("Leaks are bad");

  if (sBackground == null) {
    sBackground = getDrawable(R.drawable.large_bitmap);
  }
  label.setBackgroundDrawable(sBackground);

  setContentView(label);
}

这里的问题是与私有静态绘制对象sBackground; 静态绘制对象与活动的情况下创建的,所以在这种情况下,有一个静态引用一个可绘制引用活动,这就是为什么有泄漏。只要是引用存在,该活动将被保存在内存中,泄露其所有的意见。

here the problem is with private static Drawable sBackground; The static Drawable is created with the Activity as the context, so in THAT case, there's a static reference to a Drawable that references the Activity, and that's why there's a leak. As long as that reference exists, the Activity will be kept in memory, leaking all of its views.

这提请注意第二种情况是,当屏幕方向改变。当屏幕方向改变,系统将默认情况下,破坏了目前的活动,并创建一个新的,而preserving其状态。在此过程中,Android将重新加载从资源的应用程序的UI。现在想象一下你写的跟你不希望加载的每一个旋转的大位的应用程序。

The second case that draws the attention is when the screen orientation changes. When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state. In doing so, Android will reload the application’s UI from the resources. Now imagine you wrote an application with a large bitmap that you don’t want to load on every rotation.

这将导致大量内存泄露的可能有大的位图来加载。

This will result in a lot of memory leakage as there could be large bitmaps to load.

第三种情况,我发现是参考活动场景。这也导致内存泄漏。

The third case, I found was the reference to the activity context. It also results in memory leakage.

我不知道是否有任何简单的方法来避免此类内存泄漏 发生。或者,如果有可能的工具来检查和删除那些 从应用程序的内存泄漏。

I wonder if there is any easy way to avoid such memory leakages from happening. or if there could be tool to check and remove those memory leakages from the application.

推荐答案

您可以使用像traceview或内存分析工具来检查内存泄漏

you can use tools like traceview or memory analyzer to check for memory leaks

<一个href="http://developer.android.com/tools/help/traceview.html">http://developer.android.com/tools/help/traceview.html

<一个href="http://kohlerm.blogspot.com/2009/04/analyzing-memory-usage-off-your-android.html">http://kohlerm.blogspot.com/2009/04/analyzing-memory-usage-off-your-android.html

下面是一些文章中,我发现的有用

Here are some articles i found helpful

<一个href="http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html">http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html

<一个href="http://vahidmlj.blogspot.com/2012/10/android-memory-leak-on-screen-rotation.html">http://vahidmlj.blogspot.com/2012/10/android-memory-leak-on-screen-rotation.html

这篇关于检查并避免在应用程序内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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