1

我的方法有问题NSDateFormatter dateFromString:。有一个带有字符串日期的字典数组,我想将它们转换为NSDates. 但是每次我调用这个方法时都会有一个巨大的堆增长(我需要多次调用它,因为它是一个“更新”方法)。我也在后台线程中调用它并且 ARC 已打开。我究竟做错了什么?任何帮助,请。 在此处输入图像描述

在此处输入图像描述

更新: 函数的完整代码:

- (GraphController *) initGraphWithData: (NSArray *) points forInterval: (GraphInterval) interval andFrame: (CGRect) frame gestures: (BOOL) gestures secondGraph: (NSArray *) secondPoints graphNames: (NSArray *) graphNames

{
self = [super init];

_calendar = [NSCalendar currentCalendar];

_secondPoints = secondPoints;
_graphNames = graphNames;

[self.view setFrame:frame];

_points = [[NSMutableArray alloc] init];
double minValue = HUGE_VALF, maxValue = -HUGE_VALF;
NSDate *minDate = [NSDate date], *maxDate = [NSDate dateWithTimeIntervalSince1970:0];
NSMutableDictionary *datesTable = [[NSMutableDictionary alloc] init];          


int index = 0;
for(NSArray *pointArray in points){

    NSDictionary *point = pointArray[0];
    NSMutableDictionary *newPoint = [[NSMutableDictionary alloc] initWithDictionary:point];


    // convert date        
    NSString *dateString = (NSString *)[point objectForKey:@"date"];
    NSLog(@"dateString to convert: %@", dateString);
    [_dateFormatter setDateFormat:@"MM/dd/yyyy"];
    NSDate *date = [_dateFormatter dateFromString: dateString];
    [newPoint setObject: date forKey:@"date"];


    // convert numbers
    [newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[point objectForKey:@"value"]) floatValue]] forKey:@"value"];
    if(secondPoints)
        [newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[secondPoints[index] objectForKey:@"value"]) floatValue]] forKey:@"secondValue"];
    [newPoint setObject: [NSNumber numberWithInt:index]  forKey:@"index"];

    // min and max
    if([[newPoint objectForKey:@"value"] floatValue] < minValue)
        minValue = [[newPoint objectForKey:@"value"] floatValue];
    if([[newPoint objectForKey:@"value"] floatValue] > maxValue)
        maxValue = [[newPoint objectForKey:@"value"] floatValue];

    // check second value
    if(self.secondPoints){            
        if([[newPoint objectForKey:@"secondValue"] floatValue] < minValue)
            minValue = [[newPoint objectForKey:@"secondValue"] floatValue];
        if([[newPoint objectForKey:@"secondValue"] floatValue] > maxValue)
            maxValue = [[newPoint objectForKey:@"secondValue"] floatValue];
    }

    if([[newPoint objectForKey:@"date"] timeIntervalSince1970] > [maxDate timeIntervalSince1970])
        maxDate = [newPoint objectForKey:@"date"];
    if([[newPoint objectForKey:@"date"] timeIntervalSince1970] < [minDate timeIntervalSince1970])
        minDate = [newPoint objectForKey:@"date"];

    [self.points addObject:newPoint];
    [datesTable setObject:newPoint  forKey: [[NSNumber numberWithDouble:[date timeIntervalSince1970]] stringValue] ];

    index++;            

}




// set draw parameters

_drawVars = [[NSMutableDictionary alloc] init];

NSDate *startSearchDate;
switch (interval) {
    case GraphIntervalWeek:
        startSearchDate = [maxDate dateByAddingTimeInterval:-7*24*3600];
        break;
    case GraphIntervalMonth:
        startSearchDate = [maxDate dateByAddingTimeInterval: -31*24*3600];
        break;
    case GraphIntervalSixMonths:
        startSearchDate = [maxDate dateByAddingTimeInterval:-6*31*24*3600];
        break;
    case GraphIntervalYear:
        startSearchDate = [maxDate dateByAddingTimeInterval:-365*24*3600];
        break;
    case GraphIntervalAllTime:
        break;
    default:
        break;
}

NSMutableDictionary *searchPoint;

while(searchPoint == nil && [startSearchDate timeIntervalSince1970] > [minDate timeIntervalSince1970]){
    searchPoint = [datesTable objectForKey:[[NSNumber numberWithDouble:[startSearchDate timeIntervalSince1970]] stringValue]];
    startSearchDate = [startSearchDate dateByAddingTimeInterval:-24*3600];
}

if(searchPoint != nil && interval != GraphIntervalAllTime)
 [self.drawVars setObject:[searchPoint objectForKey:@"index"] forKey:@"startDrawIndex"];
else
 [self.drawVars setObject:[NSNumber numberWithInt:0] forKey:@"startDrawIndex"];

[self.drawVars setObject:[[NSNumber numberWithFloat:minValue] copy] forKey:@"minValue"];
[self.drawVars setObject:[[NSNumber numberWithFloat:maxValue] copy] forKey:@"maxValue"];
[self.drawVars setObject:minDate forKey:@"minDate"];
[self.drawVars setObject:maxDate forKey:@"maxDate"];
[self.drawVars setObject:datesTable forKey:@"datesTable"];



// set drawImageView
_drawArea = [[UIImageView alloc] initWithFrame: frame];
[self.view addSubview:self.drawArea];


// set overlayImageView for fingerInpect
_inspectOverlay = [[UIImageView alloc] initWithFrame:frame];
[self.view addSubview:self.inspectOverlay];

// set hintUIView for fingerInspect
_hint = [[Hint alloc] initWithFrame:frame];
[self.hint initHint];
self.hint.hidden = YES;
[self.drawArea addSubview:self.hint];    



UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
UILongPressGestureRecognizer *longPressRecognizer  = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];    


if(gestures)
    [self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:longPressRecognizer];

return self;
}
4

2 回答 2

2

我看到你正在使用 Heapshot 分析。好的。以下是更多细节:

http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/

现在,希望您的每次 Heapshot 迭代都代表在您的应用程序中执行一些操作,该操作应将其返回到基本状态。例如,打开和关闭文档。

如果是这种情况,您的数据显示每次迭代增长 500K-600K。这是相当多的。

请注意,试图分析最后一次迭代通常是没有用的;由于应用程序的当前状态是什么,这些对象中的许多都会存在。你真的想专注于第二或第四次迭代。

至于为什么特定日期或日期格式化程序泄漏,请打开引用计数事件跟踪,然后查看其中一个泄漏对象的保留/释放事件。会有一些额外的保留。

于 2013-07-20T23:11:58.933 回答
1

你做对的事情是initNSDateFormatter进入循环之前。如果您期望相同的日期格式 ( MM/dd/yyyy),您也可以将它放在相同的位置init。可能是您分配了太多对象,所以我建议将所有内容都放在一个@autoreleasepool {}(又名:自动释放内循环的内容)中。

我看到你正在分配*newPoint内部,然后你正在为该对象设置日期。你之后做什么?

于 2013-07-20T18:43:52.140 回答