Fundamentals 12 min read

Inside iOS Memory Management: Stack, Heap, Tagged Pointers, and SideTables Explained

This article breaks down iOS's memory layout—including stack, heap, static, data, and text sections—and examines three key management techniques: Tagged Pointers, non‑pointer isa, and SideTables, detailing their implementation, benefits, and underlying data structures.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Inside iOS Memory Management: Stack, Heap, Tagged Pointers, and SideTables Explained

iOS devices achieve their popularity partly thanks to an efficient memory‑management system; this article examines the memory layout used by iOS and the three main management schemes that make it work.

iOS Memory Layout

Stack (stack) : a linear, contiguous region managed by the system for each thread, storing execution records without needing pointers.

Heap (heap) : a linked, non‑contiguous region used for dynamic allocation; Objective‑C objects reside here and are accessed via pointers, managed by ARC or MRC.

Static (bss) : holds initialized global variables and constants.

Data (constant) : contains uninitialized globals that are zero‑filled at runtime for performance reasons.

Text (code) : a read‑only region that stores the executable code.

Memory‑Management Scheme 1 – Tagged Pointer

When moving a 32‑bit app to 64‑bit, objects such as NSNumber or NSDate would double in size because pointers grow from 4 bytes to 8 bytes. Tagged Pointers solve this by embedding the actual value directly in the pointer when the value fits within 8 bytes, eliminating the need for a heap allocation and reference‑count bookkeeping.

Benefits include up to three‑fold faster reads and a 106× speedup in object creation, while reducing memory consumption on 64‑bit devices.

Key characteristics:

Used for small objects like NSNumber, NSDate, and NSString under 60 bytes.

The pointer value is the actual data, not an address, so there is no isa pointer and no heap allocation.

Provides roughly three times faster memory reads.

Memory‑Management Scheme 2 – Non‑Pointer isa

On 64‑bit systems only 32 bits are needed for an address, leaving the remaining 32 bits for other metadata. The non‑pointer isa technique stores additional information (such as flags) in the high bits of the isa field.

Conditions for using non‑pointer isa include:

Contains Swift code.

SDK version lower than 10.11.

Runtime detects an __objc_rawisa segment in the image.

Developer sets OBJC_DISABLE_NONPOINTER_ISA=YES in the environment.

Certain classes (e.g., GCD) that cannot use this scheme.

Superclass disables it.

Memory‑Management Scheme 3 – SideTables, RefcountMap, weak_table_t

Apple uses a global SideTables hash table to store reference‑count and weak‑pointer information for every object. The object’s address serves as the key, and a SideTable structure holds the lock, strong‑reference map, and weak‑pointer table.

Because reference‑count updates must be atomic, Apple employs a spinlock ( spinlock_t slock) for short‑duration locking, which is more efficient than mutexes for this use case.

struct SideTable {
    spinlock_t slock;          // lock
    RefcountMap refcnts;       // strong‑reference map
    weak_table_t weak_table;  // weak‑pointer table
    // ...
};

The RefcountMap is a C++‑style map that, together with the initial hash table, allows precise lookup of an object’s reference count among billions of objects.

Reference‑count bits (using 1UL << n) encode flags such as: WEAKLY_REFERENCED: object has weak references. DEALLOCATING: object is currently being deallocated. SIDE_TABLE_RC_PINNED: reference count has reached its maximum value.

The weak_table_t consists of an array of weak_entry_t structures. Each entry stores the address of the weakly‑referenced object, an array of pointers to all weak references, and an inline array for up to four weak pointers to avoid extra allocations.

Conclusion

iOS combines several sophisticated techniques—Tagged Pointers, non‑pointer isa, and SideTables—to minimize memory usage and maximize performance, especially on 64‑bit devices. Understanding these mechanisms is essential for low‑level iOS development and performance tuning.

References:

https://www.jianshu.com/p/dcbf48a733f9

http://www.cocoachina.com/articles/13449

http://www.cocoachina.com/articles/24119

Objective‑C Advanced Programming: iOS and OS X Multithreading and Memory Management

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

iOSMemory ManagementRuntimeObjective‑CTagged PointerSideTables
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.