【发布时间】:2012-12-27 14:05:47
【问题描述】:
背景信息
我正在构建这个应用程序几个月,它几乎完成了。但是几天后,我看到内存随着日志的增加而增加,就像
D/dalvikvm(25624): GC_CONCURRENT freed 1782K, 17% free 22647K/27143K, paused 15ms+16ms, total 80ms
如您所见,我分配了很多内存。 因此,在搜索/阅读了几天并使用 MAT 查找我的漏洞之后,我决定剥离应用程序并在一个新项目中逐个活动地构建它,以找出问题所在。
问题
所以现在我有一个新项目,我只打开一个活动。
我在项目中添加了一些图像,但它们没有被使用。
我已经在 Activity 中添加了一个函数,这样我就可以在日志中看到使用的内存。
代码
@覆盖
公共无效 onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("HOME", "START");
ShowMemoryStats("MAIN");
}
public void ShowMemoryStats(String activityName) {
Double allocated = new Double(Debug.getNativeHeapAllocatedSize())
/ new Double((1048576));
Double available = new Double(Debug.getNativeHeapSize()) / 1048576.0;
Double free = new Double(Debug.getNativeHeapFreeSize()) / 1048576.0;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
df.setMinimumFractionDigits(2);
Log.d("gettings", "debug. START ================================= "
+ activityName);
Log.d("gettings",
"debug.heap native: allocated " + df.format(allocated)
+ "MB of " + df.format(available) + "MB ("
+ df.format(free) + "MB free)");
Log.d("gettings",
"debug.memory: allocated: "
+ df.format(new Double(Runtime.getRuntime()
.totalMemory() / 1048576))
+ "MB of "
+ df.format(new Double(
Runtime.getRuntime().maxMemory() / 1048576))
+ "MB ("
+ df.format(new Double(Runtime.getRuntime()
.freeMemory() / 1048576)) + "MB free)");
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
Log.v("onCreate", "maxMemory:" + Long.toString(maxMemory));
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
Log.v("onCreate", "memoryClass:" + Integer.toString(memoryClass));
Log.d("gettings", "debug. END ================================= "
+ activityName);
}
结果
调试。开始 =================================== 主
debug.heap native:分配 2,41MB 的 2,59MB(0,18MB 空闲)
debug.memory:已分配:12,00MB 的 64,00MB(0,00MB 可用)
最大内存:67108864
内存等级:64
调试。 END ==================================主要
所以现在我只想知道是什么导致了这种内存使用。 这是每个应用程序在启动时都会获得的某种默认内存吗?
感谢您的帮助。
更新 [30-12-2012]
仍在解决这个问题,但有时我认为这不是问题。原因是当我使用 AVD (Heapsize: 24MB) 时,我看到了完全不同的结果:
与我在手机 (Galaxy S3) 上调试应用程序时相比:
【问题讨论】:
-
您是否已经检查了googles io on memory management,关于应用程序旋转和保持对活动的引用的部分?
-
是的,我所有的活动都有属性 android:screenOrientation="portrait"。但奇怪的部分是 AVD 和在手机上使用它之间的堆转储的巨大差异..