How to watch memory allocation/deallocation while program written in objective-c is working on OS X?

130 views Asked by At

I'm studying Objective-C. I've found out about the ARC. I made a simple program with one class and one instance variable NSUInteger. For educational purposes I'd like to examine the memory allocation/deallocation while the program is running.

Are there any console tools to see a program's memory? Or may be it is possible to do it in the Xcode itself? In other words I'd like to see the memory snapshot in different points in times when an object was allocated and then an object was deallocated.

Thank you.

1

There are 1 answers

0
Amin Negm-Awad On

First, NSUInteger variables are no objects in the meaning of Objective-C. They are handled with the C memory management. (Typically they are local vars on the stack, freed, when the local scope is left.)

So let's go to real instance objects of classes like NSNumber or NSString or – more important – MyCustomClass. You can see the whole processing of memory management, when you write a class and compile that with manual memory management. (This is possible via the compiler options. Select the project, go to build phases/compile sources and you will find an extra column compiler flags.)) Since ARC and MRC work together, ARC code will execute the MRC memory handling methods. Simply overwrite them in the MRC class and do some logging, set some breakpoints and so on.

But, this is very important, as long as you only deal with ARC code, you simply do not have to care about memory management. Maybe it is a good idea to learn that, but it is not something you have to do necessarily.