• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSOperationQueue Memory Leak
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSOperationQueue Memory Leak


  • Subject: NSOperationQueue Memory Leak
  • From: Mike Simmons <email@hidden>
  • Date: Wed, 06 Aug 2008 08:38:09 -0500

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSOperationQueue Memory Leak
      • From: "Brock Batsell" <email@hidden>
    • Re: NSOperationQueue Memory Leak
      • From: "Michael Ash" <email@hidden>
    • Re: NSOperationQueue Memory Leak
      • From: Nick Zitzmann <email@hidden>
  • Prev by Date: Command Line Argument - Choosing a Style
  • Next by Date: NSSpellChecker (foundation) broken for custom NSSpellServer servers
  • Previous by thread: Re: Command Line Argument - Choosing a Style
  • Next by thread: Re: NSOperationQueue Memory Leak
  • Index(es):
    • Date
    • Thread