负一屏内存优化
负一屏属于launcher的一个左屏,实际上是一个viewGroup。负一屏在launcher编辑模式下有一个开关复制控制负一屏的显示和不显示。
当负一屏不显示时,我们希望能够释放掉负一屏所有的资源和内存占用。但是这时候launcher(activity)并没有停止运行(没有onDestory)。所以许多监听和对象都需要手动释放。
Studio自带的内存分析工具:Android Studio - HPROF文件查看和分析工具 - 新感觉 - 博客园
负一屏内存释放相关:
负一屏最外层的OnDestory
public void onDestroy() {
Runnable r = new Runnable() {
@Override
public void run() {
LongArrayMap<Service> removedService = new LongArrayMap<>();
for (Service service : sCreateServices) {
removedService.put(service.type, service);
}
for (Service service : removedService) {
service.onDestroy();
service.destoryCustomPageManager();
}
mStringSort = "";
removedService.clear();
sCreateServices.clear();
sIntelligenceServices.clear();
sAllService.clear();
sXyService.clear();
mSortList.clear();
mCacheCardList.clear();
sCreateServices=null;
sIntelligenceServices=null;
sAllService=null;
sXyService=null;
mSortList=null;
mCacheCardList=null;
customPageInterface=null;
mCustomPageProfile=null;
if (mHandler!=null){
mHandler.removeCallbacksAndMessages(null);
mHandler=null;
}
if (AccountCustomManager.getInstance(null)!=null){
AccountCustomManager.getInstance(null).onDestroy();
}
if (SearchManager.getInstance(null)!=null){
SearchManager.getInstance(null).onDestroy();
}
if (mCustomPageView!=null){
mCustomPageView.removeNewsView();
if (mCustomPageView.getNewsView()!=null){
mCustomPageView.getNewsView().removeView();
}
mCustomPageView.removeAllViews();
mCustomPageView=null;
}
CLog cLog = CLog.getInstance();
if (cLog!=null){
cLog.onDestroy();
}
ThreadPool.onDestroy();
mCustomPageManager=null;
}
};
runOnMainThread(r);
}
AccountCustomManager的onDestory:
public void onDestroy() {
Log.v(TAG, "onDestroy");
recycleBmpRes();
if (mReceiver != null) {
context.getApplicationContext().unregisterReceiver(mReceiver);
mReceiver = null;
}
// if (mAuthenticator!=null){
unBindAuthenService();
// }
if (mHandler!=null){
mHandler.removeCallbacksAndMessages(null);
mHandler=null;
}
mAccountCustomManager=null;
}
SearchManager的OnDestory:
public void onDestroy(){
unRegisterSearchDataListener();
if (searchLoaderTask!=null){
searchLoaderTask.onDestroy();
searchLoaderTask=null;
}
adapter=null;
if (sLoad!=null){
sLoad.removeCallbacksAndMessages(null);
sLoad=null;
}
if (sLoadThread!=null){
sLoadThread=null;
}
context=null;
if (fixedThreadPool!=null){
fixedThreadPool.shutdown();
fixedThreadPool=null;
}
ContactsHelp.release();
SmsHelp.release();
LocalFileHelp.release();
mySearchInterface=null;
mSearchManager=null;
if (contactsRunnable!=null){
contactsRunnable.onDestroy();
contactsRunnable=null;
}
if (smsMmsRunnable!=null){
smsMmsRunnable.onDestroy();
smsMmsRunnable=null;
}
if (pocketRunnable!=null){
pocketRunnable.onDestroy();
pocketRunnable=null;
}
if (calendarRunnable!=null){
calendarRunnable.onDestroy();
calendarRunnable=null;
}
if (galleryRunnable!=null){
galleryRunnable.onDestroy();
galleryRunnable=null;
}
if (fileRunnable!=null){
fileRunnable.onDestroy();
fileRunnable=null;
}
if (settingRunnable!=null){
settingRunnable.onDestroy();
settingRunnable=null;
}
if (noteRunnable!=null){
noteRunnable.onDestroy();
noteRunnable=null;
}
searchText=null;
searchStrLowerCase=null;
noSearchStr=null;
TAG=null;
SearchRunnable.clearHandler();
ContactLocaleUtils contactLocaleUtils = ContactLocaleUtils.getContactLocaleUtils();
if (contactLocaleUtils!=null){
contactLocaleUtils.onDestroy();
}
HanziToPinyin hanziToPinyin = HanziToPinyin.getHanziToPinyin();
if (hanziToPinyin!=null){
hanziToPinyin.onDestroy();
}
MyBaseItem.onDestroy();
}
以上主要释放了列表(mSortList.clear();)、线程池(fixedThreadPool.shutdown();)Handler(mHandler.removeCallbacksAndMessages(null))、广播(unregisterReceiver(mReceiver))、service(context.unbindService(conn))、