The guard said yes, the kernel said no: raggio's memory in a 4 GiB box

Table of Contents
Part 2 was about raggio taking two minutes to say hello. This part is about what happened when it tried to build an IVF index inside a 4 GiB container: it died, right after a check had said it wouldn’t.
The guard said yes, then the kernel said no
An IVF build needs memory on top of what the collection already uses, so raggio checks first: if the container doesn’t have the headroom, it refuses the job instead of dying halfway through. On the DGX, with swap off, the guard said yes, and the build was killed for running out of memory anyway. Two out of two.
The guard was wrong three times over. It asked for 1,884 MiB, and the build grew by about 3 GiB. It ignored swap, and on that host the container had 4 GiB of it, which hid the problem: with swap on, the build survived by pushing 634 MiB to disk. And it ran after the slow pre-work from part 2, so even a correct refusal came after minutes of reading.
glibc kept the heap
Where did 3 GiB come from? When Python frees the build’s temporary arrays, the memory goes back to glibc’s allocator, not to the operating system. glibc keeps freed blocks in its heap for the next allocation, and the container’s memory cap counts them as used. After a build, raggio sat at 2.72 GB, against 1.51 GB for the same index loaded fresh from disk.
By default, glibc adapts. When a big block is freed, it raises the size above which new blocks get their own mapping from the kernel, up to 32 MiB, so later big blocks come from its heap, and when they’re freed they stay there. Setting MALLOC_TRIM_THRESHOLD_ in the environment (here to 128 MiB) has a side effect, and the side effect is the point: glibc stops adapting. A block of 128 KiB or more that the heap’s free space can’t serve gets its own mapping, and goes straight back to the kernel when it’s freed.
Resident memory of the server through a build, a drop and a rebuild, both in a 4 GiB container. The default arm is the probe’s run with swap allowed, which is how the baseline ran; with swap off, the second step is where the kernel kills it. GiB from the probe and from plan C’s acceptance run.
Add a malloc_trim(0) after every index job to hand back whatever is left in the heap, move the guard before any SQL, size it at 1.25 times the measured growth, and the acceptance test with swap off (build, drop, build again) peaked at 3,527 MiB under a 4 GiB cap. No kill. Dropping an index used to need a temporary 8 GiB container, and now it runs in the normal 4 GiB one.
The fix that slowed search by a third
Then the loop did its job. Before the next plan started, it measured main again, and filtered search on the IVF index had gone from 35.7 ms to 49.4. Only in some containers, though: the ones that had just built the index answered in 37 ms, like before. The ones that had restarted and loaded the index from disk answered in 49. The cold-start plan’s acceptance run measured memory, not search, so no gate had caught it. The re-baseline did.
The cause was that side effect. A filtered search on the IVF index builds, for each of the 16 shards it probes, three temporary arrays as long as the whole filter: about 2 MB each for a filter of 255,000 ids. That’s over 128 KiB, so with glibc no longer adapting, each array the heap couldn’t serve got a fresh mapping from the kernel, with a page fault on every new page and an unmap on free, up to 48 times per query. A container that had just built the index still had a big free heap from the build, and glibc served the arrays from there. A restarted one didn’t.
An A/B with three arms confirmed it: 48.7 ms with the setting, 36.3 without it, and 36.2 with the setting plus a fixed 32 MiB mapping threshold.
A threshold between two walls
Easy, set the threshold and move on? With 32 MiB, the swapless index build was OOM-killed again, 147 seconds in, which is exactly what the setting was there to prevent. The threshold had to sit above the search’s 2 MB arrays and below the build’s big blocks, so the agents swept it:
| Mapping threshold | Filtered search | Swapless build |
|---|---|---|
| 1 MiB | won back 8 of 13 ms | passed |
| 4 MiB | won it all back | peaked at 3,940 MiB, cap 3,891 |
| 16 MiB | won it all back | killed |
| 32 MiB | won it all back | killed |
No room in between. Nothing shipped, and the decision record says so.
glibc serves a block from its heap when it can, and maps a fresh region from the kernel when the block is at least the mapping threshold; mapped blocks go back on free, heap blocks stay. Move the threshold through the values the agents tried. Both bars are measured: filtered IVF search in restarted containers, and the peak of a swapless build, drop and rebuild.
The real fix came three plans later, from the IVF work in part 6: the filter intersection was rewritten to work shard by shard, so the arrays became shard-sized, about 64 KB, under the 128 KiB threshold. Filtered IVF search went from 37.2 ms on the original code to 5.9 on the final one. I’ll take it.
Part 3 in numbers
From the final A/B, the original code against the final one, 2.55 million vectors in a 4 GiB container:
| Metric | Before | After |
|---|---|---|
| IVF index build | 391 s | 178 s |
| Swap used during IVF builds | up to 2,902 MiB | 58 MiB |
| Dropping the IVF index | 295 s, in an 8 GiB box | 102 s, at 4 GiB |
| Filtered IVF search, median | 37.2 ms | 5.9 ms |
Next Monday
Part 4, November 2: ingest. A 2.7 MB row rewritten to change one word, JSON on the event loop, the sync lever I finally pulled, and the gate that failed while every run won.