objective-cmacosgarbage-collectionautomatic-ref-counting

Use both Garbage Collector and ARC in objective c


I have read that Garbage collection is done in objective c using AUTOZONE(LIBAUTO).

Also garbage collector is available upto OS X 10.8. While studying i was thinking what is the need of ARC if garbage collector is available. Then from the sources on stackoverflow.com, i learned the difference between ARC and garbage collector and advantages of both over each other.

Now i know that ARC works at compile time and garbage collector works at run time. Also ARC is not able to release memory of CFTypes, it only works for objective C types. And Garbage collector could release retain cycles that couldn't be done using ARC.

Now i wanted to know that can we use both ARC and garbage collector together, since both are available in os x 10.7. Also why is garbage collector deprecated after 10.8. Is ARC an alternative to garbage collector and it can release every kind object that was done by garbage collector.

Also since garbage collector was not available in ios, then what was used in ios before ARC for garbage collection. Is manual memory management is able to manage everything if done correctly, and there is no need of garbage collection if Manual memory management is taken care of correctly?


Solution

  • As far as I know there is no (easy) way to enable both, and it wouldn't make sense anyway.

    You already almost answered the question yourself: both GC and ARC serve the same purpose, cleaning up memory. GC has the advantage of catching things that ARC cannot, but it has a runtime penalty. With ARC, you can still leak memory if you don't use it correctly but if done right (mostly: you adhere to the naming conventions), ARC has the major advantage of freeing you from the manual management burden with excellent runtime performance.

    So you chose either one or the other, it doesn't really make sense to mix them. If you have GC enabled, all the work that ARC is doing would be almost useless and thus wasting performance.

    BTW, before ARC, we did manual memory management using retain/release. In fact, that is still an option and still used widely (for example, I work on a huge project which would be cumbersome to convert to ARC).