NSTimer nonstop memory increasing - iOS bug?

2019-08-08 15:38发布

问题:

I'm figuring out memory issues with an iPad app using instruments - looking at the overall allocated bytes. My NSTimer causes the bytes to constantly increase, while commenting it out causes memory usage to remain static.

From viewDidLoad of my view controller:

tickTime = 1.0 / 30.0;
tickTimer = [NSTimer scheduledTimerWithTimeInterval:tickTime target: self selector:@selector(update2) userInfo: nil repeats: YES];

And the method update2:

- (void) update2 {

}

As you can see there is nothing done in the method whatsoever - yet the memory usage of the app constantly grows. If I comment out the line where I setup the timer, the memory usage remains the same.

Is this a bug in the iOS SDK? Does anyone know a workaround?

回答1:

It's a pebkac issue - I was studying the "Overall Bytes" in Instruments, which is a measure of all memory the app has ever used, rather than a measure of current memory usage.

I'm still curious how to measure the app's current total memory usage - as "Live Bytes" is around 1.5mb - even with at least 20mb of .pngs loaded.



回答2:

Does memory use drop if you do something via the UI? I.e. if you tap a button or something?

What objects are hanging about that shouldn't? Instruments gives a lot more information than just that your heap is growing.

In general, having a timer that ticks at 1/30th of a second interval is to be avoided.

  • if it is on the main thread, it'll make your app's responsiveness very choppy (at best) or totally unresponsive

  • if you are trying to do animation, use Core Animation (or one of the various Open GL patterns)

  • it'll eat battery life.



回答3:

I believe that the timer may be "firing" to fast for the iPhone to process. I had this same problem and had to slow the timer down.