Objective-C 2.0 on FreeBSD; garbage collection, anyone?

Alex Stangl alex at stangl.us
Wed Mar 10 13:44:07 UTC 2010

On Wed, Mar 10, 2010 at 01:00:59AM -0800, Nerius Landys wrote:
> #import "GarbageObj.h"
> int main(int argc, const char *argv[]) {
>   while (YES) {
>     GarbageObj *obj = [[GarbageObj alloc] init];
>     [obj foo]; // foo is does literally nothing.
>   }
>   return 0;
> }
> I am compiling this program and running it, and without the "release"
> calls there, it certainly is using up more and more memory every
> second.  Definitely no garbage collection happening.  I then modified
> the GNUmakefile to make sure that the option "-fobjc-gc" was being
> passed to gcc, and verbose output from make assured me that this was
> the case.  However, my program sill did not garbage collect (3 gigs of
> RAM, then a segfault).  I then tried the gcc option "-fobjc-gc-only"
> and gcc42 reported that it did not recognize that option.  The options
> are described here:
> http://developer.apple.com/mac/library/documentation/DeveloperTools/gcc-4.0.1/gcc/Objective_002dC-and-Objective_002dC_002b_002b-Dialect-Options.html

While I am not very familiar with Objective C, I can tell you that GC
generally runs in the background, using idle time to scavenge memory.
(It's not counted pointers, synchronously freeing memory immediately.)
So if you race to allocate memory in an infinite loop like this, you are
destined to exhaust memory, GC or no, unless the runtime is designed to
force a GC on alloc in low memory conditions.

Try putting some sort of sleep in the middle of your loop and see if GC
kicks in and you get more of a sawtooth memory usage pattern.


More information about the freebsd-questions mailing list