How reliable is garbage collection in Objective-C

Discussion in 'Mac Programming' started by ZVH, Apr 14, 2012.

  1. ZVH macrumors 6502

    Apr 14, 2012
    I've heard it wasn't, yet others seem to swear by it.

    One guy told me GC would prematurely mark and collect on class variables without warning. I haven't been able to confirm or deny it. He was referring to this type of allocation:

    [NSString stringWithFormat:mad:"%@", @"This is a test"];


    [[NSString alloc] initWithFormat: @"%@", @"This is a test"];

    He told me the first example could be prematurely collected, as in before it even gets used, whereas the GC would handle the second example properly.

    I'm considering switching to a GC environment for development, but not if it's buggy.

    Comments anyone?
  2. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    You may wish to forget Garbage Collection and, depending upon your platform requirements, use Automatic Reference Counting instead.

  3. xStep, Apr 14, 2012
    Last edited: Apr 15, 2012

    xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    The only odd thing mentioned that I could find was from the Garbage Collection Programming Guide. Under disadvantages it says, "A common design pattern whereby resources are tied to the lifetime of objects does not work effectively under GC". I'd like a better explanation than that.

    There are three memory management techniques; manual memory management where you handle the retains and releases, garbage collection (GC) where the runtime takes care of releases, and the new automatic reference counting (ARC) where the compiler balances the retains and releases.

    CG and ARC should not be prematurely releasing objects.

    In your sample lines, you need to understand that the first does not return a retained object. The life of that object is guaranteed to last to the end of the method it is in. In the second line, the alloc causes a retain. If you assigned the returned value (an object id) of the first line to an instance variable, you'll need to add your own retain to keep it around longer than the call to the method. If you don't do that, the object will disappear upon the next cycle of releasing the memory associated with it. The second line will require a release by you under manual memory management, or the garbage collection will do it if you are using that.

    The GC method wasn't a big hit with developers from what I could tell and does not work with iOS. The new ARC method is what developers are moving to, usually away from the manual method.
  4. JoshDC macrumors regular

    Apr 8, 2009
    I would strongly discourage moving to garbage collection. The fact that iCloud isn't supported in garbage collected applications should raise questions about its future. From the programming guide:

  5. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    So, first off, what everyone else said: don't use GC. ARC killed it. Seriously, don't use it.

    That said, what he's probably talking about is things like this:

    void *bytes = [someNSData bytes];
    [self doSomethingWithSomeBytes:bytes];

    Because 'someNSData' is not referenced after the first line, if that's the only live reference to it, it may be collected... *even though* bytes is a pointer to inside of it. This isn't a bug per-se, it's just an unfortunate side effect of having raw pointers in a GC'd language.

    You can avoid this by doing something like:

    void *bytes = [someNSData bytes];
    [self doSomethingWithSomeBytes:bytes];
    [someNSData self]; //use someNSData again to keep it live
  6. thundersteele macrumors 68030

    Oct 19, 2011
    As others have suggested, i wouldn't worry about garbage collection and instead switch to using ARC.
  7. adib macrumors regular


    Jun 11, 2010
    GC? Stay away from it.

    There is probably a reason why GC was short-lived. My personal experience was my app had persistent crashes on CoreGraphics when GC was turned on.

    As ARC is also a rather "new tech" that isn't much proven yet, I suggest using it only on new projects and don't convert your existing ones to ARC. Unless you're willing to spend a few days profiling and looking for leaks post-conversion.

Share This Page