5

While investigating memory issues in our application, it turns out that if the application Activity is a MapActivity, the first instance of it won't be finalized. Leading to other memory leak such as the view passed to setContentView.

Does anyone notice that before?

Here is the testing code showing that "MainActivity : 1" is not finalized whereas it is if MainActivity inherits from Activity.

To test, one needs to change device or emulator orientation many times.


import com.google.android.maps.MapActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends MapActivity {
  private static final String  defaultTag          = "MA";

  private static final boolean isDebugModeActivate = true;
  private static final boolean isClassTagDisplayed = false;
  private static final boolean isWebModeActivate   = false;

  static public void d(Object thiso, String message)
  {
      String tag = defaultTag + (isClassTagDisplayed == true ? "_" + thiso.getClass().getSimpleName() : "");
      message = (isClassTagDisplayed == false ? thiso.getClass().getSimpleName() + " : " : "") + message;
      Log.d(tag, message);
  }

  public MainActivity()
  {
    counter++;
    uid++;
    id = uid;
    d(this, id + " tst constructor (" + counter + ")");
  }
  private static int counter = 0;
  private static int uid = 0;
  private final int id;

  protected void finalize() throws Throwable
  {
    counter--;
    d(this, id + " tst finalize (" +counter + ") ");
    super.finalize();
  }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected boolean isRouteDisplayed()
    {
      return false;
    }
}

Thank you, David

david
  • 1,311
  • 12
  • 32
  • You are not the only one with leak problems on android: http://stackoverflow.com/questions/5111139/why-does-my-code-leak-when-switching-activities – Tyler Zale Feb 24 '11 at 22:23
  • http://stackoverflow.com/questions/4218359/my-simple-listview-app-is-leaking-memory-what-am-i-doing-wrong – Tyler Zale Feb 24 '11 at 22:23
  • @Tyler, yes I know. Since I didn't find this specific memory leak, I guess it is good to reference it. At least this could save hours of investigations. – david Feb 24 '11 at 22:47
  • @david: Good observation! I see it as worse than that though. I pasted your code into my app, started and stopped 4 times (going through onDestroy for sure). 4 constructors logged - no finalizers! I forced a GC via DDMS and got finalizer(3) logged, that's all. No matter how many times I force GC, no more finalizers. – NickT Feb 24 '11 at 23:11
  • @NickT, yes it is very bad on low-end device. To reduce the leak amount, when the MapActivity.onDestory is called, I remove all the children of the view passed to MapActivity.setContentView. This makes a potential OutOfMemory error happening later. – david Feb 24 '11 at 23:20
  • A solution may be using an ActivityGroup and add the MapActivity to it and try LocalActivities.removeAllActivities like it is described in [link](http://stackoverflow.com/questions/5133298/destroy-mapview) – david Apr 20 '11 at 10:38

1 Answers1

-1

Perhaps you should exchange notes with NickT here

Community
  • 1
  • 1
John J Smith
  • 11,435
  • 9
  • 53
  • 72