背景信息
我正在建立這個應用程序幾個月了,現在差不多完成了。但幾天後,我看到內存與日誌一樣內存分配在空活動
D/dalvikvm(25624): GC_CONCURRENT freed 1782K, 17% free 22647K/27143K, paused 15ms+16ms, total 80ms
正如你可以看到我有很多內存分配。 因此,在搜索/閱讀了幾天並使用MAT找到我的泄漏之後,我決定剝離應用程序,並通過新項目中的活動構建活動以找出問題所在。
問題
所以現在我有一個新的項目,我打開一個單一的活動。 我已經添加了一些圖片到項目中,但沒有被使用。 我已將一個函數添加到活動中,以便我可以在日誌中看到已用內存。
代碼
@Override 公共無效的onCreate(捆綁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);
}
結果
調試。 START ================================= MAIN debug.heap native:分配了2,41MB的2,59MB( 0,18MB免費) debug.memory:分配:12,00MB的64,00MB(0,00MB免費) maxMemory:67108864 memoryClass:64 debug。 END ================================= MAIN
所以現在我只想知道什麼是導致這種內存使用。 這是某種默認的內存,每個應用程序啓動?
感謝您的幫助。
UPDATE [30-12-2012]
仍在研究這個問題,但有時我想這是一個非問題。其原因是,當我使用AVD(HEAPSIZE:24MB),我看到一共有不同的結果:
相比,當我調試我的手機(銀河S3)上的應用程序:
您是否已經檢查過[內存管理上的搜索引擎優化](http://youtu.be/_CruQY55HOk?t=19m37s),關於應用旋轉和保留參考活動的部分? – ggpuz
是的,我所有的活動都有屬性android:screenOrientation =「portrait」。但奇怪的部分是AVD和使用它在手機之間的堆轉儲的巨大差異.. – RClemens
http://stackoverflow.com/questions/12868366/android-x86-emulator-not-respecting-vm-heapsize – Shekhar