在Android中清理应用程序的内存使用情况 [英] Clean memory usage of application in android

查看:207
本文介绍了在Android中清理应用程序的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android中以编程方式清除应用程序内存使用情况?例如,使用干净的内存来增强"CLEAN MASTER"应用之类的内存.

解决方案

这没用.

首先,要了解为什么必须了解UNIX(以及Android)如何管理RAM.

链接到XDA论坛可以提供帮助.

>

从本质上说,它是将Apps加载到ram中的,以提供更好的访问时间和更流畅的感觉.他们不使用您的CPU或耗尽电池.

实际上,如果您清除了RAM,则系统必须再次将那些必要的应用程序(例如,应用程序抽屉)加载到RAM中,这肯定会使用CPU,从而耗尽电池电量.

简短:清除撞锤会消耗电池电量.而且我无法想象你会怎么想这个;)

要了解有关Android中内存管理的更多信息,请阅读 Android开发人员中的指南. .

如果您仍要以编程方式清除内存,则可以参考此问题

简而言之,您可以按照以下说明进行操作(未经测试):

@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();
   }

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

解决方案

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();
   }

这篇关于在Android中清理应用程序的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆