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

Scott Bennett bennett at cs.niu.edu
Fri Mar 12 05:58:44 UTC 2010


     On Wed, 10 Mar 2010 11:14:25 -0800 Nerius Landys <nlandys at gmail.com>
wrote:
>>> 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".  :-(
>
     If your program never frees any memory, then there is never any
garbage to collect.  QED.


                                  Scott Bennett, Comm. ASMELG, CFIAG
**********************************************************************
* Internet:       bennett at cs.niu.edu                              *
*--------------------------------------------------------------------*
* "A well regulated and disciplined militia, is at all times a good  *
* objection to the introduction of that bane of all free governments *
* -- a standing army."                                               *
*    -- Gov. John Hancock, New York Journal, 28 January 1790         *
**********************************************************************


More information about the freebsd-questions mailing list