2014-11-04 49 views
1

我發現我的示例項目存在問題。我只是簡單地創建一個scheduledTimer來「標記」一個標籤,然後當我達到我想要的結果時,我使計時器無效,並且我設置了另一個計時器,這次是「時鐘」。 這是我的代碼使用堆內存的持久分配NSTimer

// 
// ViewController.m 
// 
// 
// 
// 
// 

#import "ViewController.h" 

#define ANIMATION_INTERVAL    0.07 // in secondi 
#define ANIMATION_DURATION    1 // in secondi 

@interface ViewController() 
{ 
    int contatore; 
    NSString *hour; 
    NSString *minute; 
    NSString *second; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    contatore = 0; 

    [self startTimeAnimation]; 


} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


-(void)startTimeAnimation 
{ 
    NSTimer * animationTimer = [NSTimer scheduledTimerWithTimeInterval:ANIMATION_INTERVAL target:self selector:@selector(timeout:) userInfo:nil repeats:YES]; 
} 

-(void)timeout: (NSTimer *)timer 
{ 
    // Simulate of the Timer Duration with a counter 
    if (contatore < ceilf(ANIMATION_DURATION/ANIMATION_INTERVAL)) 
    { 
     // Proceed with animation 
     contatore++; 

     int tempHour = arc4random_uniform(24); 
     int tempMinute = arc4random_uniform(60); 
     int tempSecond = arc4random_uniform(60); 

     if (tempHour < 10) 
     { 
      hour = [NSString stringWithFormat:@"0%d", tempHour]; 
     } 
     else 
     { 
      hour = [NSString stringWithFormat:@"%d", tempHour]; 
     } 

     if (tempMinute < 10) 
     { 
      minute = [NSString stringWithFormat:@"0%d", tempMinute]; 
     } 
     else 
     { 
      minute = [NSString stringWithFormat:@"%d", tempMinute]; 
     } 

     if (tempSecond < 10) 
     { 
      second = [NSString stringWithFormat:@"0%d", tempSecond]; 
     } 
     else 
     { 
      second = [NSString stringWithFormat:@"%d", tempSecond]; 
     } 

     _orarioLbl.text = [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second]; 
    } 
    else 
    { 
     // Stops animation 
     [timer invalidate]; 
     [timer release]; 
     contatore = 0; 



     [self setActualTime]; 


     // Starts clock 
     NSTimer *clockTimer = [NSTimer timerWithTimeInterval:.5f target:self selector:@selector(updateClock) userInfo:nil repeats:YES]; 
     [[NSRunLoop mainRunLoop] addTimer:clockTimer forMode:NSRunLoopCommonModes]; 
    } 
} 

-(void)updateClock 
{ 
    [self setActualTime]; 
} 


-(void)setActualTime 
{ 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 

    [dateFormatter setDateFormat:@"HH"]; 
    hour = [dateFormatter stringFromDate:[NSDate date]]; 

    [dateFormatter setDateFormat:@"mm"]; 
    minute = [dateFormatter stringFromDate:[NSDate date]]; 

    [dateFormatter setDateFormat:@"ss"]; 
    second = [dateFormatter stringFromDate:[NSDate date]]; 

    _orarioLbl.text = [NSString stringWithFormat:@"%@:%@:%@", hour, minute, second]; 
    [dateFormatter release]; 
} 

@end 

自從我開始「時鐘」,記憶還停留在19/20 MB恆永久分配。當計時器更新分鐘值時,持續分配會增加,正如您在gif中看到的一樣!這怎麼可能?然而,對於一個簡單的時鐘來說,19MB的內存也太多了,不是嗎? 儀器分析,新的分配都是關於CoreGraphics的!

animated gif Instruments

EDIT我測試另一設備上和持久分配降低。我不知道爲什麼,但我用這種方式解決了。謝謝

回答

2

NSTimer保留其目標,並可能導致保留週期。抓住一個弱引用self,並設置爲目標:

__weak typeof(self) weakSelf = self; 
NSTimer * animationTimer = [NSTimer scheduledTimerWithTimeInterval:ANIMATION_INTERVAL target:weakSelf selector:@selector(timeout:) userInfo:nil repeats:YES]; 

__weak typeof(self) weakSelf = self; 
NSTimer *clockTimer = [NSTimer timerWithTimeInterval:.5f target:weakSelf selector:@selector(updateClock) userInfo:nil repeats:YES]; 

我還建議你創建一個屬性爲dateFormatter,爲NSDateFormatter s爲昂貴創建。你爲什麼要發佈dateFormatter?你沒有使用ARC?

+0

不,我沒有使用ARC,因爲我不得不在ARC中尚未轉換的最大項目中實現此示例。編輯:使用這些更改時,內存分配中的跳轉比之前更大。從50/60分配,到1600! – 2014-11-04 11:29:48