gandarez
gandarez

Reputation: 2662

High memory usage on Android Application

I have a little application, this application has seven activities, basically 6 are layouts with images and one is a MapActivity, among of 5 layouts one has a ArrayList of a internal class and each time that I use my application the memory grows to 50mb, 70mb, 111mb... I tried to call Garbage Collector but I didn't receive the expected result.

public class GC 
{    
    public static final Runtime runtime = Runtime.getRuntime();

    public static void Free()
    {
        runtime.gc();
    }
}

Upvotes: 0

Views: 2075

Answers (1)

YumeApps
YumeApps

Reputation: 118

Have a look here: Avoid memory leaks on Android

  1. Make sure you are properly closing your other activities (this.finish).
  2. You need to remove the callbacks to images otherwise you will leak memory.

    private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
    

Call it with the view's root (i.e. unbindDrawables(findViewById(R.id.xml_layout_root));

Upvotes: 1

Related Questions