问题
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