Saturday, November 7, 2009

A quick thought on memory management

So, the iPhone, with as great of a platform as it is for developers, isn't without its flaws. As I've mentioned before, my "day" job, is software development, with about 99.99999% of that, in Java. I won't go into why Java is better than Objective-C, because quite honestly, I think both are exceptional object oriented languages. One thing Java has, however, that Objective-C (well, on the iPhone anyways) does not, is garbage collection. For those who aren't programmers, garbage collection is a memory management system, where objects that are no longer used automatically release the memory they're holding onto. While garbage collection doesn't guarantee you will never have a memory leak, it certainly goes a long way into ensuring you don't have them. Apple, in their infinite wisdom, decided on the iPhone to not have garbage collection. The reasons for this were sited as it takes too much processing, it slows things down and is heavy. All of which, I firmly believe to be correct.
Because of this, I suspect many apps on the app store have not been properly profiled, or written by their developers, perhaps causing some unnecessary headaches for users.
Here are just some quick ways developers can prevent leaks in their code.
1) Any time you create a new object (retain, copy, or alloc), release it. Best practice is to release it in the same method that created it, otherwise you risk releasing an object that is no longer referenced (VERY bad idea)
2) Make sure your dealloc method contains a release for every property in the header file.
3) Use Build => Build and Analyze to determine if you have any potential memory leaks as you code. If it finds any issues, they're probably legitimate issues that should be addressed.

No comments:

Post a Comment