Monday, November 10, 2014

Nexus 5 and garbage collectors

I really like my Nexus 5. It's at the level of refinement where I would say everything works more or less correctly. This makes a change from my previous smart phone the Samsung Galaxy S. That was a terrible phone. The Galaxy S was too slow, didn't work as a phone owing to a terrible microphone, didn't work as a GPS, would flatten a battery in about 4 hours if you forgot to turn off the GPS or wi-fi and would crash fairly often too. I initially got the Galaxy S due to its reputation as a sort of landmark phone. I figure that means that phones before it were even worse somehow.

So, anyway, I like my Nexus 5. It works as a phone, GPS, keeps a charge and is stable. It also does a cute impression of a flashlight, has a decent camera and can make a sweet Mango Lassi. Well, everything except that last one. I feel like some kind of digital wizard carrying it around. Need a light? Boom! No problem. Need to know the weather? Boom! Weather radar! Need a map of Belgium? Well, that's random but I can get that for you too.

It's also been a good testbed for my application. I've learned quite a few things using that phone like....

Did you know that Android's garbage collector doesn't do compaction? For me this is a little like learning that Ferrari's new car is steam powered; it's a bit difficult to wrap my head around. I figure there some good, technical reason why they do this. I'd wager that they were trying to avoid garbage collector pauses. Google seems to obsessed with avoiding pauses or stuttering on Android (which they call "jank"). The thing is, if you don't have compaction you could run out of memory without.. umm.. running out of memory!

Compaction is the step that un-fragments memory. Wikipedia has good article on memory fragmentation but basically it's when memory gets filled with lots of little holes. Think of it like empty seats in a movie theater. If you arrive too late all the free seats are in singles or groups of two and are scattered all over the place. If you're in a group or four and want to sit together you can't because there aren't four seats together. Compaction is the step where you politely ask people to move around so all the free seats are in one big row - that way large chunks of memory can sit together. If large chunks of memory can't sit together you get an out of memory error even though there's technically enough free seats.

I think I mixed my memory-aphores there.. but you know what I mean.

Luckily the latest release of the Android operating system includes code that does compaction. Although they seemed to imply that it will only do it when you switch out of the application or something. I guess the garbage collector pauses are hard to notice when you've switched out of the application. Does this mean you'll need to switch out of the application you're using every once in a while to avoid running out of memory? I hope not. :-)

No comments: