0

Dear communtity. I has a very strange leak, which i can't catch up by instruments. Application eat a memory very quickly from 200M to 450M for one cycle of removing. Call threes list for objects list show me that a problem in NSManagedObjectContext NSFetchRequest-> processRecentChanges-> propagatePendingDeletesAtEndOfEvent. Data amount is not big, current sqllite store is 37Mb, and memory doesn't free after process is done.

NSError *error = nil;
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *requestCarrier = [[NSFetchRequest alloc] init];
[requestCarrier setEntity:[NSEntityDescription entityForName:@"DestinationsListForSale"
                                      inManagedObjectContext:moc]];
[requestCarrier setPredicate:[NSPredicate predicateWithFormat:@"(carrier.name == %@)",
                              carrierName]];
NSArray *destinationsListsForSale = [moc executeFetchRequest:requestCarrier error:&error];

if (error) NSLog(@"Failed to executeFetchRequest to data store: %@", [error localizedDescription]); 
for (NSManagedObject *destinationForSale in destinationsListsForSale) [moc deleteObject:destinationForSale];


[requestCarrier release], requestCarrier = nil;
destinationsListsForSale = nil;

requestCarrier = [[NSFetchRequest alloc] init];

[requestCarrier setEntity:[NSEntityDescription entityForName:@"DestinationsListWeBuy" inManagedObjectContext:moc]];
[requestCarrier setPredicate:[NSPredicate predicateWithFormat:@"(carrier.name == %@)", carrierName]];
NSArray *destinationsListsWeBuy = [moc executeFetchRequest:requestCarrier error:&error];
if (error) NSLog(@"Failed to executeFetchRequest to data store: %@", [error localizedDescription]); 
for (NSManagedObject *destinationWeBuy in destinationsListsWeBuy) [moc deleteObject:destinationWeBuy];

[requestCarrier release], requestCarrier = nil;
destinationsListsWeBuy = nil;

SOLUTION v.1 rebuild code to move out problem

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (NSManagedObject *destination in destinationsList)
{
    [moc deleteObject:destination];
    if (x % 10 == 0) {
        [moc save:&error];
        if (error) NSLog(@"Failed to save to data store removePreviousDestinationsFromMainDatabaseForCarrier: %@", [error localizedDescription]);
        [pool drain],pool = nil;
        pool = [[NSAutoreleasePool alloc] init];
    }
    x++;
}
Alex
  • 393
  • 6
  • 21
  • Is this using reference counting or garbage collection? Do you measure the leak before or after you have returned to the run loop? – JeremyP Jan 10 '11 at 15:34
  • reference counting. I can't using garbage, bcs my MCPKit framework don't support it. – Alex Jan 10 '11 at 22:39

2 Answers2

1

Have you tried just fetching the NSMangedObjectID in your code, as suggested in this answer?

Community
  • 1
  • 1
paulbailey
  • 5,328
  • 22
  • 35
  • that is doesn't work well,i try to do this and memory volume was drop to 35M, tnx – Alex Jan 10 '11 at 18:54
  • [request setEntity:[NSEntityDescription entityForName:@"DestinationsListForSale" inManagedObjectContext:managedObjectContext]]; NSPredicate *predicateLastUsedProfit = [NSPredicate predicateWithFormat:@"lastUsedProfit > 0"]; [request setPredicate:predicateLastUsedProfit]; NSArray *destinations = [managedObjectContext executeFetchRequest:request error:&error]; NSNumber *totalProfitNumber = [destinations valueForKeyPath:@"@sum.lastUsedProfit"]; Can i used a same for this part of code? – Alex Jan 10 '11 at 18:58
0

What kind of size data set are you running this on? Are you sure it's really a leak and not just normal memory usage increase associated with dealing with a very large data set?

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75