I have an audio application that processes numerous audio streams into ten-second clips, compresses the clips, and saves them to disk. Naturally, I wanted to move from single-threaded processing to multithreaded processing for the clip compression/writing. That was easily done using the NSOperationQueue and the NSInvocation classes, and it worked nicely, apart from a bad memory leak that I can't seem to eradicate. So I finally wrote a small test application to encapsulate what I was doing in the larger program, and it too has a leak -- a big one!

The test application simply spawns 10000 threads whose sole job is to display "Hi, I am thread xx" on the console. It then sleeps and spawns 10000 more. At the end of the run some 38 MB of real memory has been used, and it never goes down. (I'm not using garbage collection, but I think I'm doing things correctly with the autorelease pool.)

Can someone tell me what I'm doing wrong?

Thanks!

Mike

---------------------------------------------------------------------------------------------------
#import <Foundation/Foundation.h>


@interface OperationQueueTester : NSObject
{
}

- (void) run;
- (void) sayHi:(id)blah;
- (void) queueOperations:(int) count;

@end

@implementation OperationQueueTester

- (id) init
{
        return self;
}

- (void) run
{
        // Allow a bit of time to have a look at the initial memory usage.
        NSLog(@"Starting in 5 seconds...");
        sleep(5);
        
        // Throw a bunch of simple operations into the operation queue.
        [self queueOperations:10000];
        sleep(10);
        [self queueOperations:10000];

        // Allow a bit of time to look at the final memory usage.
        NSLog(@"All threads have finished processing.");
        sleep(3600);
}

- (void) queueOperations:(int) count
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSInvocationOperation *processor;
NSOperationQueue *operationQueue = [[[NSOperationQueue alloc] init] autorelease];
        NSLog(@"Preparing to queue operations");
        
        // Add multiple "process:" operations to the queue with no argument.
        for (int i = 0; i < count; i++)
        {
processor = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sayHi:) object:nil];
                [operationQueue addOperation:processor];
        }
        NSLog(@"All threads queued.");
        [operationQueue waitUntilAllOperationsAreFinished];
        [pool release];
}

- (void) sayHi:(id)blah
{
        static int counter = 0;
        NSLog(@"Hi. I am thread %d", counter++);
}

@end

/**
Main routine, in which an object is instantiated to queue a large number of operations into an operation queue.
**/
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        
OperationQueueTester *queueTester = [[OperationQueueTester alloc] init];
        [queueTester run];
    [pool drain];
    return 0;
}

_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to