Deep Dive into iOS Memory

Mahdi Saedi
3 min readJun 29, 2022

--

Reduce Memory

why reduce memory? when we want to reduce memory we’re talking about reducing our memory footprint.

Why Reduce Memory?

The easy answer is users have a better experience. Not only will your app launch faster. The system will perform better. Your app will stay in memory longer. Other apps will stay in memory longer. Pretty much everything’s better.

Clean and dirty pages

memory given to you pages that can hold multiple objects on the heap and some objects can span multiple pages. each page is typically 16KB and they can come in clean or dirty.

memory use of your app is the number of pages multiplied by the page size.

Example:

a good example of this would be JPEG if we have a JPEG that’s 50KB when its memory-mapped into 4 Pages that are not full so it can be used for other things.

Memory Compressor

iOS doesn’t have a traditional disk swap system. Instead, it uses a memory compressor. This was introduced in iOS 7.

Apple

when I have a dictionary that I’m using for caching, it uses up 5 pages right now but if I haven’t accessed this in a while and the system needs some space it can squeeze it down into one page, but I’m saving space or I’ve got 4 extra pages.

we shouldn’t cache too much because when we cache we are trying to save CPU from doing repeated work, but when we cache too much we are using up all of our memory and that can have problems with the system.

keep in mind that, depending on the device, your limit will change. So you won’t be able to use as much memory on a 1-gigabyte device as you would on a 4-gigabyte device

Apple

Images

The most important thing about images to remember is that the memory use is related to the dimensions of the image, not its file size.

Example:

2048 x 1536

when I have an image with 2048 length and 1536 width with 590 KB Image Size it doesn’t allocate 590KB in memory. but how much memory does it use really? 10 MB, yes 10MB that huge. how can calculate this 10 MB?

2048 pixels x 1536 pixels x 4 bytes per pixel

how do images work on iOS?

There’s a load, a decode, and a render phase.

Load: takes this 590-kilobyte JPEG file, which is compressed, and loads it into memory.

Decode: converts that JPEG file into a format that the GPU can read. Now, this needs to be uncompressed, making it 10 megabytes.

why 4 Byte?

SRGB: (Standard Red Green Blue)

Now, 4 bytes per pixel we got by the SRGB format. This is typically the most common format that images in graphics are. It’s 4 bits per pixel, so you have 1 byte for red, 1 byte for green, 1 byte for blue, and an alpha component.

Comparison UIImage and ImageIO

UIImage is expensive for sizing and resizing
• Will decompress original image into memory
• Internal coordinate space transforms are expensive

ImageIO can read image sizes and metadata information without dirtying memory
ImageIO can resize images at cost of resized images only

Summary

Memory is a finite and shared resource
Monitor memory use when running from Xcode
Let iOS pick your image formats
Use ImageIO for downsampling images
Unload large resources that are off-screen
Use memory graphs to further understand and reduce memory footprint

Source: WWDC Apple

--

--