Clean memory usage of application in android [closed]

99封情书 提交于 2020-05-14 03:59:12

问题


How to clean application memory usage in android programmatically? for example, clean memory usage to boost memory like "CLEAN MASTER" App.


回答1:


This is useless.

First, to understand why, you have to understand, how UNIX (and thus Android) manages the RAM.

This Link to the XDA-Forum can help.

Essentially it says that Apps are loaded into the ram for better access-times and smoother feeling. They do not use your CPU or drain your battery.

In fact, if you clear the RAM, the system has to load those essential apps (as the app-drawer for example) again into the RAM, which definitely uses CPU and thus drains your battery.

Short: Clearing ram costs battery. And I can't imagine how you'd like to have this ;)

To learn more about Memory-Management in Android you can read the guide here on Android Developers.

If you still want to clear memory programmatically, you may refer to this question

In a nutshell it says that you can do it as following (not tested):

@Override
protected void onDestroy(){
 super.onDestroy();
 Java.Lang.Runtime.GetRuntime().RunFinalization();
 Java.Lang.Runtime.GetRuntime().Gc();                   
 trimCache(this.ApplicationContext);
}

public void trimCache(Context context)
   {
       try
       {
           Java.IO.File dir = context.CacheDir;
           if (dir != null && dir.IsDirectory)
           {
               deleteDir(dir);
           }
           context.DeleteDatabase("webview.db");
           context.DeleteDatabase("webviewCache.db");
       }
       catch (Exception e)
       {
           CostantsLift.WriteTextFile("   trimCache ", e.Message, currDate);
           // TODO: handle exception
       }
   }

   public bool deleteDir(Java.IO.File dir)
   {
       if (dir != null && dir.IsDirectory)
       {
           String[] children = dir.List();
           foreach (string child in children)
           //for (int i = 0; i < children.Length; i++)
           {
               bool success = deleteDir(new Java.IO.File(dir, child));
               if (!success)
               {
                   return false;
               }
           }
       }

       // The directory is now empty so delete it
       return dir.Delete();
   }


来源:https://stackoverflow.com/questions/24077085/clean-memory-usage-of-application-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!