How to respond to GCD memory pressure notifications?

I use GCD to receive notifications of pressure in memory.

The GCD documentation describes some of these constants:

DISPATCH_MEMORYPRESSURE_WARN

System memory status is at a warning stage. Programs should free up memory that they don’t need right now.

DISPATCH_MEMORYPRESSURE_CRITICAL

System memory status is at a critical stage. Programs should free up as much memory as possible.

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

Increased memory pressure is a system-wide condition that applications registered for this source must respond by changing their future memory usage, for example. by decreasing the cache size until the memory pressure returns to normal.

However, applications should not move and discard existing caches for past operations when the system memory system pressure is elevated, as this will most likely trigger VM operations, which will further aggravate system memory degradation.

It bothers me. So should I free up memory, or should I just stop allocating new memory?

+5
source share
1 answer

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

iOS has a virtual memory system, but no backup storage. When memory is running low, the system requests applications to reduce memory. If this does not free up enough memory, the system will start killing applications.

The instructions you quote in the libdispatch headers are for the MacOS virtual memory system, not for iOS.

In iOS, the application should discard objects and reduce the size of the cache when processing a memory alert. Particular attention should be paid to objects that use dirty (not cleaned) memory. This is memory that the system cannot automatically reuse on its own - it must first be discarded by the application. In a typical iOS application, images (images) use the dirtiest memory.

0
source

Source: https://habr.com/ru/post/1243948/


All Articles