????檢測內(nèi)存情況
????檢查你應(yīng)該使用多少內(nèi)存:
public void doSomethingMemoryIntensive() {
// Before doing something that requires a lot of memory,
// check to see whether the device is in a low memory state.
ActivityManager.MemoryInfo memoryInfo = getAvailableMemory();
if (!memoryInfo.lowMemory) {
// Do memory intensive work ...
}
}
// Get a MemoryInfo object for the device's current memory status.
private ActivityManager.MemoryInfo getAvailableMemory() {
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo;
}
????實(shí)現(xiàn)ComponentCallbacks2接口響應(yīng)事件進(jìn)行內(nèi)存釋放
import android.content.ComponentCallbacks2;
// Other import statements ...
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;
}
}
}
????應(yīng)用切換
當(dāng)用戶在應(yīng)用程序之間切換時(shí),Android會保留不是前臺的應(yīng)用程序 - 即用戶不可見或在最近最少使<br/>
(LRU)緩存中運(yùn)行音樂播放等前臺服務(wù)。例如,當(dāng)用戶首次啟動應(yīng)用程序時(shí),會為其創(chuàng)建一個(gè)進(jìn)程;但是<br/>
當(dāng)用戶離開應(yīng)用程序時(shí),該進(jìn)程不會退出。系統(tǒng)會保持進(jìn)程緩存。如果用戶稍后返回應(yīng)用程序,系統(tǒng)將重<br/>
新使用該過程,從而使應(yīng)用程序切換更快。 如果您的應(yīng)用程序具有緩存進(jìn)程并且它保留了當(dāng)前不需要的<br/>
內(nèi)存,那么即使用戶未使用它,您的應(yīng)用程序也會影響系統(tǒng)的整體性能。由于系統(tǒng)內(nèi)存不足,因此從最近<br/>
最少使用的進(jìn)程開始,它會終止LRU緩存中的進(jìn)程。系統(tǒng)還會考慮保留最多內(nèi)存的進(jìn)程,并可以終止它們以<br/>
釋放RAM。
????謹(jǐn)慎使用服務(wù)
- 當(dāng)服務(wù)不需要運(yùn)行時(shí),你應(yīng)該及時(shí)關(guān)閉它
- 使用JobScheduler 替代服務(wù)
- 使用IntentService
????高效的內(nèi)存容器
- [Java 常用數(shù)據(jù)結(jié)構(gòu)](https://mp.weixin.qq.com/s/5BO92rFoSh4sd1ZlgDk5zw)
- [Autoboxing]自動裝箱
- 常量替代枚舉類型 (13倍左右)
- Android優(yōu)化過容器SparseArray Family、ArrayMaps(數(shù)據(jù)量 < 1000)
????減少不必要的代碼抽象以減輕代碼量及方法數(shù)
????數(shù)據(jù)序列化:
Protocol buffers > Parcelable(AS Plugin Parcelable) > Serializable
????使用Dagger2 注解框架
使用反射的其他依賴注入框架傾向于通過掃描代碼注釋來初始化進(jìn)程。此過程可能需要更多的CPU周期和RAM,并且可能會在應(yīng)用程序啟動時(shí)導(dǎo)致明顯的延遲.
????謹(jǐn)慎使用外部庫、多數(shù)外部庫不是針對移動端編寫的、需要進(jìn)行優(yōu)化后使用
????注意內(nèi)存泄露、
尤其是Static對象、持有View、Context對象的異步回調(diào)、使用ApplicationContext
????刪除不必要的資源、第三方庫減小內(nèi)存消耗
????使用多進(jìn)程
使用Java引用,而使用C++內(nèi)存分配來分配的內(nèi)存,當(dāng)Java引用不可達(dá)時(shí),這不部分內(nèi)存可能引起泄露<br/>
可以使用Java PhantomReference引用
[推薦閱讀]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。