Monday, April 16, 2012

iPhone - Abandoned memory vs Memory leak


While programming for iOS memory management is a most important task. If you dont handle memory properly it will kill your app.

Most of the iOS developers know about memory leaks, but only a very few know about abandoned memory. Lets have a discussion about what memory leak and abandoned memory is. And we will also look at how to find the abandoned memory in our application. 

Memory leak

Memory leak is a part of memory which is left alone and no longer has any reference to it. A part of memory which is not linked by any pointer. 

Abandoned memory

Abandoned memory is a part of memory which is not used by the application but has a valid reference to it. A valid pointer to that memory will be there but the memory itself wont be used anymore.

The abandoned memory can be tracked down by using Allocation instrumentation tool.

Select Product -> Profile

Choose allocations tool
Repetitively perform an action in your app that starts and finishes in the same state.
After each iteration of the repeated action, click the Mark Heap button to take a snapshot of the heap.
Analyze objects captured by the heap snapshot to locate abandoned memory.




Why this domain name??

When I was first introduced to OOAD and Design patterns, I found myself very much attached to the process of designing a software's architecture. I honestly didn't had any idea about 'Design pattern' when I started my career as a Mobile application developer. But, as every guy who code for living, I too learnt a lot of things as I moved on. One fine day I was assigned to develop a second phase of an iPad application. (some enhancements and new features) I checked out the code and started studying it. The code was unstructured and there were many classes which doesn't know what they are meant to do. Suddenly I had an idea to start from scratch. I talked to my lead, as the time was very less he refused to accept the idea of redoing the whole from scratch. I came back and started thinking how much can I reuse from the existing code. I sat through the whole night and did everything in paper. Started with class diagram, the code snippets which can be reused, old logics and my new logics which can be replaced (and how they are efficient) and finally sequence diagrams. Finally at 3.00 AM I ended up with some paper sheets in my hand and a neat design for my application. I got approval from my lead to proceed with this design. I started coding it, and believe it or not except some small changes the whole design came together well. Everything worked. Bug fixing was so easy. Enhancements were made quick. Thats it. That's how this blog got its name.... I LOVE IT WHEN A DESIGN COMES TOGETHER