Android memory cleanup

Source: Internet
Author: User

In the past two days, a small memory cleanup plug-in has been established. This information is rarely found on the Internet. I have previously found an example of how to clean up the cache and the result is unsuccessful. Later, I thought about how to clean up the memory, so I succeeded.

The two methods are copied from the Internet to obtain the available memory and the total memory, so that we can get the used memory.

View code

Private long getavailmemory (context)
{
// Obtain the current available memory size of Android
Activitymanager AM = (activitymanager) Context. getsystemservice (context. activity_service );
Memoryinfo MI = new memoryinfo ();
Am. getmemoryinfo (MI );
// Mi. availmem; available memory of the current system

// Return formatter. formatfilesize (context, mi. availmem); // normalize the obtained memory size
Return mi. availmem/(1024*1024 );
}

Private long gettotalmemory (context)
{
String str1 = "/proc/meminfo"; // system memory information file
String str2;
String [] arrayofstring;
Long initial_memory = 0;

Try
{
Filereader localfilereader = new filereader (str1 );
Bufferedreader localbufferedreader = new bufferedreader (
Localfilereader, 8192 );
Str2 = localbufferedreader. Readline (); // read the first line of meminfo, total system memory size

Arrayofstring = str2.split ("\ s + ");
For (string num: arrayofstring ){
Log. I (str2, num + "\ t ");
}

Initial_memory = integer. valueof (arrayofstring [1]). intvalue () * 1024; // obtain the total system memory, measured in KB, multiplied by 1024 to byte
Localbufferedreader. Close ();

} Catch (ioexception e ){
}
// Return formatter. formatfilesize (context, initial_memory); // converts byte to kb or MB, and standardizes the memory size.
Return initial_memory/(1024*1024 );
}

Clean up memory in Service

You can write a small method by yourself to determine process information to determine which processes are useless and can be killed.

View code

Activitymanager activitymanger = (activitymanager) This. getsystemservice (activity_service );
List <activitymanager. runningappprocessinfo> List = activitymanger. getrunningappprocesses ();
If (list! = NULL)
For (INT I = 0; I <list. Size (); I ++)
{
Activitymanager. runningappprocessinfo apinfo = list. Get (I );

System. Out. println ("PID" + apinfo. PID );
System. Out. println ("processname" + apinfo. processname );
System. Out. println ("importance" + apinfo. Importance );
String [] pkglist = apinfo. pkglist;

If (apinfo. Importance> activitymanager. runningappprocessinfo. importance_service)
{
// Process. killprocess (apinfo. PID );
For (Int J = 0; j <pkglist. length; j ++)
{
// 2.2 or more are outdated. Use killbackgroundprocesses instead.
Activitymanger. restartpackage (pkglist [J]);
}
}

Resolution:

Runningappprocessinfo

Pkglist: Obtain the name of the package running in the process.

Importance: Importance of the process

Importance is divided into several levels. The lower the value, the more important it is. For the importance of the value, see the API. In my opinion, all processes that are greater than activitymanager. runningappprocessinfo. importance_service are killed. Generally, processes that are greater than activitymanager. runningappprocessinfo. importance_service are useless or.

Finally, the process corresponding to the package name is killed through the For Loop:

Activitymanger. restartpackage (pkglist [J]) is used in versions earlier than 2.2. the permission <uses-Permission Android: Name = "android. Permission. restart_packages"/>

2.2 or more killbackgroundprocesses (package name); permission <uses-Permission Android: Name = "android. Permission. kill_background_processes"/>

Previously, I thought that android. OS. process. killprocess (PID); could be used to kill other processes. Later, I found that the process could not work.

The resolution found on the internet is

For this method, you need to explain it in detail. In the SDK documentation, the explanation is as follows:

Kill the process with the given PID. note that though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common uid will also be able to kill each other's processes.

The English is not good and cannot be accurately translated, so I hope you can understand it by yourself to avoid misunderstanding. My personal explanation for this sentence is that the use of this method is conditional:

A. Put the killed process in the same package or application as the current process;

B. The process to be killed is an additional process created by the current application;

C. Share the UID of a common user with the killed process. (The common user here is relative to the root user)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.