2014-11-04 61 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