Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: currency converter tutorial and "self"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: currency converter tutorial and "self"



On 11 Dec 2009, at 07:31, Kai Brüning wrote:

>> I am trying to learn to code without garbage collection,
> Why? You just add another concept to learn, which many have problems with (although it *is* quite easy). And GC is the thing of the future, retain/release the past. Proof? Xcode uses GC,

*Well*... I actually think that non-GC still has some advantages, particularly in terms of management of scarce resources (e.g. file descriptors).

With GC, the problem is that finalisation will happen some time *after* the object becomes garbage, and it's possible therefore for objects holding scarce resources to live on long past the point where they were needed, causing resource exhaustion.

The two primary solutions to this are, ironically, reference counting (in which case you can't use -retain or -release because they're intercepted in the message despatch code, and you can't rely on containers doing it automatically and so on), and forcibly triggering garbage collection.  The latter is usually what the garbage collector does, for instance, when it runs out of GC'd memory; if you want to force collection yourself, you could do

  objc_collect (OBJC_FULL_COLLECTION | OBJC_WAIT_UNTIL_DONE);

For instance, if you open a lot of files and store the descriptors in your objects, calling close() on -finalize, you might want to use code like this when opening them:

  int
  gc_open (const char *path, int oflag) {
    int fd;

    fd = open (path, oflag);

    // If we failed because we're out of descriptors, collect garbage and retry
    if (fd < 0 && (errno == EMFILE || errno == ENFILE)) {
      objc_collect (OBJC_FULL_COLLECTION | OBJC_WAIT_UNTIL_DONE);
      fd = open (path, oflag);
    }

    return fd;
  }

Even then, you might well find that the framework starts going wrong if you e.g. run out of file descriptors; I have no idea if Apple is using code like the above in the framework code or not (if not, that's probably a bug), but I'd also observe that many third party libraries *won't* be doing the above (and that isn't a bug; they aren't expecting to have a garbage collector to worry about) and so you might get failures from them too.

(All of this is a somewhat theoretical problem for many applications, since the garbage collector is likely to finalise objects more than quickly enough for most purposes.  It's good to understand though, and if you have an application that is going to open files at a furious rate, or create lots of sockets or some such, you might need to.)

That said, I'm sure a lot of applications will be written using GC in the future.

Kind regards,

Alastair.

--
http://alastairs-place.net



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >currency converter tutorial and "self" (From: "Patrick J. Collins" <email@hidden>)
 >Re: currency converter tutorial and "self" (From: "Patrick J. Collins" <email@hidden>)
 >Re: currency converter tutorial and "self" (From: Kai Brüning <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.