Android 內存優化

軟件 文本編輯器 科技 PHP愛好者 2017-04-18


Android 內存優化

優化方案

檢查使用多少內存

每個 APP 的堆(heap)內存大小有硬性限制,如果您的 APP 已達到堆內存限制,並嘗試分配更多的內存,系統會拋出 OutOfMemoryError 。為了避免 OOM ,您可以查詢當前設備有多少堆空間,可以通過調用系統 getMemoryInfo() 查詢,返回 一個ActivityManager.MemoryInfo 對象,它提供該設備當前存儲器的狀態信息,包括可用的存儲器,總存儲器,和低於該閾值存儲器。

private void getMemoryInfo() {

ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();

activityManager.getMemoryInfo(memoryInfo);

LogUtil.d("totalMem=" + memoryInfo.totalMem + ",availMem=" + memoryInfo.availMem);

if (!memoryInfo.lowMemory) {

// 運行在低內存環境

}

}

當界面不可見時釋放內存

實現 ComponentCallbacks2 API 中 onTrimMemory()) ,當回調參數 level 為 TRIM_MEMORY_UI_HIDDEN ,是用戶點擊了Home鍵或者Back鍵退出應用,所有UI界面被隱藏,這時候應該釋放一些不可見的時候非必須的資源。

public class MainActivity extends AppCompatActivity

implements ComponentCallbacks2 {

// Other activity code ...

/**

* Release memory when the UI becomes hidden or when system resources become low.

* @param level the memory-related event that was raised.

*/

public void onTrimMemory(int level) {

// Determine which lifecycle or system event was raised.

switch (level) {

case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:

/*

Release any UI objects that currently hold memory.

The user interface has moved to the background.

*/

break;

case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:

case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:

case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:

/*

Release any memory that your app doesn't need to run.

The device is running low on memory while the app is running.

The event raised indicates the severity of the memory-related event.

If the event is TRIM_MEMORY_RUNNING_CRITICAL, then the system will

begin killing background processes.

*/

break;

case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:

case ComponentCallbacks2.TRIM_MEMORY_MODERATE:

case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:

/*

Release as much memory as the process can.

The app is on the LRU list and the system is running low on memory.

The event raised indicates where the app sits within the LRU list.

If the event is TRIM_MEMORY_COMPLETE, the process will be one of

the first to be terminated.

*/

break;

default:

/*

Release any non-critical data structures.

The app received an unrecognized memory level value

from the system. Treat this as a generic low-memory message.

*/

break;

}

}

}

該 onTrimMemory() 回調是在搭載Android 4.0(API級別14)加入。對於早期版本,您可以使用 onLowMemory() 回調作為舊版本的回調,這大致相當於 TRIM_MEMORY_COMPLETE事件。

謹慎使用服務

離開了 APP 還在運行服務是最糟糕的內存管理錯誤之一,當 APP 處在後臺,我們應該停止服務,除非它需要運行的任務。我們可以使用 JobScheduler 替代實現,JobScheduler 把一些不是特別緊急的任務放到更合適的時機批量處理。如果必須使用一個服務,最佳方法是使用 IntentService ,限制服務壽命,所有請求處理完成後,IntentService 會自動停止。

使用優化的數據容器

考慮使用優化過數據的容器 SparseArray / SparseBooleanArray / LongSparseArray 代替 HashMap 等傳統數據結構,通用 HashMap 的實現可以說是相當低效的內存,因為它需要為每個映射一個單獨的條目對象。

避免在 Android 上使用枚舉

枚舉往往需要兩倍多的內存,靜態常量更多,我們應該嚴格避免在 Android 上使用枚舉。

使用 nano protobufs 序列化數據

Protocol buffers 是一個語言中立,平臺中立的,可擴展的機制,由谷歌進行序列化結構化數據,類似於 XML 設計的,但是更小,更快,更簡單。如果需要為您的數據序列化與協議化,建議使用 nano protobufs。

避免內存流失

內存流失可能會導致出現大量的 GC 事件,如自定義組件的 onDraw() ,避免大量創建臨時對象,比如 String ,以免頻繁觸發 GC。GC 事件通常不影響您的 APP 的性能,然而在很短的時間段,發生許多垃圾收集事件可以快速地吃了您的幀時間,系統上時間的都花費在 GC ,就有很少時間做其他的東西像渲染或音頻流。

使用ProGuard來剔除不需要的代碼

使用 ProGuard 來剔除不需要的代碼,移除任何冗餘的,不必要的,或臃腫的組件,資源或庫完善 APP 的內存消耗。

降低整體尺寸APK

您可以通過減少 APP 的整體規模顯著減少 APP 的內存使用情況。文章:Android APK瘦身實踐

優化佈局層次

通過優化視圖層次結構,以減少重疊的 UI 對象的數量來提高性能。文章:Android 渲染優化

使用 Dagger 2依賴注入

依賴注入框架可以簡化您寫的代碼,並提供一個自適應環境測試和便於其他配置的更改。如果打算在您的 APP 使用依賴注入框架,可以考慮用 Dagger 2 ,Dagger 不使用反射掃描 APP 的代碼,Dagger 是靜態的,意味著它編譯時不需要佔用運行 Android 應用或內存的使用。

請小心使用外部庫

外部庫的代碼往往是未針對移動環境下編寫並用於工作在移動客戶端上時,可能是低效的。當您決定使用外部庫,您可能需要優化該庫為移動設備。

避免Bitmap浪費

Bitmap是內存消耗的大頭,當使用時要及時回收。另外配置:

inSampleSize:縮放比例,圖片按需加載,避免不必要的大圖載入。

decode format:解碼格式,選擇ARGB_8888/RBG_565/ARGB_4444/ALPHA_8,存在很大差異。

Cursor關閉

如查詢數據庫的操作,使用到Cursor,也要對Cursor對象及時關閉。

監聽器的註銷

Android程序裡面存在很多需要register與unregister的監聽器,手動add的listener,需要記得及時remove這個listener。

相關推薦

推薦中...