Garbage Collection Gets a Bad Rap

Monday, January 4th 11:52pm Matt

Today I was searching for information why the iPhone doesn’t implement garbage collection. Programs using Cocoa for the Mac do have garbage collection available.

Everywhere I looked I saw people stating their opinion that garbage collection was not implemented because it was a performance hog and would reduce battery life. Now, this may be the case, but of course Apple has never said it, and if they did, it would be retarded. And I’ll prove it.

Let me state for the record that I was a developer on Windows Presentation Framework, which was one of the first large projects to use C# extensively within Microsoft, so I’m well aware of the truths and lies of garbage collection as it’s a battle I had to fight often, even sometimes with my own teammates. I worked closely from time to time with the guy who designed the garbage collector in C# and if it matters I was even friends with his son, who also works at Microsoft. My point is, I am speaking from a place of experience, while others usually are not.

First, garbage collection is not a processor or resource hog unless an app is very badly written. In fact, it is more likely to make your app faster because memory is allocated out of a linear pool (there’s probably a more technically correct name for that) so allocations are much faster than without garbage collection. Also, garbage collection usually occurs at very smart times both when nothing else is going on and only when necessary using very smart metrics.

What would make garbage collection slow is if you are constantly allocating and deallocating memory for complex and deep trees of objects. Of course, this will make your app slow without garbage collection, as well, and is a good sign that your app is bad and you are not a good developer.

But most apps don’t do that, so things are going to be fine. Think about how most apps work, they are event driven. The vast majority of time they are idle. What does the garbage collector even have to do? It’s not going to run if there haven’t beed quite a few allocations. Compared to unmanaged memory allocation, the performance could go either way in very miniscule amounts, and it’s certainly nothing to affect battery life. Again, the exception is bad applications and, heaven forbid, a bad garbage collection algorithm.

Also the iPhone doesn’t multitask applications. How much garbage collection is one application that only runs while the user is explicitly using it going to do?

But I have some really firm proof for you, too. When the iPhone first came out Apple was telling people to write their apps entirely for the web. People hated this idea, but Apple loved it. They didn’t want to deliver an SDK for the iPhone.

Well what do apps written for the web use as a language? Javascript. And what is Javascript? A garbage collected language. So it stands to reason that if garbage collection is so horrific we should all stop using our web browser on the iPhone and certainly Apple shouldn’t be telling people to develop web apps for the iPhone. But they are. Why? Because garbage collection literally is not an issue.

So why is there no garbage collection for Cocoa apps on the iPhone? Well, I have no idea, and most likely, neither do you. But I’ll give you a much more reasonable explanation: time. Apple has been rushing super hard to get the iPhone SDK out the door. What can be dropped without having an effect on functionality? Garbage collection. I hope that they include it soon, because I highly doubt it’s a performance issue, and garbage collection really makes development easier.

I haven’t been at Microsoft for a few years, but when I left this issue was so political that the C# team was doing tours around the campus to show people that garbage collection was not evil. But even developers at Microsoft are stupid. They think that if they didn’t write the code, it must be bad. They won’t use frameworks, or tools, or anything. Every algorithm in every app is written from scratch even if a hundred other apps use it. Did you know that the Office team has written their own HTML rendering engine from scratch to display Outlook HTML messages? Well, they have. Because it’s better than Internet Explorer? No, it’s actually much, much worse, but they can’t trust anyone else’s code. That’s why Microsoft’s stock is so low, and why I and many others felt it was not worth staying there.

Submit a Comment