NSRunningApplication and/or NSWorkspace.runningApplications leaks memory

Originator:bjasper
Number:rdar://24067155 Date Originated:1/5/2016
Status:Open Resolved:Not yet
Product:OS X SDK Product Version:Xcode Version 7.2 (7C68)
Classification:Severe Reproducible:Always
 
Summary:
NSRunningApplication has a memory leak and/or NSWorkspace.runningApplications does as well.

Calling NSRunningApplication on it's own (via runningApplicationWithProcessIdentifier) does take quite a bit longer to see the leak, but it does still seem to happen.

When called in a loop with NSWorkspace.runningApplications, the problem happens immediately and pretty quickly grows out of control (hits 100mb within a minute or two).

I usually use KVO on runningApplications, but that was leaking as well. I traced the problem down to what is attached, which I think is a simplified version.

I would never use a timer like this in production to monitor apps, but this is just a sample project to demonstrate the severity of the problem. This still happens with KVO and also seems to happen with alternate methods like NSWorkspaceDidLaunchApplicationNotification when using NSRunningApplication.

Steps to Reproduce:
1. Run sample application
2. Watch memory usage climb and climb and climb

Expected Results:
Memory usage to stabilize and not grow out of control

Actual Results:
Memory usage grows out of control

Version:
Xcode Version 7.2 (7C68) & OS X 10.11.2 (15C50

Notes:
Spent all day debugging this as it's causing a big memory leak in my application. Over time memory grows to hundreds of MB and my users complain, which makes me sad. Hopefully this can be solved.

Originally happened with KVO on [NSWorkspace sharedWorkspace].runningApplications, but completely eliminated that by calling it on a NSTimer, which I would normally never do in production—but demonstrates the problem a little bit faster. In my app it takes a bit longer to surface, but it does eventually (and consistently) surface.

Have tried autoreleasepools. Those don't seem to help. Out of ideas. Please help me Apple Support—you're my only hope!

Configuration:
Happens consistently on MacBook Pro with Xcode & OS X versions described.

Attachments:
https://github.com/bradjasper/NSRunningApplicationMemoryLeaks

Comments

Xcode bug

In my testing, this appears to be a bug with Xcode, not OS X. Only projects that I build/run via Xcode experience the memory leak issue. If I just build, then head into Finder, and manually launch the .app, memory allocation appears to stabilize.

I'm receiving this bug too.

Please help us Apple Support!

By Igor.Tarataiko at Jan. 20, 2016, 9:47 a.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!