I have an app which caches large images so that user do not wait for imageWithContentsOfFile. As a general rule I cache a previous and next image.
1) Can I make this caching adaptive based on the available memory in iPad ? If yes what should be the threshold ? Below is the function to calculate the available memory
-(void) report_memory {
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 ) {
Log(@"Memory in use (in bytes): %u", info.resident_size);
} else {
Log(@"Error with task_info(): %s", mach_error_string(kerr));
}
}
2) I know there is no way to (except the private/undocumented API) know the memory level warning, otherwise it could be a good factor to determine how many pages I can cache. But just to confirm can I use them in some way.
3) Right now I am thinking of caching 3 screens (which have 6 images) and in case my ViewController receive memory warning I unload all screens except the visible one and reset number of screens to cache to 2 (4 images). But I don’t found it optimized because either I am caching less than what is possible or in some conditions even loading 4 leads to crash.
If you are looking to cache as much data as possible without causing the application to crash, you can always make use of the function didReceiveMemoryWarning to remove excess caches when needed. Within this function, you don’t have to clear out everything. You could use it to selectively clear up enough room for the current situation and then continue caching until it fires off this warning again.
An alternative would be to start a cache routine until the warning fires. This would allow you to create a count of items that were able to be cached. Once this count is reached, you could have your caching iterations stay far enough below that count to avoid issues.
Not exactly an in-depth explanation, but these are some ideas for making use of the standard methods available that should still be able to achieve your goal.