This code tells me the amount of memory I'm currently consuming:
-(u_int)memoryUsage {
u_int result = 0;
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
TASK_BASIC_INFO,
(task_info_t)&info,
&size);
if( kerr == KERN_SUCCESS ) {
result = (info.resident_size / 1024) / 1024;
} else {
NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}
return result;
}
To test this, I wrote this:
NSLog(@"USAGE: %u mb",[self memoryUsage]);
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < 1000000; ++i) {
[array addObject:@"Hello!"];
}
NSLog(@"USAGE: %u mb",[self memoryUsage]);
[array removeAllObjects];
NSLog(@"USAGE: %u mb",[self memoryUsage]);
The output is:
5 mb
10 mb
10 mb
After making an array with 1000000 strings, memory was increased by 5 mb. But after cleaning it up, memory usage is not modified.
Why is that?