Hey guys! Jerry is back with another neat trick you can do on your Android phone. This one helps keep things running smoothly and gives us some custom control over Android’s internal task killer. Like most things that affect the low-level operation of Android, it requires root, but it’s a good read even for those people who don’t feel the need to root (and violate their warranty!) because you just might have a better understanding about what’s going on behind the scenes. Open a couple of extra tabs in your browser, get your phone (and maybe even a USB cable) ready, and keep an eye out for a break.
Oh, and a warning: this is pretty low-level stuff. It’s not hard to do, but then , what he does, a little advanced. Don’t say we didn’t warn you.
You hear over and over again that Android runs on the Linux kernel. The biggest advantage of this is that Linux is stable, open source, and extremely hackable. This means that many of these benefits extend to Android as well. The ability to set up a low-memory killer is one of them. This is something I have personal experience with, both difficult and easy. While this is beneficial for all versions of Android, phones with HTC Sense will show the most improvements.
How the memory killer works
When your phone boots up, a file inside the boot image (init.rc) sets the system parameters. Things like navigating to the framework files, configuring your networks, and setting limits at which programs are removed from free RAM are all done by this file. Now the super Android geek can dig into the init.rc file and fully customize the low memory killer, but you don’t have to do that to get good results. Init.rc installs six different «tiers» of open applications. Let’s take a look at them:
- FOREGROUND_APP : This app is currently on screen and running
- VISIBLE_APP : this is an application that is open and running in the background because it is doing something else
- SECONDARY_SERVER : is a process (a service that the application needs) that is alive and ready in case something needs to be done
- HIDDEN_APP : This is again a process that is idle (but still alive) in case it is needed by an application that is alive and running
For the most part, we never want to adjust when these apps and processes are killed. These are the things that the programs we use need to function properly. For the more daring and experienced users, changing the settings for options HIDDEN_APP possible, albeit with a lot of trial and error. There are two more settings, and they are the most interesting for us today:
- CONTENT_PROVIDER : These are applications that provide data (content) to the system. HTC Facebook Sync? it CONTENT_PROVIDER . Things like Android Market or Fring. If they are alive, they can update and provide the content they need to do at a given interval. If you kill them, they certainly can’t.
- EMPTY_APP : I call these «ghosts». These are applications that you have opened but made with them. Android uses a unique style of memory management. When an activity ends, instead of killing it, Android keeps the app in memory so that reopening it is a faster process. These «ghost» apps don’t use batteries or CPU time, they just fill up RAM that would otherwise be empty. When this memory is required by another application or process, the RAM is reset and made available for the new application. To satisfy geeky people (like me), Android does this by keeping a list of recently used apps, with the oldest apps on the list having the lowest priority — they are killed first if RAM is needed elsewhere. This is the perfect way to handle «ghost» processes, so no need to touch that part 😉
At the level EMTY_APP we can play Robin Hood and get some interface performance. We rob the rich (the RAM used by the applications we use) and give to the poor (our pool of free RAM). In this way, we decide when Android closes old, unused apps rather than letting the system decide for us. That’s why we get sick.
I want to emphasize — there is nothing wrong with the way Android handles this out of the box. It’s efficient, works well every time, and stays stable. It is our intervention and the need for immediate gratification that is the real problem. Waiting .3 seconds never killed anyone again. But I’m just as guilty as anyone else and I want immediate results, so let’s do it!
How do we manage all this?
Remember earlier I said that you don’t have to dig into the cryptic startup files to change these settings? Well, you’ll love it. You can change things with an app from the Market! There are a few that work but I prefer MinFreeManager, because it’s lightweight and doesn’t share personal information like some others. Let’s see how to set up this bad boy.
Step one — remove any task manager that works for you. They will only interfere with what we are doing today. If you don’t, we’ll all show and laugh while our phones work fine 🙂
step two — download and install MinFreeManager
[ Рынок Ссылка | AppBrain ]
Step Three — open the application. Hit the back button to get rid of the keyboard (we’ll talk about editing numbers, which you’ll see in a bit), and hit menu > presets.
Select Aggressive. There is no point in installing and running such an application without running it in full force. If after a day or two you think you’re having trouble starting at these settings, you can try «moderate» or tweak the numbers manually. Then click «Apply» to set the options. If you want the settings to apply on every reboot, the >> apply menu on boot takes care of you. For most, that’s it. Enjoy having 75-100MB of free RAM all the time.
If you want to tweak the numbers yourself, each entry is a level of free RAM that apps and processes in that particular category will start killing. Below are the default settings for Android.
Note that nothing is erased from RAM until free RAM reaches 32 MB. ( If you’ve been listening to me while I’ve been standing on a soap box, you know that in my opinion SenseUI doesn’t perform well with less than ~40MB of free RAM ). At 32 MB or less free the system will start killing «ghosts». We can move up the list and see that if we ever get free to 14MB or less, the system can (and will) kill the app you’re running in the foreground.
If you decide to play around with these numbers, be very careful with the top four numbers. Monkeys with this can make things very unstable and very ugly. The content provider (read it again above) is less annoying, but setting it too high will make things like Facebook sync even weirder than it is now. The blank section of the app is free for everyone. If this setting is set to less than 96 MB (IMO), it will negatively affect the use of such an application. Set things up, give him a couple of hours to settle in and decide if he needs more tweaks.
Hint: if the «aggressive» settings are too high, instead of trying «soft» try changing the content provider number a little lower and see what happens.
LowMemKiller Geek Edition
Since Android runs on the Linux kernel, this means that the settings are stored as plain text files. they can be edited on the fly, which is exactly what applications like MinFreeManager do. If you don’t want to use a third party application to make these changes, you can easily change the values from the command line. Launch your favorite terminal client and follow the instructions.
To check the current settings for the MinFree setup:
su
cat /sys/module/lowmemorykiller/options/minfree
This will return a string of numbers, something like this:
1536.2048.4096.5120.5632.6144
These numbers are pages. For Android, the page is 4 KB. So in the setup example above EMPTY_APP (the last one in the list is 6144) is 24 MB. 6144 pages = 24576 KB = 24 MB. The formula will be:
((6144 * 4) / 1024)
Therefore, a value of 25000 would be ~97MB ( it says it’s a Geek Edition! ).
For a good starting point and an example of how to change numbers, try this:
su
echo «1536,2048,4096,5120,15360,23040» > /sys/module/lowmemorykiller/options/minfree
This sets it up so that whenever the amount of free RAM goes below 90MB, the «ghosts» are killed in order, and if the amount of RAM goes below 60MB, CONTENT_PROVIDERS was killed . Everything else is not broken by the kernel.
Think about it for a second — no more worrying about keeping things like clock and gmail up and whitelisting task killers because we specify which types of apps and processes can be removed to free up memory.
If you have a set of numbers that works for you, it’s easy to simplify the process of applying it on reboot. Yes, these changes disappear on every reboot as they are stored on a virtual disk (Ramdisk) which is erased when the phone is turned off. You will need a text editor that can save files in a Unix-readable format. Nothing made by Microsoft will work. I always recommend Notepad++ to Windows users who need to edit Unix files. Get it and install.
Open an empty file and paste the following into it:
echo «1536,2048,4096,5120,15360,23040» > /sys/module/lowmemorykiller/options/minfree
of course, change the default settings to your desired settings. Save the file as Freeram without extension.