Skip to content

iOS 26: SD model memory not released after repeated ImageGenerationEngine dispose (iPhone 17 Pro) #2004

Closed

Description

@leehack

Resolved on main by #2018, merged as bde98bc9402ad8fb9878c0ef8f78539077cda1c0, adopting published stable-diffusion-native v0.3.0-6. Owner fixes move Apple ggml host buffers and model-loader scratch to Mach VM allocations.

The same full-VAE SD-Turbo harness completed four load/dispose cycles and eight decodes without load/decode failures on each of iPhone 16 Pro (iOS 18.3.2) and iPhone 17 Pro (iOS 26.5.1), session session-5511bf15-6626-4b14-b920-70836728c503, tested source 6b191b28. Stable per-device image hashes and saved artifact identity were independently reviewed for the merged tree. Nonzero residual memory remains; this is bounded single-session evidence, not a zero-leak claim. Details: doc/cross_platform_validation.md on main. Core/companion publication remains part of separate 0.19.11 release prep.

Original report

On an iPhone 17 Pro (iOS 26.5.1, DDP device iphone17pro-26-5), the second dispose of a full-VAE SD-Turbo ImageGenerationEngine kept about 2.0 GB resident (roughly the 1.88 GiB of weights). Every later load then failed the memory check: "needs about 2.86 GiB … only 1.24 GiB is available".

Load / dispose Footprint before After dispose
1st (split 2^26 arm) 141 MB 169 MB, freed
2nd (split 2^28 arm) 169 MB 2100 MB, not freed
next 8 loads ~2100 MB refused by the memory check
  • ggml_metal_free: deallocating is logged before both disposes, and nothing else in the native log differs between them.
  • The iPhone 16 Pro (iOS 18.3) in the same session returned to 136–142 MB after all five arms, so this is not harness-held memory and not the graph split.
  • Undecided: a real leak, or delayed reclaim by the GPU driver. The harness samples the footprint only at the moment of dispose. Next step: sample at +1 s, +5 s and +30 s after dispose, and retry the load after a delay.
  • Likely owner: the native/Metal layer after the context free (weights buffer on MTL0). Not the Dart engine.

Evidence: llamadart-evidence/issue-1047/0.19.10/results/ddp-c1/job-001 (DDP session-075e7004…). Found during #1047 device verification (plan #1995).

Activity

  1. leehack commented on Nov 14, 2026

    @leehack
    OwnerAuthor

    Triage: bug, priority:P2. Repeated load/dispose of an image model on iOS 26 can leave about 2 GB resident and block later loads. Unconfirmed whether this is a leak or delayed reclaim; needs a timed footprint probe. Area: image generation, iOS, Metal. Candidate for 0.19.10 if it confirms as a leak (regression rule); otherwise next patch.

  2. added
    bug
    priority:P2
    on Nov 14, 2026
  3. leehack commented on Nov 14, 2026

    @leehack
    OwnerAuthor

    Confirmed on DDP (session e2adad06, iPhone 17 Pro, iOS 26.5.1, v0.3.0-4 candidate). The memory is retained, not slowly reclaimed. On one engine, the footprint stays flat at about 2.1 GB over 30 decodes, so there is no growth per decode. 1st dispose: 177 MB at +1 s, 157 MB at +30 s, freed. 2nd dispose (after a reload): 2199 MB at +1 s and 2173 MB at +30 s, retained. A 3rd load then succeeds and the footprint stays about 2.19 GB, so the retained memory is reused, not leaked again. Practical effect: checkMemory sees about 1.2 GiB available after the 2nd dispose and refuses later loads (C1 run). The iPhone 16 Pro (iOS 18.3) frees on every dispose. Evidence: llamadart-evidence/issue-1047/0.19.10/results/ddp-c2b/job-001.

  4. leehack commented on Nov 14, 2026

    @leehack
    OwnerAuthor

    Root cause confirmed: ggml's iOS host allocator. It is not a Metal or driver leak.

    On every Apple platform except macOS, ggml backs Metal shared buffers and CPU buffers with posix_memalign/free. On iOS 26, libmalloc keeps large freed blocks in a cache that still counts toward phys_footprint. As a result, os_proc_available_memory() stays low and the next load fails checkMemory.

    Upstream fixed this in llama.cpp #31437 (merged 2026-11-02; it switches to vm_allocate on all Apple platforms). llamadart-native's pin cd959bae already has the fix. stable-diffusion-native's pinned ggml predates it.

    DDP probe (session cec41644, no model): 1.93 GB blocks, allocated, written and freed.

    Allocator iPhone 17 Pro, iOS 26.5.1 iPhone 16 Pro, iOS 18.3.2
    mmap freed immediately (2048 → 119 MiB) freed
    vm_allocate freed immediately (2044 → 115 MiB) freed
    posix_memalign + free retained 2038–2044 MiB at +5 s, on every cycle; available memory 1331 MiB instead of 3260 MiB freed (2005 → 76 MiB)

    Holding a ladder of further allocations dropped the iOS 26 footprint to 269 MiB. The retained memory is therefore reclaimable under pressure, not a hard leak, but checkMemory sees it as used.

    Fix (approved by the maintainer):

    1. Carry upstream ea24b952c in stable-diffusion-native as release v0.3.0-5, with a CI rule that iOS libraries must not import _posix_memalign.
    2. Repin llamadart.
    3. Verify with the iPhone: avoid GPU watchdog restarts in full-VAE Metal decodes #1047 reload soak on DDP.

    Workaround until the repin: keep one ImageGenerationEngine instead of disposing and reloading, or pass checkMemory: false on reload.

  5. leehack commented on Nov 14, 2026

    @leehack
    OwnerAuthor

    Device soak passed on runtime v0.3.0-5 (#2018 head f7dcae3, DDP session session-790c4db5-300b-4c86-aeb0-2e74585a34a2; SD-Turbo q8_0 + TAESD; 4 load/dispose cycles per device).

    Device Peak per load After dispose Baseline
    iPhone 17 Pro, iOS 26.5.1 ~2.4 GB 163–190 MB 148 MB
    iPhone 16 Pro, iOS 18.3.2 ~2.4 GB ~370 MB, flat across cycles 125 MB (first cycle)

    On iOS 26 the ~2 GB retention after dispose is gone. No load_failed, and decode hashes are stable on both devices. iOS 18 keeps a one-time ~245 MB after the first cycle that does not grow on later reloads, so it is not this bug. It is tracked on #2009.

  6. leehack commented on Nov 14, 2026

    @leehack
    OwnerAuthor

    Correction to the iOS 18 note above. On the same iPhone 16 Pro (iOS 18.3.2), the earlier runs with v0.3.0-3 and v0.3.0-4 came back to about 142–151 MB after dispose. With v0.3.0-5 it holds about 370 MB. The level is flat and nothing failed to load, but it may be a regression from the repin. The harnesses differed, so I am running a same-harness A/B (v0.3.0-4 against v0.3.0-5) on the iPhone 16 Pro before #2018 lands. This replaces the #2009 tracker item.

  7. leehack commented on Nov 14, 2026

    @leehack
    OwnerAuthor

    Same-harness A/B: v0.3.0-5 causes the iOS 18 increase. It is allocator cache, not a leak.

    Sessions: session-b80b7491 (v0.3.0-4, main eb423d0) and session-12a34aa5 (v0.3.0-5, #2018 head 80b55c9). Both use the same harness ab-ios18-1, 4 load/dispose cycles each.

    Device v0.3.0-4: after dispose minus first baseline v0.3.0-5
    iPhone 16 Pro, iOS 18.3.2 21–26 MiB 240–241 MiB, flat
    iPhone 17 Pro, iOS 26.5.1 about 1960–2010 MiB, plus one load_failed (this is #2004) 28–44 MiB

    Where the memory sits on iOS 18 with v0.3.0-5:

    • At dispose, MALLOC_LARGE holds about 203 MiB dirty.
    • Untagged vm_allocate regions are back to 8 MiB and Metal allocations to 33 MiB, so the ggml buffers themselves are freed.
    • malloc_zone_pressure_relief releases 0 bytes.
    • A page-aligned 256 MiB malloc/free drops the footprint to 129 MiB.

    So libmalloc keeps the model loader's freed read and convert buffers. With v0.3.0-4, ggml's large posix_memalign/free traffic flushed that cache as a side effect; after the vm_allocate change nothing does. The next load reuses the memory, but it counts against the memory limit: about 3020 MiB remains after dispose, against about 2929 MiB the load check needs.

    The fix belongs in stable-diffusion-native's model loader buffers. Evidence is in llamadart-evidence/issue-2004/results/ab-ios18/.

  8. leehack commented on Nov 15, 2026

    @leehack
    OwnerAuthor

    2026-11-15: the owner repair is now published in v0.3.0-6, owner 0ec6c1fa1e60d5124413b6c14b8fe510f112567d, still upstream be486498eadc29dc3cd234d944c2180ed42b1415. It combines v5's Apple ggml host-buffer allocation fix with owner#46's loader-scratch release, addressing the matched-harness iOS18 allocator-cache increase recorded above. This is no longer an unclassified graph-split regression.

    Consumer#2018 is draft at e3902ecc7384e4ee5cd827d07b8721340a4b974a: all46 check runs are completed success/skipped, including CI and pinned digests; fresh exact-head independent audit is still required. Publiccore/SDcompanion0.19.10, localeb423d09 and main501a0772 still pin-4, so the retention fix is artifact-available but not default-consumable.

    The PR records exact-binary UUID/normalized-byte verification against the published XCFramework SHA256 7e5a79218d731ce7fb6b54020190a79630bd843d9a1dbafb24e5f0442ab95cb8. Tested source6b191b28/testZIPa7af0af3d97606d2e93d0eacb84f4307d521ca6120a6ae50bbbb601f255881a8: four loads/disposals and eight SD-Turbo embedded-full-VAE512 decodes each on iPhone16Pro/iOS18.3.2 and17Pro/iOS26.5.1, actualMTL0, no failures. Residuals ~38–40MiB/~32–44MiB are nonzero single-session observations, NOT zero-leak proof.

    P2 unchanged. Next gate: audit current head/base, then separately approved merge/release through plan#2011. Sync12 bundle checksums/manifest/SwiftPM plus SDcompanion0.19.11/corefloor>=0.19.11; no new bindings or public ImageGenerationEngine API is needed because headers/capability floor are unchanged. Preserve older-companion warnings, failure/cancel/reuse and teardown limitations; no new device job by this scan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority:P2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions