0

I've noticed a significant memory usage having the following function executed by a timer:

_timer = [NSTimer scheduledTimerWithTimeInterval:0.01
                                          target:self
                                        selector:@selector(test)
                                        userInfo:nil
                                         repeats:YES];

- (void)test {
    NSRunningApplication *app = [NSWorkspace sharedWorkspace].frontmostApplication;
    app.processIdentifier;
}

The code basically does nothing.

Accessing almost any property (bundleIdentifier, bundleURL, description...) of a NSRunningApplication instance results into memory usage increasing at ~1MB/sec (considering the specified time interval). And the worst thing is that it never stops. I haven't tried to put it to the limit yet though...

I've tried to profile it using Instruments (Leaks template), but it finds no memory leaks.

Any clue?


Edit #1:

I've performed a simple experiment, creating a console application with a single swift file:

import Cocoa

while true {
    guard let app = NSRunningApplication(processIdentifier: 315) else {
        break
    }
}

Put any pid you have running. It takes a gig in a few seconds...


Edit #2:

My latest finding is that Process Type directly affects the behavior. Consider:

TransformProcessType(&psn, UInt32(processType))

If processType = kProcessTransformToBackgroundApplication or kProcessTransformToUIElementApplication, I face the issue. If process type = kProcessTransformToForegroundApplication (default value), everything works perfectly fine.

Alexey Kvasov
  • 211
  • 2
  • 4
  • And what are the new allocated objects. Instruments will tell you. 0.01 -> i think 0.1 -1.0 is a good value – Marek H Oct 08 '15 at 08:36
  • How about an autoreleasepool? – Willeke Oct 08 '15 at 13:48
  • @Willeke: autoreleasepool doesn't help. – Alexey Kvasov Oct 08 '15 at 14:30
  • @MarekH: I don't really understand your question. Pleease have a look at the screenshots: [screen1](https://monosnap.com/file/bw16w6yrLkUOrA9pu8klANFCN3GjVp) [screen2](https://monosnap.com/file/NiCofKyufs9cw5bVFiyMDwtU1rNOfM) – Alexey Kvasov Oct 08 '15 at 14:41
  • Either it's OS X version or you have more hidden code. I see NSLock there and that is something smelly. I do not have it there with this code. Try this code on empty project. Timeinterval 0.01 is a waste of resources and battery life. Shame on you for this. CPU can never sleep. I doubt you do real-time app. – Marek H Oct 08 '15 at 15:07
  • My persistent bytes are constantly on 11MB. Empty Xcode Cocoa app template + this code – Marek H Oct 08 '15 at 15:15
  • @MarekH: Obviously, it's just a test code. The real app relies on notifications. The timer with such an interval value is defined just to make the issue to appear faster. Set it 0.5, or 1.0... whatever. It just makes memory usage increasing slower. Thanks anyway! – Alexey Kvasov Oct 08 '15 at 15:25
  • Try another OS X SDK / Xcode. It can be bug in underlaying framework (frequent with Apple). You might have zombies turned on. Also try reboot. – Marek H Oct 08 '15 at 15:34
  • When I do the test with the pid of the Finder it is ok, with the pid of Activity Monitor memory increases. – Willeke Oct 08 '15 at 16:43
  • Never mind, Finder also increases memory. autoreleasepool fixes it. – Willeke Oct 08 '15 at 16:58

0 Answers0