NSWorkspace sharedWorkspace runningApplications causing memory leak; alternative option?

974 views Asked by At

I'd like to know whether anyone has a suggestion for an alternative to using runningApplications, as something like the following appears to be leaking memory:

https://openradar.appspot.com/24067155 https://github.com/bradjasper/NSRunningApplicationMemoryLeaks

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkApps:) userInfo:nil repeats:YES];

}

- (void) checkApps : (id) sender {

    @autoreleasepool {

        NSArray *appsArray = [[NSWorkspace sharedWorkspace] runningApplications];

        for (NSRunningApplication *a  in appsArray) {
            NSLog(@"%@", [a localizedName]);
        }

    }

}    

Is the only option to wait until Apple provides a solution? I'm working in a sandboxed environment, so some NSTask-based alternatives may not work. Thanks in advance for any ideas.

2

There are 2 answers

1
elasticthreads On BEST ANSWER

The answer to your question, is there another sandboxable option?: is no. This is how you're supposed to look for running applications.

You might try KVO (on the sharedWorkspace's runningApplications property) instead. The documentation suggests doing just that rather than what you're doing:

Instead of polling, use key-value observing to be notified of changes to this array property.

0
William Gustafson On

After a fair amount more troubleshooting, I eventually found that the memory leak issue only occurs when building/running the app/project from Xcode (Version 7.2 (7C68)). If I build the project, and then head into Finder and manually launch the app built, memory allocation appears to stabilize.

I don't have Zombie objects enabled, and I've made no changes from the default project settings. This must be a bug within Xcode.