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 の増加を示しています。それはかなりです。

最後の反復を分析しようとしても、通常は役に立たないことに注意してください。アプリの現在の状態が何であれ、これらのオブジェクトの多くは存在します。その 2 回目または 4 回目の反復に本当に集中したいと考えています。

特定の日付または日付フォーマッターがリークしている理由については、参照カウント イベントの追跡を有効にしてから、リークしたオブジェクトのいずれかの保持/解放イベントを確認してください。いくつかの余分な保持があります。

于 2013-07-20T23:11:58.933 に答える
1

あなたが正しくしていることはinitNSDateFormatterループに入る前です。同じ日付形式 ( MM/dd/yyyy) が必要な場合は、同じ場所に配置するinitこともできます。あまりにも多くのオブジェクトを割り当てている可能性があるため、すべてを@autoreleasepool {}(別名: 自動解放内のループの内容) 内に配置することをお勧めします。

*newPointfor の内部を割り当ててから、そのオブジェクトに日付を設定しています。その後はどうしますか?

于 2013-07-20T18:43:52.140 に答える