Why my 0.3 MB image occupies around 10 MB on RAM?
5 (2)

Click to rate this post!
[Total: 2 Average: 5]

Last Updated on March 13, 2022 by Deya Eldeen

You are writing an application that has a long list of entries, with each entry containing an image, the total download size of all images is about 10 MB, but the images take around 200 ~ 300 MB on RAM, you wonder why 🧐?

RAM normally does not understand images that are compressed, they are stored as raw bitmaps, even if the image is compressed, it gets inflated into memory as a raw image.

Image Size on RAM = (pixels height Γ— pixels width Γ— color depth bytes)

The following image takes around 300 KB on disk and has an sRGB color profile, which is 24 bits (8 bits per channel).

Unsplash (CC0)

The size of the image on the RAM would be:

Image Size on RAM = (1665β€Š Γ— β€Š2081 Γ— 3) bytes = 9.8MB

In Swift, Kingfisher comes with an option to downsize images according to the screen scale, so you can have images in a reasonable size even if they come largely from the server.

import UIKit
import Kingfisher

extension UIImageView {
    
    func setImageAsThumb(url:String) {
        let formattedURL = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
        let scale = UIScreen.main.scale
        let resizingProcessor = ResizingImageProcessor(referenceSize: CGSize(width: 50.0 * scale, height: 50.0 * scale))
        self.kf.setImage(with: URL(string: formattedURL), placeholder: nil, options: [.processor(resizingProcessor)])
    }

}

Leave a Reply

Your email address will not be published.