开发者

Memory leaks in android?

This is something that I have noticed in Android. If you restart the phone, you will find that the amount of free memory is more [say 190 mb in the case of HTC Wildfire]. As time passes by, the amount of free memory keeps reducing.

[This free memory I'm refering to is a statistic that i get from a TaskKiller app. I dont trust this app to kill my other applications, but use it to monitor my memory resources.]

Is it something wrong with the app that I am using or are there memory issues in android [fo开发者_C百科r want of a better term].

And if yes, How I as a developer can stop this from happening.


I don't regard them as "issues" since Android is made in such a way that applications keep working in background. call it multi-tasking. and these applications cost you memory. ( I am assuming that you're referring to the RAM size)

When the phone starts, almost all of the apps are sleeping. slowly, background services such as sync and else start up, and they start occupying memory.

That's why you see a drop in free memory. I don't think there's something to worry about, since Android takes care of Memory Management very well.

Although, as a developer, you should create such an app which occupies as less memory as possible.


Sheikh Aman posted as I was typing this and covers a lot of what I was going to say - in particular about how any multi-tasking, multi-process system generally starts with the minimum and over time, due to user inter-action other components will be loaded up thus reducing free RAM etc.

As for actual memory leaks - it is possible in all systems for poor code to cause them so try to avoid apps which seem to display this behaviour. To avoid causing them yourself, this is a nice article which is worth reading...Avoiding Memory Leaks


I don't know if you are having any trouble with this in your app, but I have created a drop in solution that fixes all the android memory leak issues with standard android classes: http://code.google.com/p/android/issues/detail?id=8488#c51

public abstract class BetterActivity extends Activity
{
  @Override
  protected void onResume()
  {
    System.gc();
    super.onResume();
  }

  @Override
  protected void onPause()
  {
    super.onPause();
    System.gc();
  }

  @Override
  public void setContentView(int layoutResID)
  {
    ViewGroup mainView = (ViewGroup)
      LayoutInflater.from(this).inflate(layoutResID, null);

    setContentView(mainView);
  }

  @Override
  public void setContentView(View view)
  {
    super.setContentView(view);

    m_contentView = (ViewGroup)view;
  }

  @Override
  public void setContentView(View view, LayoutParams params)
  {
    super.setContentView(view, params);

    m_contentView = (ViewGroup)view;
  }

  @Override
  protected void onDestroy()
  {
    super.onDestroy();

    // Fixes android memory  issue 8488 :
    // http://code.google.com/p/android/issues/detail?id=8488
    nullViewDrawablesRecursive(m_contentView);

    m_contentView = null;
    System.gc();
  }

  private void nullViewDrawablesRecursive(View view)
  {
    if(view != null)
    {
      try
      {
        ViewGroup viewGroup = (ViewGroup)view;

        int childCount = viewGroup.getChildCount();
        for(int index = 0; index < childCount; index++)
        {
          View child = viewGroup.getChildAt(index);
          nullViewDrawablesRecursive(child);
        }
      }
      catch(Exception e)
      {          
      }

      nullViewDrawable(view);
    }    
  }

  private void nullViewDrawable(View view)
  {
    try
    {
      view.setBackgroundDrawable(null);
    }
    catch(Exception e)
    {          
    }

    try
    {
      ImageView imageView = (ImageView)view;
      imageView.setImageDrawable(null);
      imageView.setBackgroundDrawable(null);
    }
    catch(Exception e)
    {          
    }
  }

  // The top level content view.
  private ViewGroup m_contentView = null;
}


Android has garbage collection so at this level these aren't leaks. You don't need to worry.


It could be that you have an app that is leaking memory, but more generally, for performance reasons modern operating systems such as Android will tend to use all available memory as cache memory, releasing it when an app requires it.

  • If you are running out of memory and apps are unable to start, then you may have a leak
  • If a particular app is using what appears to be too much memory, then you may have a problem

So long as you can still run the apps you need to, I wouldn't worry since Android is doing it's job.

As a developer, in theory, it's simple - don't use more resources than you need, and release them when you are finished with them. In practice however, we have varying degrees of success.

The Application Resources section of the dev guide is a good place to start, and there's usually good articles on the blog.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜