Hey guys! Jerry back with another neat trick you can do on your Android phone. This one helps keep things running smoothly, and gives us some user control over Android's internal task killer. Like most things that affect the low level operation of Android, this one requires root, but it's a good read even for you folks who don't feel the need to root (and break your warranty!) because you just might have a better understanding of what's going on behind the scenes. Open up a couple extra tabs on your browser, get your phone (and maybe even your USB cable) ready and follow the break.
Oh, and a warning: This is pretty low-level stuff. It's not hard to do, but what it does is a bit advanced. Don't say we didn't warn you.
You hear time and time again that Android runs on the Linux kernel. The biggest benefit of this, is that Linux is stable, open source, and extremely hackable. This means that many of those benefits carry over to Android as well. Being able to tweak the low memory killer is one of those. This is something that I have personal experience with, both the hard way and the easy way. While this benefits every flavor of Android, phones running HTC Sense will show the most improvement.
How the low memory killer works
When your phone boots up, a file inside the boot image (init.rc) sets the system parameters. Things like the path to framework files, setting up your networks, and setting the limits at which programs are killed off to free RAM are done by this file. Now a super-Android-geek might dig inside the init.rc file and completely customize the low memory killer, but you don't have to do this to still get good results. The init.rc sets up six different "levels" of open applications. Let's have a look at them:
- FOREGROUND_APP: This is the application currently on the screen, and running
- VISIBLE_APP: This is an application that is open, and running in the background because it's still doing something
- SECONDARY_SERVER: This is a process (a service that an application needs) that is alive and ready in case it's needed to do something
- HIDDEN_APP: This again is a process, that sits idle (but still alive) in case it's needed by an app that's alive and running
For the most part, we never want to adjust when these apps and processes are killed off. They are the things that the programs we use need to properly function. For the more bold and advanced users, changing settings for HIDDEN_APP settings is possible, albeit with a LOT of trial and error. There's two more settings, and these are the ones most interesting to us today:
- CONTENT_PROVIDER: This is apps that provide data (content) to the system. HTC Facebook Sync? That's a CONTENT_PROVIDER. So are things like the Android Market, or Fring. If they are alive, they can refresh and provide the content they are supposed to at the set interval. If you kill them, they can't of course.
- EMPTY_APP: I call these "ghosts." They are apps that you have opened, but are done with them. Android uses a unique style of handling memory management. When an activity is ended, instead of killing it off Android keeps the application in memory so that opening them again is a faster process. Theses "ghost" apps use no battery or CPU time, they just fill RAM that would be otherwise empty. When this memory is needed by a different application or process, the RAM is flushed and made available for the new app. To satisfy the geekier people (like myself) Android does this by keeping a list of recently used apps, with the oldest apps in the list given the lowest priority -- they are killed first if RAM is needed elsewhere. This is a perfect way to handle 'ghost' processes, so there's no need to touch this part ;)
The EMTY_APP level is where we can play Robin Hood and gain some UI performance. We rob from the rich (RAM used by apps we are done using) and give to the poor (our pool of free RAM). This way WE decide when Android closes old, unused apps instead of letting the system decide for us. Things like this are why we root.
I want to stress -- there is nothing wrong with the way Android handles this out of the box. It's efficient, works well every time, and keeps things stable. It's our meddling and the need for instant gratification that is the real issue. Waiting .3 seconds longer never killed anyone. But I'm just as guilty as anyone else, and want immediate results too, so let's do it!
How do we manipulate all this?
Remember earlier I said you didn't have to dig into cryptic start up files to change these settings? Well you're gonna love this. You can change things around with an app from the Market! There are several that work, but I prefer MinFreeManager because it's lightweight and doesn't share any personal information like some of the others do. Lets have a look at how to set this bad boy up.
Step one -- delete any task manager you have running. They are only going to interfere with what we're doing today. If you don't, we're all going to point and laugh while our phones run smoothly :)
Step two -- download and install MinFreeManager
Step three -- open the app. Press the back button to get rid of the keyboard (we'll talk about editing the numbers you see in a bit), and hit menu > presets.
Choose "Aggressive". There's no sense in installing and running an app like this without running it at it's full potential. If after a day or two you think you're seeing issues from running at these settings, you can try "mild" or tweak the numbers by hand. Next hit "apply" to set the parameters. If you'd like the settings to be applied at every reboot, menu >> apply at boot will take care of you. For most everyone, that's it. Enjoy having about 75-100 MB of free RAM all the time.
If you want to tweak the numbers yourself, each entry is the level of free RAM that apps and processes in each particular category will start to get killed. Below are the default settings for Android.
Notice nothing gets wiped from RAM until the amount of free RAM reaches 32 MB. (If you've listened to me while I've stood on my soapbox, you know that in my opinion SenseUI just doesn't work well with less than ~40 MB of free RAM) At 32 MB or less free, the system will start killing the 'ghosts'. We can work our way up the list and see that if we ever get down to 14 MB or less free, the system can (and will) kill the app you have running in the foreground.
If you decide to play with these numbers, be very careful with the top four numbers. Monkeying with these can make things very unstable, and very ugly. The Content Provider (read it again above) is less touchy, but setting it too high will make things like Facebook sync even more screwy that it already is. The Empty App section is the free for all. Having this set to anything less than 96 MB (IMO) defeats the purpose of using an app like this. Tweak things, give it a couple hours to settle in and decide if it needs more tweaking.
Protip: if 'Aggressive' settings are a bit too much, instead of trying 'mild' try changing the Content Provider number a bit smaller and see what happens.
LowMemKiller Geek Edition
Because Android runs on a Linux kernel, that means parameters are stored as plain text files. these can be edited on the fly -- thats exactly what apps like MinFreeManager are doing. If you'd rather not use a third party app to make these changes, you can easily change the values from the command line. Fire up your favorite terminal client and follow along.
To check the current parameters for the MinFree setting:
This will return a string of numbers, something like this:
These numbers are pages. To Android, a page is equal to 4 kb. So in the above example, the EMPTY_APP settings (last in the list -- 6144) is 24 MB. 6144 pages = 24576 kb = 24 MB. The formula would be:
((6144 * 4)/ 1024)
Therefore, a setting of 25000 would be equal to ~97MB (told you it was the Geek Edition!).
For a good starting point, and an example of how to change the numbers, try this:
echo "1536,2048,4096,5120,15360,23040" > /sys/module/lowmemorykiller/parameters/minfree
This sets things up so that whenever free RAM drops below 90MB, 'ghosts' are killed off in order, and if RAM ever drops below 60MB, CONTENT_PROVIDERS get killed off. Everything else is left unmolested by the kernel.
Think about that for a second -- no more worrying about keeping things like clock and gmail alive and on the task killers whitelist, because we specify what kinds of apps and processes can be killed off to free up memory.
Once you have a set of numbers that work well for you, it's easy to streamline the process of applying it when you reboot. Yes, these changes go away at each reboot as they are stored in a virtual disk (the Ramdisk) that erases when the phone is turned off. You'll need a text editor that can save files in a format readable by Unix. Nothing made by Microsoft is going to work. I always recommend Notepad ++ to Windows users with a need to edit Unix files. Grab it, and install it.
Open a blank file and paste the following in it:
echo "1536,2048,4096,5120,15360,23040" > /sys/module/lowmemorykiller/parameters/minfree
of course change the default settings to your desired settings. Save the file as freeram with no extension.
Hook up your phone to your computer and push the freeram file to /system/bin. Then at the command line prompt:
chmod 744 freeram
Now reboot your phone, and once it's loaded open your terminal and type in:
To set YOUR parameters instead of the default Android parameters. You'll need to do this at every reboot, or edit it into the init.rc file, which is waayyy too geeky to cover here. We have a forum for that. :)
Whether you like the easy way, or prefer the more hardcore way, give it a try. It's easy to revert, and its 100 percent free.
Till next time,