打开键盘时本机堆内存大小增加 - android

时间:2013-07-23 04:25:03

标签: java android

我已在我的版面上加载了3个ListView和一个EditText

当我触摸EditText时,键盘会打开。在那一刻,我使用以下代码检查内存大小:

Runtime rt = Runtime.getRuntime();
long vmAlloc =  rt.totalMemory() - rt.freeMemory();
long nativeAlloc = Debug.getNativeHeapAllocatedSize();

Log.d("Memory",className+"Maximum   : "+formatMemoeryText(rt.maxMemory()));

Log.d("Memory",className+"VM Heap   : "+formatMemoeryText(rt.totalMemory()));
Log.d("Memory",className+"NativHeap : "+formatMemoeryText(Debug.getNativeHeapSize()));

Log.d("Memory",className+"VM    (A) : "+formatMemoeryText(vmAlloc));
Log.d("Memory",className+"Nati(A)   : "+formatMemoeryText(nativeAlloc));

Log.d("Memory",className+"TotalAll  : "+formatMemoeryText(nativeAlloc+vmAlloc));
Log.d("Memory", "----------------------------------------------------");

每次键盘打开时,本机堆大小都会增加。最终应用程序将强行关闭。

我试过了:

System.gc();
Runtime.getRuntime().gc();

但它只减少了大约0.5-1MB。下次打开键盘时,它会增加20-30MB。

如何阻止应用关闭或清除其本机堆内存?

2 个答案:

答案 0 :(得分:3)

由于键盘导致内存不足,导致您的应用无法崩溃,当您通过对焦请求键盘时,程序中必定存在导致内存泄漏的内容在输入字段或以编程方式它分配内存但不是太大导致程序崩溃,请检查您的代码,并发现内存泄漏。

答案 1 :(得分:0)

确保在打开Android密钥卡时未更改设备方向。因为如果是,那么你的活动将被重新整理,并且如果你的任何对象持有你活动的参考,可能会导致内存泄漏。