DEV Community

Cover image for My Memory Reservation Queue for Claude Code Sessions Kept Jamming. Mostly, the Ledger Didn't Match Reality
maruhana
maruhana

Posted on Originally published at zenn.dev AI-assisted

My Memory Reservation Queue for Claude Code Sessions Kept Jamming. Mostly, the Ledger Didn't Match Reality

Several Claude Code sessions on one 32 GB Windows PC reserve memory before starting heavy jobs. Over 11 days there were 68 reservations and at least 18 waits. Most jams came from the ledger drifting away from what was actually running.

This is an English adaptation of an article I originally published in Japanese on Zenn: メモリーの予約が詰まった場面を並べたら、ほとんどは台帳と実際のずれだった. The measurements and conclusions are mine; I used an AI tool to help translate and restructure the text.

Short version: before a heavy job starts, the session books memory in a ledger ("I'll use this much"). In 11 days there were 68 reservations, and at least 18 had to wait in a queue. Most jams weren't real shortages. The ledger and reality had drifted apart in four ways: a reservation held while waiting on something else, a job started in another window that the ledger couldn't see, a script that kept running after it was "stopped", and an amount the ledger never counted (commit charge). I fixed the counting or the rules each time. Even with an accurate ledger, big jobs didn't get through first-come-first-served, so I now let them through one at a time.

A 1-minute video summary (narration is synthetic, English captions available):

The setup

I run Claude Code sessions per project on one Windows PC with 32 GB of RAM. Different sessions start heavy jobs: ComfyUI image generation, local LLM benchmarks, an embedding server. The integrated GPU uses the same 32 GB.

The reservation script that heavy jobs must go through is Guard 3 in my post on stopping known mistakes, and switching the stop condition from free memory to a pinned SSD is in my post on what actually hurts. Near the end of that second post, I wrote a few lines saying reservations collided every day. This post is what was behind those lines.

The reservation script (heavy_lease.py) uses the same formula as before:

available for a new job = free (measured) − Σ(other reservation − what that reservation's processes actually use) − 3 GB spare
Enter fullscreen mode Exit fullscreen mode

"Free" is available physical memory. "Actually use" is the summed working set of the reservation's process tree. Commit, which comes up later, is Private Bytes per process and the commit charge for the whole PC.

Other reservations' "not yet used" part is subtracted up front because jobs like ComfyUI look small until the model loads. If there isn't enough, the job joins a queue and gets its reservation first-come-first-served.

11 days of records

I counted the lines the reservation script actually printed, from the conversation logs and the output of background runs.

Item Count
Reservations (distinct reservation IDs) 73 (5 were script tests → 68 real)
of which solo starts (--solo, no other reservation or waiter) 12
Joined the queue (distinct wait IDs) 18 or more
Refused for lack of memory 19 lines
Solo start refused (another reservation or waiter existed) 4 lines
Released by the job itself when done 57

By size: 18 reservations of 6 GB or less, 16 over 6 and up to 12 GB, 31 over 12 and up to 20 GB, and 3 over 20 GB. The busiest day had 16, with 7 heavy jobs overlapping.

Released (57) is lower than reservations because the rest were cleared automatically when the process ended or the reservation expired. The 18 waits count from day 5, when I added the queue ledger. Waits whose ID doesn't appear anywhere in the conversation logs, background output, or job logs aren't counted.

For those 18 waits, the conversation logs give start and end times.

How the wait ended Count How long
Got the reservation 8 2, 13, 17, 21, 22, 28, 63, 120 min
Left the queue before getting it 10 6 under 3 min; the others 14, 56, 72, 96 min

The longest, 120 minutes, was waiting for an evaluation that took hours. More waits left the queue than got through. The reasons: the other reservations and waiters were gone so the job switched to a solo start (4), the order was rearranged or the job gave way (3), the PC was about to be shut down (2), and the job was changed and re-requested (1).

Drift 1: holding a reservation while waiting on something else

On the morning with the most overlapping heavy jobs, the queue stopped moving.

An evaluation session that uses embeddings had reserved 9 GB. But it was waiting for a reply about borrowing the GPU on my other PC, and was actually using 0.3 GB. The formula kept subtracting the 8.7 GB "not yet used". Behind it, image generation (20 GB) and an LLM benchmark (24 GB) were both stuck. A solo start, which may begin even when memory is short, isn't allowed while any other reservation exists.

From the ledger's side, the reservation was correct. It said "I will use 9 GB" and simply hadn't grown yet. The problem was that nobody knew when it would grow.

I fixed it in the rules, not the script: take a reservation only when you can start right away. While the start time depends on someone else's reply, release it and take it again once the time is set. That morning, the evaluation session released, then took 9 GB again once the GPU time was settled.

Drift 2: a job started in another window was invisible to the ledger

A reservation is tied to the calling session's process and its descendants. "Actually use" is the memory of that process tree.

When ComfyUI is started in a separate window (with Windows Start-Process, for example), it may not land in the reserving session's process tree. The ledger then shows a 17 GB reservation using 0, as if it will still grow by 17 GB, while ComfyUI's real 13 to 17 GB is already eating free memory. The same memory was subtracted twice, and other sessions waited longer than they had to.

The script had an attach command to re-point the reservation at the started process, but people forget it in a hurry. So I made it count without attaching: any process of 1 GB or more that isn't covered by a reservation, and whose command line contains the reserving session's working folder, is counted toward that reservation.

def auto_attached(leases, table):
    # processes of 1 GB or more that aren't in any reservation's tree
    cands = [p for p in table if p not in covered and ws(p) >= G]
    for p in cands:
        c = cmdline(p).lower()
        for l in leases:
            if any(k in c for k in keys(l)):   # reservation's working folder, or the session-name folder (with \ separators)
                hit.setdefault(l["id"], set()).update(subtree(p, kids))
                break                          # count it only for the first matching reservation
Enter fullscreen mode Exit fullscreen mode

Three days later it missed once. The image generation session reserved from its working folder, but ComfyUI itself ran on Python in a different folder, so the command line didn't contain the working folder. Now it also looks for the folder named after the session.

Leftover forgotten attaches show up in status: if a reservation is still using less than 20% of its size 10 minutes after it was taken, a line suggests attaching.

Drift 3: a script that was "stopped" kept running

The run where free memory fell to 5 MB is in the SSD post. There was one more drift after it.

In that run, Claude Code stopped the session's background jobs because memory was short. The session's screen said the background script and the watcher had stopped. The image generation session saw that, stopped ComfyUI, and released its reservation.

But only the outer background job had stopped. The script inside it, which restarts ComfyUI every 2 or 3 images, was still alive. Right after the release, it started ComfyUI again with no reservation. The ledger showed 0 reservations while nearly 19 GB was in use.

I suspected another session had cut in, went back through the records, and found the surviving script instead. What I changed was how I check: between "stopped" and "released", confirm the processes are actually gone. status lists large processes without a reservation, with their command lines, so looking at it after a release catches this.

Drift 4: the ledger counted free memory, not commit

In the SSD post, I changed the stop condition for running jobs from free memory to a pinned SSD, because free memory was only a stand-in for what actually hurts. But the reservation formula, which decides whether a job may start, still counted free memory. The other thing that hurts, the limit on how much memory jobs can commit (the "two things that actually hurt" section of that post), isn't in the formula. The script only shows commit in status.

On this PC the commit limit is normally 47.5 GB (31.5 GB of RAM plus a 16 GB page file). It isn't a fixed wall. The page file is set to "initial 16 GB, maximum 32 GB", and Windows grows it when needed, up to 63.5 GB. It did reach 63.5 GB during the forced power-off that happened before I added reservations. I don't know at what point it grows. Microsoft's documentation says a system-managed page file grows when commit reaches 90% of the limit, but doesn't cover a page file with a manually set initial and maximum like this one. Here, it hadn't grown even when commit reached 46.1 GB (97%).

The risk is while it grows. Windows logs an event saying some applications' memory requests may be denied (my translation of the Japanese event text), and a job whose request is denied can crash. During the 11 days, all 201 status outputs I still have showed the limit at 47.5 GB.

Some commit sits outside every reservation. Even with almost no heavy jobs running, the OS and resident apps (the swarm of Claude Code sessions, the browser, and so on) had 17 to 21 GB committed. None of that belongs to any reservation.

Free memory and commit headroom move separately

Moment Free Commit headroom (to 47.5 GB)
A: midday on the day with 7 overlapping heavy jobs 16.4 GB 15.4 GB
B: the Drift 3 day, while ComfyUI ran with no reservation 3.0 GB 7.8 GB

At A, the embedding evaluation (reserved 9 GB, using 2.4 GB) was running, and ComfyUI's 20 GB was waiting behind it. Whether both could run at once depends on which number you look at.

  • By free memory: the formula gave 6.8 GB available, short of 20 GB. But a free-memory shortfall can be covered by paging. It gets slower; it doesn't become impossible.
  • By commit: the PC already had 32 GB committed, and adding ComfyUI (18 to 20 GB) goes past 50 GB. Paging covers missing free memory, not missing commit. Past 47.5 GB, Windows would have to grow the page file, and jobs could crash while it does.

So they didn't run together; they ran in turn.

B was the opposite: only 3.0 GB free, but still 7.8 GB of commit headroom. Looking only at free memory misreads both moments.

Why commit isn't in the formula

The formula still doesn't include commit. I hadn't decided against it; I just hadn't done it. So I recomputed, from 194 status outputs over the 11 days that kept the formula's value, whether including it would have changed any answer.

What I recomputed Result
Commit headroom (to 47.5 GB) was smaller than free memory 4 times
Commit headroom minus other reservations' unused part fell below the current formula's value (using up to 47.5 GB) 0 times
Same, using only up to 46 GB 3 times (by 0.3 to 1.1 GB)

The current formula subtracts a 3 GB spare, so against 47.5 GB it always refused on free memory before commit would have. Of the 12 solo starts, which skip the formula, 5 have the commit at start time in the records; commit plus the reservation size came to 40.5 to 45.0 GB. Treating the reservation size as its commit, within the records I have, adding commit to the formula wouldn't have changed whether a job could start.

And if I add it, I can't just add the reservation size. A reservation declares how much memory the job will use, and commit differed a lot from use depending on the job. These are single runs on this PC:

Job Peak use Peak commit
ComfyUI (SDXL family, 2 images) 17.6 GB 18.1 GB
ComfyUI (Qwen-Image family GGUF Q4_K_M, 2 images) 19.8 GB 19.7 GB
llama.cpp, 14B (Q4_K_M) fully on the integrated GPU 10.2 GB 10.3 GB
llama.cpp, same model on CPU 15.6 GB 7.8 GB
Audio check (4B judge model on the integrated GPU, transcription on CPU) 12.9 GB 13.1 GB
Embedding server (1B, CPU, about 4,000-character texts, 16 at a time) 7.9 GB 10.7 GB

Jobs on the integrated GPU had nearly the same peak commit and peak use (the Qwen-Image run had a stretch during loading where use was 19.8 GB and commit 16.5 GB). In ComfyUI, the integrated GPU's shared memory rose to 13.3 GB (SDXL) and 15.5 GB (Qwen-Image), but the PC's total commit minus ComfyUI's barely moved, so I take it that the GPU part is included in the process's commit. CPU jobs ranged from half of use to nearly 1.4 times. CPU llama.cpp committed less because it opens the model file by mapping it into memory (mmap), and that part isn't committed. With mmap off, it used 10.1 GB and committed 10.2 GB, nearly the same. The embedding server stayed at about 10 GB committed while its use dropped from 7.9 GB into the 4 GB range. So some memory was committed but unused; I haven't checked what it was. To put commit in the formula, a reservation would need a separate field for expected commit.

Also, commit got close to the limit not when jobs started, but while they ran: 45.2 GB in the middle of a run of images, and 46.1 GB in the 5 MB run from the SSD post. Image generation has its own watcher that stops generating when commit passes 46 GB, and what stopped that run was this watcher and Claude Code, not the limit itself. For now: while running, that watcher; before starting, look at the commit line in status and run things in turn.

The night I wanted to slip in an urgent check

Another night I wanted to slip a short, urgent check (an audio check, reserving 12 GB) into an evaluation expected to take hours (reserving 9 GB). That night's status showed 10.3 GB free and 36.4 GB committed. Paging looked like it could cover the free-memory gap, but adding 12 GB to commit gave 48.4 GB, past 47.5 GB. I arranged for the evaluation to stop its embedding server at a break and release, let the check through, then reserve again. In the end the check got delayed, the evaluation stopped for the day, and the check got a normal reservation afterward.

Queue jams: big jobs don't get through first-come-first-served

Some jams happened even with an accurate ledger.

On the Drift 1 morning, after the evaluation session released, 13.7 GB was available. The queue held 20 GB and 24 GB, both too big. It's first-come-first-served, so nothing behind the 20 GB job moves until it goes. The only way forward is more free memory, but nothing else large was running, so free memory wasn't going to grow.

I took both out of the queue and let them through one at a time with solo starts (--solo). A solo start is allowed only when no other reservation or waiter exists, and may begin even when memory is short; in exchange, it watches for a pinned SSD. The SSD watcher doesn't warn about a commit shortfall, so the commit line in status gets a look before starting too. When the first job finished and released, it told the next session, which started solo the same way. Shorter jobs went first. That day, 7 heavy jobs went through one by one.

What I took away

  • A reservation ledger only works while it matches how memory is actually used. Most jams came from ledger numbers drifting from reality: holding while waiting, starting where the ledger can't see, "stopped" but still running, and an amount the ledger doesn't track.
  • Reserve only when you can start right away. A reservation held while your start time depends on someone else blocks everyone behind you.
  • Design the counting for people who forget. An attach step existed, and it got forgotten in a hurry. Inferring the owner from the command line counts it anyway.
  • Between "stopped" and "released", check the processes are gone. The outside can stop while the inside keeps running.
  • Look at use and commit separately. Depending on the job, commit was half of use or nearly 1.4 times. Commit exists outside reservations too. Paging covers missing free memory, not missing commit, so the shorter free memory is when you start, the more the commit headroom matters.
  • Big jobs competing with each other don't resolve first-come-first-served. Let them through one at a time, and tell the next one when you release.

What I didn't measure

  • The exact number of waits. Waits whose ID wasn't left anywhere can't be counted, and the first 4 days had no wait IDs at all.
  • What owner inference from the command line misses. 10 reservations in the 11 days were counted without an attach, as far as the records go. After adding the session-name folder, the status output I have shows no case where it missed. I can't say anything about times when I didn't run status. One other drift did turn up: a reservation expired automatically after its estimated time while ComfyUI was still running. The "large processes without a reservation" line in status caught it, and the reservation was taken again. The script still clears reservations on expiry even if the job is running.
  • Whether job objects would prevent this. Windows has job objects for handling processes as a group. Per Microsoft's documentation, child processes created with CreateProcess by a process in a job are in the same job by default, and the whole job can be terminated together. That might prevent Drifts 2 and 3. I haven't tried it.
  • How much commit varies per job. The table above is one run each and will move with settings and inputs.
  • What happens at the moment the page file grows. I haven't pushed commit past 47.5 GB to see, because other jobs' requests might be denied too.
  • Other PCs. This is built for this PC, where the integrated GPU uses the same 32 GB. I haven't applied it to my other PC, where sessions share GPU-side memory instead.

Summary

I've used a memory reservation script for heavy jobs for 11 days: 68 reservations, at least 18 waits. Most jams came from the ledger drifting from reality. Reservations held while waiting are now released and retaken, jobs started in another window are counted by inferring the owner from the command line, and after a stop I check that the processes are gone. Separately from the free memory the ledger counts, I watch commit and run combinations that would approach the limit in turn. Big jobs that don't fit even with an accurate ledger now go through one at a time instead of first-come-first-served.

The original of this article: メモリーの予約が詰まった場面を並べたら、ほとんどは台帳と実際のずれだった.

Top comments (0)