Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond to memory pressure notifications from GCD?

I am using GCD to get memory pressure notifications.

GCD documentation describes some constants like so:

DISPATCH_MEMORYPRESSURE_WARN

The system memory pressure condition is at the warning stage. Apps should release memory that they do not need right now.

DISPATCH_MEMORYPRESSURE_CRITICAL

The system memory pressure condition is at the critical stage. Apps should release as much memory as possible.

Seems logical that I should free unused memory. However, in other places (man pages and source code) I find this note related to these constants:

Elevated memory pressure is a system-wide condition that applications registered for this source should react to by changing their future memory use behavior, e.g. by reducing cache sizes of newly initiated operations until memory pressure returns back to normal.

However, applications should NOT traverse and discard existing caches for past operations when the system system tem memory pressure enters an elevated state, as that is likely to trigger VM operations that will further further ther aggravate system memory pressure.

This confuses me. So should I free memory, or should I just stop allocating new memory?

like image 871
Beirs Avatar asked Feb 26 '16 14:02

Beirs


1 Answers

MacOS has a virtual memory (VM) system that uses a backing store: the file system. The file system is used to hold memory that is not currently in use. When the system is running low on real memory (RAM), things in memory that are not actively being used can be written to disk and loaded back into RAM later.

iOS has a virtual memory system but not a backing store. When memory runs low the system asks apps to lower their memory footprint. If that does not free up enough memory the system will start killing apps.

The guidance you are quoting from the libdispatch headers is referring to the MacOS virtual memory system, not iOS.

On iOS an application should discard objects and reduce cache sizes when handling a memory warning. Particular attention should be paid to objects that are using dirty (non-purgeable) memory. This is memory the system can not automatically reuse on its own - it must be discarded by the application first. In a typical iOS application images (pictures) use the most dirty memory.

like image 75
quellish Avatar answered Oct 21 '22 13:10

quellish