All this recent talk about GC here on cocoa-dev and on the Obj-C mailing list has come at an opportune time.

I'm working on a file compression framework that's GC-only and 32-bit. It reads a file using NSFileHandle 256KB at a time into an NSData object, processes each object into another NSData object, and writes the result to the new file via another NSFileHandle.

If I create a Foundation tool Xcode project, turn on GC and add my FW, then put something like this in main, it works fine, even if the file is > 5 GB.
        int main (int argc, const char * argv[]) {
                objc_startCollectorThread();
                NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
                NSString *path = @"path/to/file.ext";
                [MyClass processMethod:path];
                [pool drain];
                return 0;
        }
GC kicks in periodically and keeps memory usage to a reasonable level.

However, in when I use the FW in a GC-enabled Cocoa app, it sometimes fails if I call the process: method either on the main thread or another one. According to Activity Monitor, both real memory and VM grow to many GB. Sometimes GC kicks in and real memory shrinks to a few MB then starts to grow again. However, sometimes it just grows, and I get the "malloc mmap failed (error code=12)" message.

If I add something like this to the method's processing loop
        if (++block % 1000 == 0)
                objc_collect(OBJC_EXHAUSTIVE_COLLECTION);
memory usage stays reasonable in the Cocoa app.

I've tried other options in various combinations (e.g. objc_collect(OBJC_COLLECT_IF_NEEDED) and objc_clear_stack(OBJC_CLEAR_RESIDENT_STACK)), but only the above seems to help.

Any idea why GC seems to work automatically in a Foundation tool, but in a Cocoa app it sometimes needs manual invocation?

----
Karl Moskowski <kolpa...@voodooergonomics.com>
Voodoo Ergonomics Inc. <http://voodooergonomics.com/>

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to