Reference

NSPurgeableData

NSPurgeableData is a Foundation subclass of NSMutableData whose backing memory the system can automatically discard under memory pressure. You bracket access with beginContentAccess and endContentAccess so iOS knows when the bytes are safe to reclaim.

iOS developmentiOSiPadOS

NSPurgeableData

Also known as: purgeable data ios, nscache purgeable, ios nspurgeabledata

NSPurgeableData is a Foundation subclass of NSMutableData whose backing memory the system can automatically discard under memory pressure. You bracket access with beginContentAccess and endContentAccess so iOS knows when the bytes are safe to reclaim.

  • NSPurgeableData subclasses NSMutableData and adopts NSDiscardableContent so iOS can reclaim its bytes under memory pressure.
  • Wrap reads/writes in beginContentAccess()/endContentAccess(); a false return means the data was already purged and must be regenerated.
  • Best for recomputable caches (decoded images, network bodies) — never store irreplaceable data in it.

What NSPurgeableData does

NSPurgeableData is a concrete subclass of NSMutableData that adopts the NSDiscardableContent protocol. Its contents live in *purgeable memory*: pages the kernel is free to reclaim when the device runs low on RAM, without paging them to disk or crashing your app. This makes it ideal for recomputable or re-downloadable data such as decoded image bitmaps and network response bodies.

Access is reference-counted through content access. Call beginContentAccess(), which returns a Bool, before reading or writing the bytes. While at least one content-access call is outstanding, the system will not purge the memory. When you are done, call endContentAccess(); once the counter hits zero the data becomes eligible for reclamation. A new NSPurgeableData starts with an access count of one, so it is initially safe to use.

Using it safely

If beginContentAccess() returns `false`, the content has already been purged and the bytes are gone — you must regenerate or re-fetch them. Treat purgeable data as a *cache*, never as the source of truth. NSCache uses the same discardable-content machinery and is usually the higher-level tool for managing many such objects.

Always balance every successful beginContentAccess() with endContentAccess() and keep the access window as short as the work that touches the bytes. Holding access open defeats the purpose, because the system cannot reclaim memory it cannot prove is idle. Use discardContentIfPossible() to hint that a no-longer-needed buffer can be dropped immediately.

Related terms

Keep reading the reference.

Act on it

Guides and tools for this topic.