Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the Instrument for memory leak checking - iPhone

alt text

Above given images is of my application leaks.

Here I want to understand that, in Extended Detail - you can see different colors like light green, light pink, light brown, light purple.

What does each color indicates?

Now the other confusion is "How to locate the code which is creating a memory leak?"

Upto what limit of memory leak - the actual iPhone can go on with. (suppose 10 bytes no problem, 20 bytes no problem & 200 bytes a problem)

  • What does each color indicates?
  • Which color indicates our code / From which detail we can get to the code where we have allocated the object & forgot to dealloc it?

(For example - On clicking of UIKit second cell in detail - we cant get to the code)

  • Why we must resolve all the leaks? - even a single leak can chock up iPhone ?
  • Why iPhone allows leaks to be remain in memory? / why garbage collection isn't done automatically after termination of application?
  • If I try to dealloc objects which should be deallocated according to instruments, My application terminates abnormally. If I don't dealloc, My application runs perfectly, How?
  • Why it is suggested that you wait in a view up to 10 or more seconds, if there is a leak, leak will be detected by Instruments?
like image 514
Sagar Kothari Avatar asked Aug 25 '09 16:08

Sagar Kothari


2 Answers

Ignore the colors, in that one the [DashBoard viewDidLoad] is the source of the leak, something in how it's initializing a URLConnection (possibly you did not free that when the connection was finished?)

Now to answer the other questions you had:

  • Why we must resolve all the leaks? - even a single leak can chock up iPhone ?

Yes. Part of the reason is not only that you will simply run out of memory, but since there is only so much memory to go around for the whole phone a watchdog application is constantly monitoring your app and will shut it down early if it sees memory use only ever growing...

  • Why iPhone allows leaks to be remain in memory? / why garbage collection isn't done automatically after termination of application?

All your application memory is freed when the app quits.

  • If I try to dealloc objects which should be deallocated according to instruments, My application terminates abnormally. If I don't dealloc, My application runs perfectly, How?

Here I can't help, you really need to read more on the retain/release memory cycle... if you release an object that has a retain count of 0, the app crashes because the object is gone.

  • Why it is suggested that you wait in a view up to 10 or more seconds, if there is a leak, leak will be detected by Instruments?

Because instruments works by sampling memory every so often, so it might take a little bit for instruments to get around to reading the memory after an action.

like image 73
Kendall Helmstetter Gelner Avatar answered Nov 05 '22 07:11

Kendall Helmstetter Gelner


First of all, the things in the stack are colored by which library they come from, so it doesn't contain that much information.

Second, instead of worrying about how much leakage the iPhone can take, I'd focus on not having it leak.

To find leaks, there are a couple options:

  • Use the CLANG static analyzer when building your project
  • Look for leaks manually. You must always follow The Rules of memory management: if you alloc, retain, or copy an object (including using @property (retain) or (copy)), you must release or autorelease it.
like image 27
jtbandes Avatar answered Nov 05 '22 05:11

jtbandes