New in F-Script 2.0 is a fast, concurrent, generational garbage collector. It was a lot of work to implement. Fortunately, Apple did all of it, as F-Script 2.0 uses the new Cocoa garbage collector. As with Objective-C, when you run F-Script in GC mode you no longer have to manage retain counts and to care for cyclic references. You can still use F-Script in non-GC mode and you can embed F-Script in GC or non-GC application.
Garbage collection is performed concurrently, on its own thread. Another notable aspect is that it works both for objects and for raw memory buffers. Actually, for raw memory buffers, you can either use managed or unmanaged memory. In Objective-C, managed memory (i.e. memory with automatic garbage collection) is allocated using functions such as NSAllocateCollectable(), and unmanaged memory is allocated, as usual, using functions such as malloc().
F-Script 2.0 lets you use unmanaged memory as usual (i.e., with the malloc: method) and introduces new methods, in the FSPointer class, for dealing with managed memory:
+(FSGenericPointer *)allocateCollectable:(NSUInteger)size options:(NSUInteger)options
This method allocates a collectable (i.e. managed) memory buffer with the NSAllocateCollectable() function, then creates and returns an FSGenericPointer instance pointing to this buffer, or nil if the memory cannot be allocated. If you want to pass 0 as option (which means that the memory buffer is not scanned for pointers by the garbage collector), you might prefer to invoke the shorter convenience method:
+(FSGenericPointer *)allocateCollectable:(NSUInteger)size
Features such as weak references, explicit GC control, etc. are all available from F-Script using the standard methods provided by Cocoa. For example, here is a interactive session that shows the use of NSValue to hold a weak reference that is automatically set to nil when the referenced object is collected:
> myObject := 'hello world'
> myWeakReference := NSValue valueWithNonretainedObject:myObject
> myWeakReference nonretainedObjectValue
'hello world'
> myObject := 22
> myWeakReference nonretainedObjectValue
nil
You can control the garbage collector using the standard NSGarbageCollector Cocoa class. For example, the following code asks for a full garbage collection:
NSGarbageCollector defaultCollector collectExhaustively
The following code disables garbage collection temporarily:
NSGarbageCollector defaultCollector disable.
...do some stuff...
NSGarbageCollector defaultCollector enable.
Thanks, I really missed F-Script when I enabled GC on SolarSeek =)