understanding onTrimMemory( int level )

前端 未结 3 1214
死守一世寂寞
死守一世寂寞 2020-12-12 11:16

I recently read this article on Managing Your App\'s Memory,I strongly suggest to read it if you are an AndroidDev and never did.

There are lots of good pra

3条回答
  •  臣服心动
    2020-12-12 12:04

    Sample Implementation

    public class AppContext extends Application {
    //This my introduce OutOfMemoryException if you don't handle register and removal quiet well, better to replace it with weak reference   
    private static List memInfoList = new ArrayList();
    
    public static abstract interface IMemoryInfo {
            public void goodTimeToReleaseMemory();
        }
    
    @Override
        public void onTrimMemory(int level) {
            super.onTrimMemory(level);
    //don't compare with == as intermediate stages also can be reported, always better to check >= or <=
                if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW) {
                    try {
                    // Activity at the front will get earliest than activity at the
                    // back
                    for (int i = memInfoList.size() - 1; i >= 0; i--) {
                        try {
                            memInfoList.get(i).goodTimeToReleaseMemory();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
    /**
         * 
         * @param implementor
         *            interested listening in memory events
         */
        public static void registerMemoryListener(IMemoryInfo implementor) {
            memInfoList.add(implementor);
        }
    
        public static void unregisterMemoryListener(IMemoryInfo implementor) {
            memInfoList.remove(implementor);
        }
    }
    

    public class ActivityParent extends Activity implements AppContext.IMemoryInfo {
    
        protected ActivityParent child;
    
    
    @Override
        protected void onStop() {
            super.onStop();
            try {
                if (child != null)
                    AppContext.unregisterMemoryListener(child);
            } catch (Exception e) {
    
            }
        }
    }
    

    public class ActivityChild extends ActivityParent {
    @Override
        protected void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);
            child = this;
        }
    
            /---move following onResume() in parent as following eg:
    /*
    *@Override
    *       protected void onResume() {     
    *           super.onResume();
    *           if(null != child){
    *           AppContext.registerMemoryListener(this);
    *           }
    *       }
    */
            @Override
            protected void onResume() {     
                super.onResume();
                AppContext.registerMemoryListener(this);
            }
    
    @Override
    public void goodTimeToReleaseMemory() { 
        super.goodTimeToReleaseMemory();
    //remove your Cache etc here
    }
    //--NO Need because parent implementation will be called first, just for the sake of clarity 
    @Override
        protected void onStop() {
            super.onStop();
            try {
                if (null != child)
                    AppContext.unregisterMemoryListener(child);
            } catch (Exception e) {
    
            }
        }
    

    More Info:

    When your app is running: TRIM_MEMORY_RUNNING_MODERATE The device is beginning to run low on memory. Your app is running and not killable.

    TRIM_MEMORY_RUNNING_LOW The device is running much lower on memory. Your app is running and not killable, but please release unused resources to improve system performance (which directly impacts your app's performance).

    TRIM_MEMORY_RUNNING_CRITICAL The device is running extremely low on memory. Your app is not yet considered a killable process, but the system will begin killing background processes if apps do not release resources, so you should release non-critical resources now to prevent performance degradation.

    When your app's visibility changes: TRIM_MEMORY_UI_HIDDEN Your app's UI is no longer visible, so this is a good time to release large resources that are used only by your UI.

    When your app's process resides in the background LRU list: TRIM_MEMORY_BACKGROUND The system is running low on memory and your process is near the beginning of the LRU list. Although your app process is not at a high risk of being killed, the system may already be killing processes in the LRU list, so you should release resources that are easy to recover so your process will remain in the list and resume quickly when the user returns to your app.

    TRIM_MEMORY_MODERATE The system is running low on memory and your process is near the middle of the LRU list. If the system becomes further constrained for memory, there's a chance your process will be killed.

    TRIM_MEMORY_COMPLETE The system is running low on memory and your process is one of the first to be killed if the system does not recover memory now. You should release absolutely everything that's not critical to resuming your app state. To support API levels lower than 14, you can use the onLowMemory() method as a fallback that's roughly equivalent to the TRIM_MEMORY_COMPLETE level.

    http://developer.android.com/reference/android/content/ComponentCallbacks2.html

提交回复
热议问题