I am currently struggling with a major problem with my application, which challenges (my) logic. I am sure that I am doing something wrong, but I do not see that.
I execute the base executeFetchRequest on my main data object, but the array of returned managed objects is never output ONLY when I run it on the iPhone, under the simulator it works exactly as expected.
This is despite the fact that NSAutoreleasePool is used to minimize memory. I also checked with the tools, and there are no leaks, just increasing the memory allocation (by [NSManagedObject (_PFDynamicAccessorsAndPropertySupport) allocWithEntity:] '). In my actual application, this ultimately leads to a call to didReceiveMemoryWarning.
I released a minimal program that reproduces the problem below. I tried various things, such as the failure of all objects before draining the pool, but without joy. If I provided an NSError pointer to retrieve, then the error will not be returned. No background threads.
#import <mach/mach.h>
#import <mach/mach_host.h>
+(natural_t) get_free_memory {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS) {
NSLog(@"Failed to fetch vm statistics");
return 0;
}
natural_t mem_free = vm_stat.free_count * pagesize;
return mem_free;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
NSManagedObjectContext *context = [(id)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"INITIAL FREEMEM: %d", [RootViewController get_free_memory]);
for(int i=0; i<20; i++) {
NSAutoreleasePool *looppool = [[NSAutoreleasePool alloc] init];
NSLog(@"FREEMEM: %d", [RootViewController get_free_memory]);
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"TestEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSArray *array = [context executeFetchRequest:request error:nil];
[request release];
[looppool drain];
}
NSLog(@"FINAL FREEMEM: %d", [RootViewController get_free_memory]);
}
When I run above on the simulator, I get the following output (which seems reasonable to me):
2011-06-06 09:50:28.123 renniksoft[937:207] INITIAL FREEMEM: 14782464
2011-06-06 09:50:28.128 renniksoft[937:207] FREEMEM: 14807040
2011-06-06 09:50:28.135 renniksoft[937:207] FREEMEM: 14831616
2011-06-06 09:50:28.139 renniksoft[937:207] FREEMEM: 14852096
2011-06-06 09:50:28.142 renniksoft[937:207] FREEMEM: 14872576
2011-06-06 09:50:28.146 renniksoft[937:207] FREEMEM: 14897152
2011-06-06 09:50:28.149 renniksoft[937:207] FREEMEM: 14917632
2011-06-06 09:50:28.153 renniksoft[937:207] FREEMEM: 14938112
2011-06-06 09:50:28.158 renniksoft[937:207] FREEMEM: 14962688
2011-06-06 09:50:28.161 renniksoft[937:207] FREEMEM: 14983168
2011-06-06 09:50:28.165 renniksoft[937:207] FREEMEM: 14741504
2011-06-06 09:50:28.168 renniksoft[937:207] FREEMEM: 14770176
2011-06-06 09:50:28.174 renniksoft[937:207] FREEMEM: 14790656
2011-06-06 09:50:28.177 renniksoft[937:207] FREEMEM: 14811136
2011-06-06 09:50:28.182 renniksoft[937:207] FREEMEM: 14831616
2011-06-06 09:50:28.186 renniksoft[937:207] FREEMEM: 14589952
2011-06-06 09:50:28.189 renniksoft[937:207] FREEMEM: 14610432
2011-06-06 09:50:28.192 renniksoft[937:207] FREEMEM: 14630912
2011-06-06 09:50:28.194 renniksoft[937:207] FREEMEM: 14651392
2011-06-06 09:50:28.197 renniksoft[937:207] FREEMEM: 14671872
2011-06-06 09:50:28.200 renniksoft[937:207] FREEMEM: 14692352
2011-06-06 09:50:28.203 renniksoft[937:207] FINAL FREEMEM: 14716928
However, when I run it on a real iPhone 4 (4.3.3), I get the following result:
2011-06-06 09:55:54.341 renniksoft[4727:707] INITIAL FREEMEM: 267927552
2011-06-06 09:55:54.348 renniksoft[4727:707] FREEMEM: 267952128
2011-06-06 09:55:54.702 renniksoft[4727:707] FREEMEM: 265818112
2011-06-06 09:55:55.214 renniksoft[4727:707] FREEMEM: 265355264
2011-06-06 09:55:55.714 renniksoft[4727:707] FREEMEM: 264892416
2011-06-06 09:55:56.215 renniksoft[4727:707] FREEMEM: 264441856
2011-06-06 09:55:56.713 renniksoft[4727:707] FREEMEM: 263979008
2011-06-06 09:55:57.226 renniksoft[4727:707] FREEMEM: 264089600
2011-06-06 09:55:57.721 renniksoft[4727:707] FREEMEM: 263630848
2011-06-06 09:55:58.226 renniksoft[4727:707] FREEMEM: 263168000
2011-06-06 09:55:58.726 renniksoft[4727:707] FREEMEM: 262705152
2011-06-06 09:55:59.242 renniksoft[4727:707] FREEMEM: 262852608
2011-06-06 09:55:59.737 renniksoft[4727:707] FREEMEM: 262389760
2011-06-06 09:56:00.243 renniksoft[4727:707] FREEMEM: 261931008
2011-06-06 09:56:00.751 renniksoft[4727:707] FREEMEM: 261992448
2011-06-06 09:56:01.280 renniksoft[4727:707] FREEMEM: 261574656
2011-06-06 09:56:01.774 renniksoft[4727:707] FREEMEM: 261148672
2011-06-06 09:56:02.290 renniksoft[4727:707] FREEMEM: 260755456
2011-06-06 09:56:02.820 renniksoft[4727:707] FREEMEM: 260837376
2011-06-06 09:56:03.334 renniksoft[4727:707] FREEMEM: 260395008
2011-06-06 09:56:03.825 renniksoft[4727:707] FREEMEM: 259932160
2011-06-06 09:56:04.346 renniksoft[4727:707] FINAL FREEMEM: 259555328
, , . , , , , .
, , , 16- . 1000 . , , :
for(int i=0; i<1000; i++) {
id entity = [NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:context];
[entity setValue:[NSString stringWithFormat:@"%d",i] forKey:@"name"];
[entity setValue:[NSNumber numberWithInt:i] forKey:@"value"];
}
if (![context save:nil]) {
NSLog(@"Couldn't save");
}
- , , ! , . .
, , , .