Relieving memory pressure
Relieving memory pressure
- Subject: Relieving memory pressure
- From: Gabriel Zachmann via Cocoa-dev <email@hidden>
- Date: Sun, 7 Jun 2020 12:25:36 +0200
I am developing a slideshow app; one of its features is that it keeps a history
of the last N images it has displayed. It keeps kind of a cache , where it
stores, among other things, the image returned by
CGImageSourceCreateThumbnailAtIndex(), because this is one of the expensive
operations.
(You might remember, that this "thumbnail" is necessary so that all the image
processing going on in the CGImage API is done at load time, not at the point
when the CALayer is created.)
For large images, this "thumbnail" can well occupy 1 GB of RAM.
So, when many large images are shown, the app might well use 8 GB or more,
which creates heavy memory pressure (Activity Monitor), and causes heavy
stuttering.
My current approach is to calculate the amount of unused memory,
whenever a new images is put in the cache, and if that is below 1.5 GB, I
reduce the size of the cache, releasing the old images (CGImageRelease).
For computing the amount of unused memory, I use host_statistics64(),
and vmstat.free_count+vmstat.inactive_count
(vmstat is the struct filled by host_statistics64).
My approach seems to help, but
I am still wondering what is your opinion: is that a good approach?
Is the way I calculate unused memory the best / most reliable one?
One reason for asking is that I don't completely understand the way macOS
manages memory.
(There seems to be a lot of "magic" going on in the VM manager.)
All insights and hints will be appreciated .
Best regards, Gabriel
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden