Issue 242835
Summary [clangd] On Windows, clangd keeps more than 10 GiB of freed memory committed after background indexing (rpmalloc)
Labels new issue
Assignees
Reporter namefreezers
    RAM prices made me dig into this: clangd on an idle Chromium checkout was
holding 45 GiB of committed memory.

The Windows x64 release is built with
[`LLVM_ENABLE_RPMALLOC=ON`](https://github.com/llvm/llvm-project/blob/llvmorg-23.1.3/llvm/utils/release/build_llvm_release.bat#L187).
To check: `clang -v` from the same install prints
`Build config: +alloc:rpmalloc`. With rpmalloc, clangd keeps most of the memory it used for background indexing committed
after indexing has finished.

After indexing LLVM itself, clangd 23.1.3 sits at 14.6 GiB of commit, while its
own `$/memoryUsage` accounts for 3.0 GiB. `main` built without rpmalloc settles
at 4.0 GiB on the same workload, 10.6 GiB less. A build of `main` with
rpmalloc's statistics, at 15.0 GiB of commit, holds 11.5 GiB of memory that
clangd has already freed: spans in rpmalloc's caches, and blocks freed by
other threads that wait for their idle owner thread.

### How to reproduce

1. Check out LLVM (measured at `e22471a2c697`) and build it, so that
   `build/compile_commands.json` and the generated headers exist:
 ```
   cmake -G Ninja -S llvm -B build -DCMAKE_BUILD_TYPE=Release -DLLVM_TARGETS_TO_BUILD=X86 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra"
   ninja -C build
 ```
   In this configuration ClangIR is on by default and brings in MLIR; the
   compile database has 7,564 entries and clangd indexes 7,224 TUs.
2. Start the 23.1.3 `clangd.exe` with only
 `--compile-commands-dir=<checkout>/build` (in VS Code:
 `"clangd.arguments": ["--compile-commands-dir=<checkout>/build"]`) and no
   index yet (`build/.cache/clangd/index` absent). Open any C++ file of
 the checkout (I used `clang/lib/Sema/SemaDecl.cpp`) and wait until
 background indexing finishes.
3. Compare the "Commit size" of `clangd.exe` in Task Manager (Details) with
   "clangd: Show memory usage".

Ryzen 9 7900X (12 cores), 64 GB, Windows 11:

| clangd | indexing time | peak commit | commit when idle | `$/memoryUsage` |
|---|---|---|---|---|
| 23.1.3 release | 1020 s | 15.9 GiB | 14.6 GiB | 3.0 GiB |
| `main` (e22471a2c697), `LLVM_ENABLE_RPMALLOC=ON` | 1059 s | 16.0 GiB | 14.7 GiB | 3.0 GiB |
| `main`, `LLVM_ENABLE_RPMALLOC=OFF` | 3469 s | 7.4 GiB | 4.0 GiB | 3.0 GiB |

Indexing time runs from the first to the last TU indexed in clangd's log.

### Cause

- rpmalloc keeps freed spans in a per-thread cache and in a global cache.
- A span freed by threads that don't own it (a large or huge block, or a
  small or medium span whose blocks were all freed by other threads) goes on a
  list of the owning heap (`_rpmalloc_deallocate_defer_free_span`), and only
  the owner processes that list, the next time it needs a new span. This
  includes huge blocks (over about 4 MiB).
- `rpmalloc_thread_collect()` is declared, but it is empty in the vendored
  rpmalloc 1.4.5.
- The background-index workers allocate and free many GiB and then block in
  `BackgroundQueue::work()`. They don't allocate again until new work arrives,
  so what they cached and what other threads freed for them stays committed.
- The periodic cleanup from https://reviews.llvm.org/D93452 only exists for
  glibc (`malloc_trim`). It also runs on a single thread, which with rpmalloc
 could not release the other threads' caches anyway.

clangd/vscode-clangd#712 ("Excessive memory usage with clangd 19", Windows)
looks like the same problem: LLVM 19 was the first release built with rpmalloc.

### Possible fix

I have a patch that makes every background-index worker return its cached
memory when the queue goes idle. It implements `rpmalloc_thread_collect()` in
the vendored copy and adds a small `sys::Process` function for it. On the
workload above, commit when idle goes from 14.8 GiB to 5.2 GiB (a newer `main`
without and with the patch, run back to back). On this workload I measured no
difference in indexing throughput or in edit and completion latency. I'll
open a PR.

I used an AI assistant (Claude Fable 5.2/Opus 5.5; GPT-6 Astra for additional reviews) for parts of the analysis and for drafting
this; I've checked the code and the numbers myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to