Skip to content

Title heap hands back memory it frees: the SDK's heap fixes as patches/sdk (#75) - #110

Merged
cobanov merged 5 commits into
cobanov:mainfrom
Adon1s:pr/title-heap
Nov 25, 2026
Merged

cobanov merged 5 commits into
cobanov:mainfrom
Adon1s:pr/title-heap

Conversation

@Adon1s

@Adon1s Adon1s commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What happens

Over a run of photo requests the CPU heap grows and the pool's largest free block shrinks, and it does not come back
between requests. On upstream main (fa0fc59), 25 full-size photos took the largest block from 1,669 to 1,055 MiB
(−614) while the heap grew from 983 to 1,321 MiB (+338). MCsumz sees the same thing with long chats (#75).

Cause

The title heap is the payload SDK's: dlmalloc in eight arenas over direct memory. At the pinned SDK (b83202be) it
maps its segments back to back, and dlmalloc joins neighbours into one segment. A joined segment can only go back to
the pool whole, so one block left near its end keeps everything below it. Memory a request frees stays in the heap,
where the GPU cannot use it.

Change

As asked in #75, the SDK author's fixes are carried as patches instead of moving the pin:

  • patches/sdk/: four later commits of mihawk-99/PS5_PayloadSDK by its author, path-filtered to the heap and its
    host tests. 8278ca5e (calloc clears reused memory), 3752be7e (arena locks wait instead of spinning),
    9d048784 (2 MiB segments instead of 16), 55a2de04 (a 64 KiB gap so segments never join, ps5_heap_trim(), the
    heap's in-use figure). Each patch says which commit it comes from and what of it is left out. BASE names the pin
    they apply to.
  • scripts/build-app.sh: exports the pin with git archive, applies the patches with patch --fuzz=0, builds
    heap.o and mspace.o with the SDK's own Makefile into libps5lm_heap.a, and links that ahead of
    libps5platform.a. lld's --why-extract report is checked, so the build stops if the stock heap gets in. The
    pinned checkout and the shared SDK cache are never modified, so fetch_pinned's checks still hold.
  • scripts/test-native-build.sh: runs the SDK's own host tests (make -C platform test) on a patched export first
    (198 of 198 pass), and records the patches' sha256s in result.json.
  • ps5/app/heap_info.cpp (the only file that includes ps5platform/heap.h now): the app trims the heap 0.25 s after
    each llama-server request ends (from the activity hook) and every 10 s; a fleet worker every 10 s (not tested in
    worker mode). The trim does not wait for a gap between requests, because a run of photos never leaves one.
  • The stats thread no longer asks the heap once a second: with the patches that walks every block. It shows the main
    loop's figure, at most 10 s old.
  • The memory line is built as one string, so llama-server's prints can't land inside it, and ends with exact MiB:
    [MiB: largest 1316, heap 1100 in 63 segments, in use 939, walk 6.0 ms, requests 25, in flight 0]. Its start is
    unchanged for scripts/bench-ubatch.py.
  • Request files for measuring: memory each / memory off (a memory line after every request and after its trim),
    trim (trim now, between two memory lines), trim on / trim off. In scripts/ps5lm-app.sh and
    docs/CONSOLE.md.

When the pin moves to 55a2de04 or later, patches/sdk/ can be deleted and the rest still works.

Tested

On a PS5 Slim, stock title PPSA99575 built from each commit, Qwen 3.8 27B UD-IQ2_S with its F16 projector, 2k
context, --image-max-tokens 1280, screen on. The same 25 full-size photos, one at a time, with a memory line
after each (MiB):

Build Largest block when ready Largest block after 25 photos Lost Heap when ready Heap after 25 photos Errors Median per photo
main fa0fc59 1,669 1,055 −614 983 1,321 0 11.8 s
this PR 1,768 1,316 −452 898 1,100 0 11.8 s

So 261 MiB more of the pool is free in one piece after 25 photos, and the heap holds 221 MiB less. With the patches
the heap's in-use figure shows what is left: 854 MiB in use when ready, 939 after the run.

What this does not fix:

  • 204 of the 452 MiB still lost go at photo 15, the first photo with more image tokens than any before. The heap
    did not grow there (1,088 → 1,086): that is the vision encoder's GPU buffers growing, which the vision warm-up PR
    from Photo runs lose ~0.6 GiB in their first 25 images: the title heap and the vision warm-up (measured, with fixes) #75 is for.
  • The heap still grows, less: 898 → 1,100 MiB over the run, while what it has in use grows only 854 → 939. The free
    space it keeps sits in segments that still hold a block, and in each arena's last segment, which dlmalloc never
    gives back.

In my fork the same patches have run since 2026-11-11 on the console, through 100-photo runs, Q2_K_XL with vision
and long text loads, with no errors.

How to test

  1. Build as usual. build-app.sh prints build-app: title heap from patches/sdk (4 patches on SDK b83202be...),
    and app.log has heap: patches/sdk, empty segments go back to the pool after each request and every 10 s.
  2. scripts/ps5lm-app.sh memory each, then run requests: each one logs memory after a request and
    memory after the trim.
  3. scripts/ps5lm-app.sh trim off turns the trim off to compare; trim on turns it back on.

…trim after each request

The title heap is the payload SDK's (dlmalloc in eight arenas over direct
memory). At the pin (b83202be) it maps segments back to back, dlmalloc
joins them, and a joined segment can only go back whole, so one block left
near its end keeps everything below it.

patches/sdk holds four later commits of the SDK by its author,
path-filtered to the heap: 8278ca5e (calloc clears recycled memory),
3752be7e (arena locks block instead of spinning), 9d048784 (2 MiB
segments) and 55a2de04 (a 64 KiB gap so segments never join,
ps5_heap_trim, the heap's in-use figure). build-app.sh exports the pin,
applies them with patch --fuzz=0, builds heap.o and mspace.o with the
SDK's Makefile into libps5lm_heap.a, links it ahead of libps5platform.a
and checks --why-extract that the stock members stayed out. The pinned
checkout and the SDK cache stay as pinned. test-native-build.sh runs the
SDK's host tests on the patched copy and records the patches' sha256s in
result.json.

The app (heap_info.cpp, the only includer of ps5platform/heap.h):
- trims 0.25 s after each POST request ends (activity hook) and every
  10 s; a fleet worker every 10 s
- request files: trim (now, on, off), memory each / memory off
- the memory line is built as one string and ends with exact MiB: largest
  block, heap mapped and segments, in use and the walk's time, trim time,
  requests ended and in flight
- the stats thread no longer walks the heap once a second
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T06:13:55.578880Z b084150 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b084150fea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ps5/app/main.cpp Outdated
Comment on lines +1336 to +1339
} else if (!after_request && ps5lm::heap_can_trim()) {
ps5lm::heap_figures(); // the dashboard's heap figure (stats.cpp), at most 10 s old
}
last_trim = now;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh cached heap figures independently of request trims

With the default memory_each == false, if requests keep completing less than 10 seconds apart, each request-triggered trim skips heap_figures() but still resets last_trim. This prevents the periodic refresh from running, leaving the cached heap figures stale indefinitely rather than at most 10 seconds old. StatsCollector::run() now reads only heap_cheap(), and write_report() consumes its cached heap_gib, so diagnostic reports can retain the ready-time heap size throughout a busy run and hide subsequent growth. Use a separate snapshot-refresh deadline, or refresh the figures after request-triggered trims too.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed in 564b769: the heap read now has its own 10 s deadline, separate from the trim's, so requests ending under 10 s apart no longer leave the dashboard and report figure stale.

Requests ending under 10 s apart each reset the periodic trim's clock,
and the trim after a request did not read the heap, so the dashboard's
and the report's heap figure stayed at the last periodic read for as
long as the run kept going. The read now has its own 10 s deadline.
@MCsumz

MCsumz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

@Adon1s Thanks! I can only test it at the weekend, then I'll run my long-chat test on it: Qwen3.5-9B + mmproj, 64k, q8_0, Open WebUI up to ~48k tokens with an image each turn, plus a few full reprocesses. main vs main + #110, memory logged every turn. I'll post the numbers here.

BR Marcell

# Conflicts:
#	CHANGELOG.md

@cobanov cobanov left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Adon1s, this is a careful port. I checked the four patches against mihawk-99's commits. 0002–0004 match them line for line on the heap paths, and 0001 leaves out only the shm and vrange checks, as its message says. The licensing is fine: GPL-3.0-or-later SDK code in a GPL-3.0 repo, with the authorship kept. Exporting and patching instead of touching the pin, the --why-extract check and the #error guard on the larger stats struct are all the right calls, and CI shows the SDK's host tests passing on the patched copy. 564b769 fixes Codex's stale-figure point correctly.

I merged main into the branch (a CHANGELOG conflict only, both entries kept) and will merge once CI is green on it.

Two small things for a follow-up:

  1. patches/sdk/README.md: please name the licence and the author (GPL-3.0-or-later, Mihawk), as patches/mesa/README.md does. Please also say that c32d2be is left out on purpose: it's payload ForApp direct memory, the only other heap commit between the pin and 55a2de04, and a title doesn't need it.
  2. docs/BUILDING.md:110: add patches/sdk (the title heap) to the patches/ row of the layout table.

@MCsumz's long-chat run on main this weekend will cover the text-only side. A quick fleet-worker check before the next release would close the last gap.

# Conflicts:
#	CHANGELOG.md
@cobanov
cobanov merged commit 5e96235 into cobanov:main Nov 25, 2026
2 checks passed
MCsumz added a commit to MCsumz/PS5LM-memfix that referenced this pull request Nov 27, 2026
…stream

docs/UPSTREAM-TESTS.md: the 27 November runs, main 5e96235 on the PS5
Pro and main + cobanov#114 + cobanov#117 on the PS5 Fat. The README's fix table now
says where each fix is upstream: the heap (cobanov#110), the keep-alive fix
(with the llama.cpp move, cobanov#103), the Qwen3.5 cache (cobanov#88) and the media
marker (cobanov#112) are on main; the scratch fix is next.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MCsumz

MCsumz commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

@Adon1s @cobanov

Long-chat run on main with #110, as promised. scripts/ps5lm-memtest.py: ~1.5k tokens per turn up to 48.5k, the whole history resent every turn (as Open WebUI does), then 3 full reprocesses and their returns. memory each on; figures from the line after the trim.

PS5 Fat: Qwen3.5-9B Q4_K_M + Q8_0 mmproj, image in every request, 64k q8_0 PS5 Pro: Qwen 3.8 27B UD-Q2_K_XL, text only, 64k q4_0
Build main 5e96235 + #114 + #117 main 5e96235
Pool free: turn 1 → 48.5k → after the reprocesses 3.74 → 3.48 → 3.43 GiB 0.50 → 0.44 → 0.43 GiB
CPU heap in use: turn 1 → end 647 → 682 MiB 859 → 887 MiB
Heap mapped, segments at the end 757 MiB, 82 926 MiB, 79
Largest free block at the end 3,512 MiB 442 MiB
Full 48k reprocess 140 s 419 s

Every request was answered. The heap stays bounded on both: in use grows ~30 MiB over the whole chat and not at all during the reprocesses, and the Pro ran it with 0.43 GiB left. On the Fat, most of the 0.3 GiB the pool loses is outside the heap, where prealloc_x grows with the context (the third fix in #98). Not covered: the fleet-worker check.

BR Marcell

MCsumz added a commit to MCsumz/PS5LM-memfix that referenced this pull request Nov 27, 2026
With the title heap (cobanov#110) upstream main writes "memory after a request"
and "memory after the trim" once "memory each" is on, with the flexible
pool and "in use" in the line, so the script printed n/a there. It now
takes those lines as well (memfix's still work), waits for the trim and
the log write, and prints the pool's largest free block when the line
has it. The docstring says to turn the lines on after the model is
ready, since a load restarts the app.

Used for the cobanov#110 long-chat run on 27 November.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MCsumz

MCsumz commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

@cobanov fleet-worker check with #110, the last gap from your review. PS5 Fat as an RPC worker on main 5e96235 (+ #114, #117), PS5LM_WORKER_SCREEN=off; host llama-server on a Mac (CPU, --rpc, all layers of Qwen3.5-9B Q4_K_M on the worker, 64k q8_0). The same long chat as above: 43 turns to 49k, then 3 full reprocesses, with the worker's memory line after every request.

Turn 1 Turn 42 (48k) After 3 reprocesses
Pool free 6.06 GiB 5.86 GiB 5.65 GiB
CPU heap mapped / in use 52 / 40 MiB 68 / 56 MiB 78 / 56 MiB
Largest free block 6,208 MiB 6,000 MiB 5,784 MiB

Every request was answered; a full 49k reprocess took 176 s over RPC. The worker's heap stops growing by turn 10 (56 MiB in use from there to the end), and the pool stays level across the reprocesses after a one-time 0.2 GiB step at the first. Worker mode in, out (leave-worker) and back to the app all went over FTP, with no one at the console.

BR Marcell

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants