【发布时间】:2018-10-22 07:57:41
【问题描述】:
我从 A 启动活动 B。然后我销毁 B 活动。然后我再次从 A 启动活动 B,依此类推。当我这样做时,我的堆大小每次都会增加约 0.5 MB。我使用 android profiler,当活动 B 被杀死后,我强制从 android profiler 进行垃圾收集。问题是 GB 之后的堆大小没有减少。我的代码也是上面的,是来自 android studio 的通用代码。如果有问题,我如何通过 Heap Dump 看到它?它显示了许多分配,我不明白。
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(newIntent(MainActivity.this,Main2Activity.class));
}
});
}
MainActivity2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
检查日志堆:
Log.d("tag", "debug. =================================");
Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)");
Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory() / 1048576.0)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory() / 1048576.0)) + "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory() / 1048576.0)) + "MB free)");
【问题讨论】:
-
@UmangBurman Nope.Irrelevant
标签: android heap-memory