(Update) Re: Objective-C 2.0 on FreeBSD; garbage collection, anyone?

Nerius Landys nlandys at gmail.com
Fri Mar 12 04:15:57 UTC 2010

> Sorry for the delay. Medical problem.
> Here's what I know.
> 1) Under FreeBSD 8.x OBJC APPEARS NOT to use garbage collection. I
> looked at the source and the GC routines aren't defined anywhere and I
> stepped through the assembly language and the allocation routing call
> malloc(). There is a specific conditional in the FreeBSD compiler that
> cloosed malloc() or GC_malloc() depending on whether a GC variable is
> defined.
> 2) Even through you can specify -fobjc-gc on a compile line using the
> FreeBSD compiler, it appears there is no warning issued if GC is not
> enabled.
> 3) GCC 45 in the ports directory DOES NOT include OBJC. Neither does 44.
> 4) I downloaded GCC and compiled it this way:
> ../gcc-4.4.3/configure --disable-nls --disable-rpath
> --prefix=/usr/local/gcc44 --with-system-zlib --enable-languages="c,c
> ++,objc" --enable-objc-gc
> I compiled an ObjC program this way:
> /usr/local/gcc44/bin/gcc -fobjc-gc -g -O prog.m -lobjc_gc -static
> -pthread
> I checked and the GC routines are in there.
> I have a Core2 Quad, 8GB of RAM, and 32 GB of swap. I ran "a" program
> (below). No swap activity.  No swap consumption. No increase in memory
> consumption. Five minutes. It hasn't crashed.
> #include <sys/types.h>
> #import <objc/Object.h>
> int main(int argc, const char *argv[]) {
>  while (1) {
>    Object *obj = [[Object alloc] init];
>    u_int h = [obj hash];
>  }
>  return 0;
> }

Wow thanks, that really helps.  I now understand more about how this
stuff works.  I'm kind of new to C programming in general, and library
linking and all that sort of stuff.

My gcc command is like this now:

  /usr/opt/gcc44/bin/gcc -o main Main.m MyObj.m -lobjc_gc -lpthread -static

The "-static" is nice because my libobjc_gc.so is in a non-standard
location on my system, namely /usr/opt/gcc44/lib/ , and so I don't
have to specify LD_LIBRARY_PATH if I compile with "-static" (but as a
result the binary is larger and takes more memory to run, by about 700

The "-lpthread" is necessary because pthreads are used as part of the
garbage collection process.

The "-lobjc_gc" is used instead of "-lobjc" to enable garbage collection.

I compiled gcc 4.4.3 (downloaded directly from links on gcc.gnu.org)
myself (not from ports), with
  ./configure --prefix=/usr/opt/gcc44 --enable-objc-gc

My source code looks like this:

#import <pthread.h>
#import "MyObj.h"
int main(int argc, const char *argv[]) {
  while (YES) {
    MyObj *obj = [[MyObj alloc] init];
    [obj hash];
  return 0;

#import <objc/Object.h>
@interface MyObj : Object {}

#import "MyObj.h"
@implementation MyObj

I get no memory increases in my program.

I have not tried to get garbage collection to work with GNUstep yet.
It very well may not work, because the garbage collection is probably
tied to objc_gc (for example instead of gnustep-base or
gnustep-runtime or something like that).  Like I said I don't know too
much about C programming at this point.  I do see a libgnustep-base.so
in my GNUstep installation, but I don't see anything of the form

I'm going to write some programs just using Object, not using GNUstep
at this point.  I think I'll wait until I get an Apple and an iPhone
before I try to use the other object classes (NSObject and friends).

More information about the freebsd-questions mailing list