Adaptive image caching based on available memory level or iOS memory level

I have an application that caches large images so that the user does not wait for imageWithContentsOfFile. Typically, I cache the previous and next image.

1) Can I make this caching adaptive based on the available memory in the iPad? If so, what should be the threshold? Below is the function for calculating 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 that there is no way (except for the private / undocumented API) to know the memory level warning, otherwise this can be a good factor for determining how many pages I can cache. But just to confirm if I can use them in any way.

3) Now I am thinking of caching 3 screens (which have 6 images), and if my ViewController receives a warning about memory, I unload all the screens except the visible one, and reset the number of screens for caching to 2 (4 images). But I do not think it is optimized, because either I cache less than possible, or in some conditions even loading 4 leads to a failure.

+4
source share
2 answers

If you are looking to cache as much data as possible without causing the application to crash, you can always use the didReceiveMemoryWarning function to remove extra caches if necessary. As part of this function, you do not need to clear everything. You can use it to selectively clear enough space for the current situation, and then continue caching until it resumes this warning.

An alternative would be to start the caching procedure until the warning lights up. This will allow you to create the number of items that could be cached. Once this count is reached, you may have your caching iterations stay far enough below this amount to avoid problems.

Not a very detailed explanation, but these are some ideas for using the standard methods available, which should still be able to achieve your goal.

0
source

So, having waited so long, I answer my question if someone can help.

You should avoid any undocumented API to get a warning level and take action based on this. The only suggested action to prevent memory, regardless of level, is to free up as much memory as you can. You can use heuristics based on the report_memory function (see Question) to determine how much we can cache. Although I did not conduct any tests to calculate the threshold (it should be based on total RAM). I would really like someone to run these tests and update.

The approach to reset the number of pages for caching in memory warns that works for my case.

0
source

Source: https://habr.com/ru/post/1402091/


All Articles