Testing memory management in Objective-C (iOS)?

The static analyser will attempt to find errors in your code by inspecting it without running it. It's really good at spotting times when you accidentally make an allocation error. It'll tell you about things that are unarguably going to cause a problem and occasions when you've stepped outside of normal Objective-C conventions.So don't necessarily be confused if it highlights something you think won't lead to a problem — Objective-C is a combination of rules and conventions and the analyser looks for breaches of both.

The static analyser will attempt to find errors in your code by inspecting it without running it. It's really good at spotting times when you accidentally make an allocation error. It'll tell you about things that are unarguably going to cause a problem and occasions when you've stepped outside of normal Objective-C conventions.So don't necessarily be confused if it highlights something you think won't lead to a problem — Objective-C is a combination of rules and conventions and the analyser looks for breaches of both.

Instruments will look for leaked objects at runtime. So it can find only problems that actually occur.So in principle you'd need to go through every single potential usage path in your program to catch everything. It tends to be really useful in practise though.

And it's not that complicated to use. Just launch with the leaks instrument, run for a bit, you'll get a graph of when memory is leaked and you can find out what sort of objects are being leaked. You can then ask to be shown where in the source the object was created.

Keep in mind that you'll get some secondary leaks (in that, if one object leaks then all the objects it was retaining will also leak). On the desktop Guard Malloc is also available, though I think it isn't yet on iOS. That causes your program to run a lot more slowly but will cause it to crash immediately upon any memory access error.

So that's really helpful because normally things like writing past the end of C arrays or accessing deallocated objects may cause an error but won't necessarily do so, sometimes causing bugs in some part of the code to corrupt structures used by other parts of the code and cause a crash in code that's completely valid.

Instruments will do a ton more than just look for leaked objects; it'll do everything from CPU analysis to I/O analysis to object population analysis. For example: friday.com/bbum/2010/10/17/… – bbum Jan 31 '11 at 5:13.

I see a lot of people stating that "Instruments can track down leaks in context of memory management" and leaving it at that. So far from the truth; the Leaks analysis is useful, but barely scratches the surface of the awesome optimization opportunities it can reveal. Can be used for Zombie detection; i.e.To track down the over-release of an object that causes a crash Can monitor the allocation bandwidth; the # of objects your app creates/deallocates over time.

Reducing that is a very effective means of optimizing an app. Can inspect the exact set of objects live and in memory at any one time; the working set. Using Heapshot analysis, can show you exactly how your app permanently grows over time.

Can tell you exactly where any given object is retained and/or released, including backtraces.

Yeah the best thing you can do is to learn more about memory management developer.apple.com/library/mac/#documen... Build and Analyze works pretty well for determining some things like if something is never released. It will help you track down alot of problems, but it is by no means perfect. Instruments will help you track down leaks but it is a little harder to use and track down exactly where things go wrong.

Another helpful tool you can use to track down crashes from memory is to click Run->Stop On Objective C Exceptions. Which will let you go through stack traces of where your application crashes to find out where it goes wrong.

I'm fairly new to Objective-C (come from a Java background) but I was able to get by with the two features you mentioned (Build/Analyze) and Instruments. I think those worked because I got my app approved by Apple and haven't had any customer complaints so far, so at least that's something. Anyway I found Analyze to be far less helpful than I would have thought.

Mostly I found problems by using Allocations and Leaks Instruments. Analyze didn't find things that I think they could probably check for. An example would be not releasing (or autoreleasing) an object that you alloc/init and then assign to a retained property.

You can even search your source for every time you use alloc and then just do a double check to make sure you're doing correct memory management for each case. Leaks instrument was very helpful, but make sure you put your app through the ringer while its running. Just click on all sorts of stuff, go in and out of screen lots of times, click on things people would never do, etc.You will find the leaks.

Then crush them. Very satisfying.

See How to improve memory footprint for iPad apps.

I've got some memory management tutorial videos, including one on using Instruments to fix memory crashes: markj.net/iphone-memory-management-tutor... markj.net/iphone-memory-debug-nszombie.

Clang with xcode is another great option. clang-analyzer.llvm.org/xcode.html.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions