Reducing Memory Usage in Background
When the app is entering background, the System will relinquish a lot of memory on our behalf. One really important thing that the system will take care of is the backing graphic store. A huge amount of memory goes to maintains the view hierarchy of the app, so we are getting saving a lot here. The infamous cache associated with -imageNamed: method of UIImage is also flushed. Not the actual images, which are the entire responsibility of the app. And SQLite, Core Data and NSCache related memory is also reclaimed by the system.
But the system can’t do anything for us, so we need to make sure to accomplish a few chores.
- Release
UIImageit can be argued that images are the most pernicious object for memory. As we have said, the system will flush the cache, but we need to release any image. - Flush any cache or data that can be regenerated. Here we need to evaluate if the cache is expensive to create. If it is really expensive to create, it might be sound to keep it on memory.
- For read only files, we might need to consider to use
mmap()because it will allow the system to do some optimization, and even to reclaim memory without terminating the app.
View controllers, on the other hand, are typically very small, so we need not to worry about them. And I can hear some of you crying in the back because all the stuff that hung off from almost any view controller. And yes, it is true, most of the time, we get a lot of models, and other heavy stuff linked to out view controller, but this is the sort of thing we have to get rid of and we have addressed previously. The View Controller itself, is not that big.