iPhone and Linux

Thursday, December 10, 2009

"Free memory"

Nothing is free.

A lot of iPhone users are obsessed with the amount of free memory they have. Apps like SBSettings will show how many megabytes of free memory you have and even allow you to free up more. A script is going around that allows you to free memory every time it drops by 2 megabytes!

This may go against conventional wisdom, but free memory isn't all it's cracked up to be. There is an old unix saying "free memory is wasted memory." The system keeps inactive data in memory in order to make things faster to look up if it's needed again later. This makes your amount of free memory look low, but it's nothing to worry about. If the memory is needed by another program, the kernel will instantly free it.

Example, you run an app which uses a certain library. That library is loaded into memory. You close the app, but the library stays in memory, just hanging out, doing nothing, making your free memory number go lower. If you load another app that uses that library, it will be faster to look up and will cause the app to load faster, but if the memory has been freed, it will be a little slower as the system has to read the library into memory from disk to use it with the app.

You may be able to see and hear an example of this on your home computer. I have 2GB ram on a Linux system that only needs 500MB, but if I boot my computer and open Firefox, it takes a few seconds to load and I will hear my hard drive clicking as it reads information from the disk. If I close it and open it again, it opens instantly with no drive clicking. That's because everything the system needed to run Firefox was still in memory. If I free the pagecache (echo 1 > /proc/sys/vm/drop_caches) my free memory goes up, but if I open Firefox again, it takes longer and my hard drive clicks the same as it did the first time.

Another example closer to topic is the mail app on the iPhone. Open it up and close it with the home button. Open and close it a couple more times and you should see that it opens almost instantly. Now go to SBSettings, free the memory and open mail again. It will be jerkier and slower than normal.

If you don't see a difference, open an email. It will probably take a couple of seconds to load the email. Once it's loaded, go back to your inbox. Now open the email again and it will open instantly. Go back and forth from your inbox to the email a couple of times and the email should open almost instantly every time. Now, without closing mail, use SBSettings to free memory. Then click the email and it will take a while to load. Again, the data in memory has been dumped and it has to be read anew.

This isn't to say that all free memory is bad, obviously. I have a 3G and wish I had as much free memory as the 3Gs guys. If an app needs X amount of memory and the system can't free that much, something will crash. Having extra memory is a good thing, but freeing memory to get the "free memory" number up means the system isn't using the memory the way it was designed. Things aren't being buffered and will need to be read from disk more often, and that's always slower.

But maybe there is some advantage to clearing out the memory before opening a large app. It would keep the system from having to flush the memory and write to disk while the app is opening and could make for a more stable app launch, but I suspect everything else will be slightly slower in general.

Blog Archive