I’m reading a book on Objective C, and I was wondering about 2 things:
- Should I take the time currently to read a whole chapter on memory management?
- If you are doing a really good job on manual management, can you get to better performance than using ARC?
You can do better, but you might also end up doing a lot worse (introducing leaks and crashes).
I’d say you should read about it, but use ARC as default anyway. When you have to, override ARC for certain files and manage your alloc’s/releases by yourself.
I recommend Ray’s article about memory management. It describes a few practices which helps a lot. Obj-C is more about “conventions” and consistency than language rules, so learning those conventions is a must if you’re doing manual memory management.
1
To answer question one:
Regarding question two, you most definitely can do better than ARC. Automated resource management in any language is always a heuristic approach that can never interpret the intentions of the programmer 100%. When you opt to control this process, you will almost always have fewer alloc/free calls for a given run of your program. This means more efficiency, (usually) less memory fragmentation, and sadly, the unfortunate task of having to worry about memory leaks.
Memory management (and pointers/aliasing) tend to be taught to people with the stigma of difficulty. This is an awful mistake. These concepts are actually quite simple and fundamental to how the machine you’re operating works. Face them head on and early, and you’ll be a force to reckon with.
That said, unless you have a specific reason to use MRC, ARC will save you debugging time and let you write safer code faster.
Yes, you should read it. Automatic memory management of any kind is not magical; like any abstraction, it’s not perfect and foolproof. When you’re confronted with it’s limitations or something difficult to understand, it pays to know what is going on behind the scenes. Moreover there are many third-party libraries that don’t use ARC so you may not have the option of only using ARC.
As the other answers say you should read it to get an understanding of what it does and there are still a lot of non-ARC code out there in various projects you might want to use but I would like to point out that even though it is possible to beat ARC in performance you probably won’t do it and the effort to do manual memory management could be spent elsewhere.