Recently this time has been in the development of mobile phone applications to meet the problem of Android memory cleanup, see a lot of information, but also tried a lot of methods, but no use, and then constantly groping finally the whole out, the following deliberately summed up the method of memory cleanup, can be those useless process to shut Down, on the code ~
1 protected void Onclearmemory (context context) { 2 Activitymanager Activitymanger = (activitymanager) context 3. Getsystemservice (Context.activity_service); 4 list<activitymanager.runningappprocessinfo> List = Activitymanger 5. getrunningappprocesses (); 6 if (list! = null) 7 for (int i = 0; i < list.size (); i++) { 8 Activitymanager.runningappprocessinfo apinfo = List.get (i) 9 Ten string[] pkgList = apinfo.pkglist; 11 if (apinfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 14 (int j = 0; J < Pkglist.length; J + +) { 16 if (Pkglist[j].equals (Context.getpackagename ())) { Continue; 19} 20 if (Build.VERSION.SDK_INT <= build.version_codes. FROYO) { Activitymanger.restartpackage (Pkglist[j]); +} else { Activitymanger.killbackgroundprocesses (Pkglist[j]); 25} 26} 27} 28} 29 New Alertdialog.builder (Settingactivity.this) Settitle ("Please note") Setmessage ("Memory Cleanup Complete") Setpositivebutton ("OK", NULL) Show (); 35 36} |
|
|
|
Getrunningappprocesses (): get those running processes;
Runningappprocessinfo: Access to ongoing process information;
Importance: Divided into several levels in Android
1. Importance_foreground Foreground process
The so-called foreground process refers to the process that the user needs to complete the current work. The judging conditions are as follows (one can be satisfied):
-an activity in the process is interacting with the user (the Onresume () method of the activity is called)
-Bind to the process where the service of the activity that the current user is interacting with
-a service in the process is running in the foreground, that is, the service's Startforeground () method is called
-a service in the process is executing a lifecycle callback method (for example, OnCreate (), OnStart (), or Ondeatroy ())
-The broadcastreceiver in the process is executing the onreceive () method.
In general, there are only a handful of foreground processes at a given moment. These foreground processes are the most important, and when the system memory is low enough to continue running all of these processes, the system will kill them. In this case, generally refers to the system has reached a limit edge, so in order to let the UI continue to respond, the system has to kill some foreground process;
2. Importance_visible Visual Process
A visual process is a component that is not running in the foreground, but that still affects the content that the user sees on the screen. Processes that meet the following conditions can be counted as visual processes:
-The activity running by the process is not in the foreground, but is still visible (called the OnPause () method). This may be the case, the activity that is running in the foreground launches a dialog box that hovers over the activity, but is still partially visible.
-the service in the process is bound to a visual (or foreground) activity (the activity has called the OnPause () method).
Visual processes are also highly important processes that kill visual processes only if the system has to kill the visual process in order to keep the foreground process running;
3. Importance_service Service Process
The so-called service process means that, in addition to the above two processes, if a process has started a service process through the StartService () method. Although this service process is not related to what the user sees, the work they do is also of interest to the user (such as playing music in the background or downloading resources on the Internet), and the system will keep the service process until the system has to kill the service process for the foreground process and the visual process to run;
4. Importance_background Background Process
A background process is that the activity in the process is currently not visible to the user (the activity invokes the OnStop () method). Background processes do not have any impact on the user's experience, and the system can kill background processes when the foreground process, visual processes, and service inheritance require memory resources. There are often many background processes running, and these background processes are saved in a recent use list, and the benefit is to ensure that the process that the user has recently seen is finally killed. If an activity has implemented the life cycle method correctly, and has saved the current state, then the system kills these background processes to the user's visual effect, there is no impact, because when the user returns back, the activity has saved all the visual state;
5. Importance_empty Empty Process
An empty process does not have any program components running. The only reason for the system to remain empty is to cache considerations, which is primarily to increase the startup time of the component. These empty processes are often killed by the system to maintain balance between the entire system resource and the kernel cache.
This shows that the lower the importance value, the more important, so we can judge the different levels according to our own needs.
2.2 Versions below with Activitymanger.restartpackage (Pkglist[j]); Corresponding permissions <uses-permission android:name= "Android.permission.RESTART_PACKAGES"/>
More than 2.2 of the use of killbackgroundprocesses (package name); Corresponding permissions <uses-permission android:name= "Android.permission.KILL_BACKGROUND_PROCESSES"/>
Also recommended a small series of frequently used app online test tool: www.ineice.com
Talking about: Android app cleanup memory