Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Memory and limits

What is resident

StructureGrows withNotes
Trigram posting listsdistinct trigrams × filesRoaring bitmaps; run-optimised after a build; after a load read in place from the mapped index file (page cache, not heap), except lists indexed since
Symbol tablesdefinitionsper file, restored from the saved index; plus a dictionary of distinct names, built after a build and saved with the index
Reference tablescall sites and type mentionsnames interned once; positions packed
Dependency graphresolved importsbidirectional, with cached dependent counts
File storefilesone small entry per file; content is not kept
Mapped contentfiles > 1 MiB read by searchesthe “mapped” figure in the UI; released when a file is removed

Small files are read into owned buffers per access and dropped afterwards, so search traffic does not accumulate memory. The latest results give the CI corpus’s resident memory after a build.

Where it goes

/api/diagnostics reports the index’s estimated heap per structure under index.memory, largest first, next to the process’s resident memory (index.resident_bytes, Linux only). corpus_bench prints the same breakdown after a build, and the scale scoreboard records it per scenario.

PartWhat it holds
postingstrigram posting lists (Roaring bitmaps) and their table
referencessymbol references and their interned names
symbolssymbol definitions and the name dictionary
importsimports as written, and those still waiting for a file to resolve to
graph_pathspath tables the import resolver looks paths up in
graph_edgesdependency edges and dependent counts
file_tableindexed paths and the path lookup
file_metadataranking metadata and display paths
dir_indexfiles by folder, for the watcher

The figures are estimates from lengths and capacities, not allocator measurements (src/heap_size.rs). tests/heap_estimates.rs keeps them honest: it counts every allocation and fails if the estimates for a loaded index fall outside 75% to 105% of the heap it really holds. Checked the same way on public code (tokio + Django at b1f9287, the Linux kernel re-measured after symbols were packed, C1.6). A loaded index reads its posting lists in place from the mapped index file, so they are not heap; the mapped bytes are reported apart (mapped_postings_bytes) and are page cache the system can evict and share:

CorpusIndexLive heapEstimatedPosting lists mapped
tokio + Django (6,200 files)just built75.2 MB46.4 MB (62%)none
tokio + Djangoloaded from disk19.4 MB18.7 MB (96%)12.6 MB
Linux kernel (85,700 files, 1.23 GB)just built422.6 MB379.4 MB (90%)none
Linux kernelloaded from disk161.9 MB157.2 MB (97%)149.3 MB

The Linux kernel, loaded from disk:

PartEstimated
symbols113.9 MB
graph_paths14.0 MB
file_table12.7 MB
file_metadata7.8 MB
postings (on the heap: the table and lists indexed since the load)4.0 MB
references3.2 MB
everything else1.7 MB

Each symbol name is held once. A symbol is 16 bytes (its name’s number, line, column, kind and whether it defines the name), and the distinct names sit back to back in one string with a hash table over them. Before, a symbol was 48 bytes plus its own copy of its name: the Linux kernel’s loaded symbols took 239.0 MB, and on the 302,000-file corpus symbols took 405 MB and resident memory after a build was 1,490 MB, now 212 MB and 1,152 MB (same machine, 3 rounds). Names no symbol uses any more (after edits) are kept until they outnumber the used ones, then the table is rebuilt without them.

Each path is held once. The file store interns every file’s canonical path; the ranking metadata (its root-qualified path is an offset into that path), the import resolver’s path tables and the import lists all share that one allocation, so file_table counts the paths and the others count only their own tables. On the 302,000-file real corpus this took resident memory after a build from 1,665 MB to 1,504 MB (same machine, 3 rounds). The lowercase file-name stem is the exception: it is kept inside the metadata (up to 23 bytes; 85% of stems fit), because every candidate file of a search reads it and reading it through the path cost a cache miss per file.

Known limitations: on Windows the metadata keeps its own copy of the root-qualified path, since it is spelled with / and the stored path with \. A search with a path filter reads every candidate’s path from the file store: on an index just built (paths spread through the heap among the build’s other allocations) that measured 19% slower on 302,000 files (95 to 113 ms); on an index loaded from disk, where the paths are allocated in order, it is as fast as before.

A just-built index holds more than the same index loaded. On tokio + Django it is 75 MB against 19 MB. Most of the gap is the posting lists, which a loaded index reads from the mapped file. The rest is spare capacity: posting lists built one insert at a time keep it (a list in one container costs 168 bytes instead of 42), and the estimates cannot see it, which is why they cover only 62% there. A restart gives the memory back.

Resident memory is higher again. A build allocates and frees far more than the index it leaves behind (file contents, per-batch trigram sets, merge buffers), and the allocator keeps some of the freed pages.

The allocator. The server, fcs and the benchmarks use mimalloc (its v2 line) instead of the system allocator, on every platform. Queries allocate a lot (result lists, decoded posting lists, file contents), and with glibc’s allocator a server that had just built its index answered about 2x slower than the same server after a restart: on the 302,000-file corpus a common word took 161 ms in the building process and 77 ms in a fresh one, for the same instructions. It was the allocator’s state after the build, not the index’s layout: a copy of the index loaded into the building process was just as slow. With mimalloc, same machine, base a708ca2:

CorpusCommon wordIdentifierRegex with literalBuildResident after build
tokio + Django (5 rounds)47.4 → 38.2 ms15.2 → 10.7 ms40.1 → 36.6 ms5.30 → 4.79 s100 → 117 MB
302,000 files (1 round)181 → 87 ms78 → 44 ms110 → 75 ms189 → 176 s1,534 → 1,487 MB

jemalloc was as fast but does not build for Windows; mimalloc’s v3 line held 50% more memory on tokio + Django. Once a build or a load is done the server has the allocator hand unused memory back (mimalloc’s mi_collect, and malloc_trim for glibc), outside the write lock. Before the switch, malloc_trim alone measured:

CorpusResident beforeAfterTook
tokio + Django135 MB107 MB8 ms
Linux kernel793 MB709 MB36 ms
Linux + LLVM + Rust + Kubernetes (302,000 files)1,718 MB1,613 MBnot timed

What is left above the live heap is fragmentation: freed memory that shares pages with live data. Compacting the posting lists after a build (reallocating each at its exact size) was tried. It did not lower resident memory and made finalize about 200 ms slower on tokio + Django, so it is not done. corpus_bench shows everything outside the estimates as “not estimated”.

Known limitations: on a small index mimalloc holds a little more resident memory than glibc did (17 MB more on tokio + Django); on a large one it holds less. Resident memory after a build stays higher than after a restart. The breakdown leaves out the allocator, thread stacks, mapped content, the regex cache and spare capacity inside posting lists, so it reads low, most of all just after a build. Use it to see which part grew between two runs, not as an exact total.

Limits that protect the server

LimitValueWhere
Page size1 to search.max_page_size (1,000); default search.default_page_size (50)max
Offset≤ search.max_page_offset (100,000)offset; deeper pages are refused
Files readbest first; search.fast_rank_files (no limit by default), up to search.max_fast_rank_files on requestmax_files
Match budgetsearch.min_match_budget (32,768) to search.max_match_budget (100,000), search.match_budget_multiplier (8) × page end; about 230 bytes per match while a search runsstops the scan; reported as truncated_by_budget and limits_hit
Deadlinetimeout_ms or just under request_timeout_secsthe scan stops on its own even if the HTTP response was abandoned
Context lines≤ search.max_context_lines (10) per hit (/api/context: ≤ 200)context
Concurrent searchesmax_concurrent_searches, shared by REST and gRPCbeyond it: 503 / RESOURCE_EXHAUSTED immediately
Regexcompiled size and DFA size limitsabsurd patterns are a 400
Per-file hitssearch.per_file_matches (1,000) per document per search, its best by score; the budget from result 1000 onkeeps a broad regex bounded
File sizemax_file_size (default 10 MB)larger files are not indexed
WebSocket frames4 KiB inbound/ws/progress ignores inbound data

When a limit cuts a search short, the response names it in limits_hit (max_files, match_budget, per_file_cap, timeout or indexing, each with a sentence for people). gRPC sends the names in the fcs-limits-hit response header, fcs prints them on stderr, and MCP adds a “Results capped” line, so a capped result never passes for a complete one.

None of these need tuning for normal use; they exist so a bad query or an abandoned client cannot pin the machine. The defaults are measured; see Search limits. MCP’s own limits are in [mcp] (search_code 50 hits by default, at most 1,000, paged with offset).