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

Nerius Landys nlandys at gmail.com
Wed Mar 10 19:14:28 UTC 2010


>> 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.

Well thanks for that advice.  My new program looks like this:

#import "GarbageObj.h"

int main(int argc, const char *argv[]) {
  int inx = 0;
  while (YES) {
    inx++;
    GarbageObj *obj = [[GarbageObj alloc] init];
    [obj foo];
    if (inx == 100000) {
      inx = 0;
      sleep(1);
    }
  }
  return 0;
}



Unfortunately the memory usage is still steadily increasing.  No
garbage collection even if I compile with "-fobjc-gc".  :-(


More information about the freebsd-questions mailing list