-4

One possible exam question reads as follows:

"Explain the benefits and drawbacks of Objective C's memory management when compared to c++'s"

I do know Objective C uses ARC, and ARC enables us to avoid destroying an object that is still being referenced by something else (meaning, its still needed). But I can't find any drawbacks at all, anywhere. I can only think of "There are no drawbacks" as an answer, but since the question explicitly asks for drawbacks, I'm guessing there must be at least one.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Objective C doesn't have to use ARC, it's just common now. – Kevin Dec 15 '13 at 23:26
  • I did perform some searching before posting this, and I did come across that one, however, if you read my question, then read the answers in that thread, and try to find at least one real drawback other than a fear for bugs and backwards compatibility back in the 2011 (meaning with devices that no longer matter), you won't find any. As a result, my question which is not whether to use or nor ARC hasn't been answered (I already know how to use both methods quite well as I normally code in ObjC++ when using xcode). – user2970795 Dec 15 '13 at 23:49
  • 1
    @user2970795 This is an awful question in that it ignores a slew of other details that are critical. You are better off figuring out whatever mindset the professor/teacher has than trying to gain anything from a this forum. In particular, the very nature of C++ and Objective-C is so different -- compile time emphasis vs. runtime emphasis -- that the specifics of ARC vs. retain/release/autorelease vs. one of the numerous C++ models will be moot **in the context of your exam**. I.e. you need to learn whatever the heck it is your teacher wants you to learn and ignore the bigger picture. – bbum Dec 16 '13 at 06:41

2 Answers2

1

Reference counting may solve a problem that you don't have. It comes at a price, and you'll end up paying the price no matter whether you wanted the solution in the first place.

Contrary to what the gut feeling may say, most objects actually don't need to be shared at all and have a well-defined, unique ownership throughout their life. All that's needed is the ability to pass those objects around; reference counting provides that, but it provides much more, and has a greater cost.

(This answer compares reference counting in Objective C to C++-style lifetime management. It does not consider whether reference counting in Obj-C is sensible in the first place. ARC is simply an automated form of MRC, and if you were using MRC in the past and it made sense, then the question whether to migrate to ARC is not the point of this post. Rather, this post applies equally to the comparison of "MRC in Obj-C vs C++".)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    Also, ARC is inferior to MRC at so many levels. Before ARC, we had to pay attention to ownership and do our `retain`s and `release`s. Now, with ARC, we still have to do that, but instead of `retain` and `release`, we have to disguise the fact that we are still writing manually refcounted code by including **utterly ugly** and not the least counter-intuitive (if not "stupid") cast qualifiers such as `__unsafe_unretained` and `__bridge_transfer`. –  Dec 15 '13 at 23:32
  • You're welcome. IDK if you're doing/have done any Objective-C programming recently; and as usual, this is my - strong but backed up by facts - opinion. At the end, it comes down to personal preference, so I suggest you try both technologies if you ever want/need to make a decision. –  Dec 15 '13 at 23:38
  • Thank you for your fast answer Kerrek SB!. You mention that it comes at a price, and that it provides much more and has a greater cost. Could you tell me what is the price and the greater cost?, that would be extremely helpful! – user2970795 Dec 15 '13 at 23:44
  • I don't think this is an answer Im afraid.. you only say 'it comes at a price' leaving all the real things unsaid. – Daij-Djan Dec 16 '13 at 00:02
  • @Daij-Djan: "The price" can be experienced as an exercise for the reader: Implement your own reference counting system :-) You should be seeing atomic synchronization costs somewhere along the way. – Kerrek SB Dec 16 '13 at 00:16
  • The perf overhead of reference counting, even for ObjC, varies significantly between OS versions. So there's no one "the" price. – Catfish_Man Dec 16 '13 at 03:25
  • of course you don't say it takes N seconds longer – Daij-Djan Dec 16 '13 at 09:31
0

Reference counting 'frees' you from always thinking about WHEN to delete an object. Anybody using the object just says, I still need it (want to retain it) or I am done with it (I release it)

that makes memory management way easier and also makes the code more manageable BUT it comes at the price of 2 additional method calls whenever you pass stuff around: you have to retain the object, THEN save the pointer and later also call release it.

When you deal with LOTS of objects that become a real life problem. Just the extra calls can kill your algorithms performance

and especially if you don't need any reference counting because the scope where the object is used is clear, the overhead is just annoying.


so it is convenience + maintainability vs. speed

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135