第一个 MapActivity 实例总是泄漏吗?

发布于 2024-10-19 03:26:57 字数 1654 浏览 7 评论 0原文

在研究应用程序中的内存问题时,发现如果应用程序 Activity 是 MapActivity,则它的第一个实例将不会最终确定。导致其他内存泄漏,例如传递给setContentView的视图。

之前有人注意到吗?

下面的测试代码显示“MainActivity : 1”尚未最终确定,而如果 MainActivity 继承自 Activity,则会最终确定。

为了进行测试,需要多次更改设备或模拟器的方向。


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;
    }
}

谢谢你, 大卫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-10-26 03:26:57

也许您应该在此处与 NickT 交换意见

Perhaps you should exchange notes with NickT here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文