ios - Persistent allocation of Heap Memory NSTimer -


I got a problem with my sample project, I just create a scheduled timer that "animate" a label And then, when I reach the result that I want, I invalidate the timer and I set each other, at the moment as "clock" This is the code that I use

  // // ViewController.m // // // #import "ViewController.h" ANIMATION_DURATION 1 // View Interface @ Interface Controller () {int contatore; NSString * hour; NSString * minutes; NSString * Second; } @end @ Implementation View Controller - (zero) viewDidoadload {[Super Viewedload]; // Setup an additional after loading the view, usually from a nib Contatore = 0; [Starttime animation]; } - (zero) did risiv memorialisation [[super davidimmemi warning]; // Any resource settlement which can be reproduced. } - (Zero) Starttime animation {NSTIMER * AnimationTimer = [Estimator Scheduled Timer with Time: Animator Target: Self Selector: @Sillector (Timeout :) User Inc.: Zero Repeats: Yes]; } - (zero) timeout: (NSTimer *) timer {// simulate timer duration with a counter if proceed with (contatore & lt; ceilf (ANIMATION_DURATION / ANIMATION_INTERVAL)) // // Animation Contour ++; Int tempHour = arc4random_uniform (24); Int tempMinute = arc4random_uniform (60); Int tempSecond = arc4random_uniform (60); If (tempHour & lt; 10) {hour = [NSString stringWithFormat: @ "0% d", tempHour]; } And {hour = [NSString stringWithFormat: @ "% d", tempHour]; } If (tempMinute & lt; 10) {minutes = [NSString stringWithFormat: @ "0% d", tempMinute]; } And {minutes = [NSString stringWithFormat: @ "% d", tempMinute]; } If (tempSecond & lt; 10) {seconds = [NSString stringWithFormat: @ "0% d", tempSecond]; } Else {second = [NSString stringWithFormat: @ "% d", TempSecond]; } _orarioLbl.text = [NSString stringWithFormat: @ "% @:% @:% @", hour, minute, seconds]; } Else {// stops animation [invalid timer]; [Timer release]; Contatore = 0; [Self set practicaltime]; // Clock Starts NSTimer * clockTimer = [NSTimer TimerView Time Span: .5f Target: Self Selector: @Sylector (UpdateClock) userInfo: Zero repeats: Yes]; [[NSRunLoop mainRunLoop] addTimer: clockTimer forMode: NSRunLoopCommonModes]; }} - (zero) UpdateClock {[auto-setactualtime]; } - (zero) set effectual time {NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; [DateFormatter setDateFormat: @ "hh"]; Hour = [datefirm string stringed: [date of NSDT]]; [DateFormatter setDateFormat: @ "mm"]; Minute = [datefirm string string: [NSDT date]]; [DateFormatter setDateFormat: @ "ss"]; Second = [datefirm string string: [date of NSDT]]; _orarioLbl.text = [NSString stringWithFormat: @ "% @:% @:% @", hour, minute, seconds]; [DateFormatter release]; } @ And  

Since I start "clock", the memory still remains constant with 19/20 MB with constant allocation. When the timer updates the value of the minute, the continuous allocation increases because you can see it in GIF! How is this possible? However, 19 MB of storage is too much for a simple clock, right? Profiling on Instruments, new allocation core graphics are about!

animated GIF tool

edit I tested it on any other device and continuously decreased allocation I do not know why, but I solved this way. Thanks

NSTimer retains its goal and maintains one The cycle can be: Hold a weak reference of itself and set it as a goal:

  __ weak type too (self) weakhold = self; NSTimer * AnimationTimer = [NSTimer Scheduled Timer With Time Interval: ANIMATION_INTERVAL Goal: Weak Self Selector: @Sillector (Timeout :) User Ink: Zero Repeats: Yes];  

and

  __ weak type too much (self) weakness = true; NSTimer * clockTimer = [NSTimer timer with time interval: .5f target: weak self selector: @sillector (updateclock) userInfo: zero repeats: yes]; Do I also suggest that you make an asset for dateFormatter, as is the cost of creating  NSDateFormatter  and why you issue a dateformer. Are you? Are not you using ARC? 


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -