p00f / clangd_extensions.nvim

Clangd's off-spec features for neovim's LSP client. Use https://sr.ht/~p00f/clangd_extensions.nvim instead
MIT License
446 stars 16 forks source link

clangd can use all system memory, including up to 64 gigabytes of ram #62

Closed jbwyatt4 closed 1 day ago

jbwyatt4 commented 1 week ago

Copying and pasting this issue report from this link: https://github.com/nvim-lua/kickstart.nvim/issues/773

Describe the bug To Reproduce

1) Please download the Linux kernel source 2) Build a kernel for the LSP as documented here: https://gitlab.com/jbwyatt4/vim-megarepo/-/blob/main/neo-elk/Workflow.md?ref_type=heads 3) Go to any .c file, the indexer, clangd, may take up to the full system memory (which on my system is 64 gigabytes of ram) and cause an out of memory situation or be killed by an OOM killer.

Desktop

OS: Fedora 39
Terminal: Version 3.50.1 for GNOME 45
A terminal emulator for the GNOME desktop
Using VTE version 0.74.2 +BIDI +GNUTLS +ICU +SYSTEMD

Neovim Version

NVIM v0.9.5
Build type: RelWithDebInfo
LuaJIT 2.1.1692716794
Compilation: /usr/bin/gcc -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-para
meter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla -Wdouble-promot
ion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -fno-comm
on -Wno-unused-result -Wimplicit-fallthrough -fdiagnostics-color=auto -fstack-pr
otector-strong -DUNIT_TESTING -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DU
SING_UV_SHARED=1 -I/usr/include/luajit-2.1 -I/usr/include -I/usr/include/luv -I/
builddir/build/BUILD/neovim-0.9.5/redhat-linux-build/src/nvim/auto -I/builddir/b
uild/BUILD/neovim-0.9.5/redhat-linux-build/include -I/builddir/build/BUILD/neovi
m-0.9.5/redhat-linux-build/cmake.config -I/builddir/build/BUILD/neovim-0.9.5/src
 -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/usr/include -I/u
sr/include -I/usr/include

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

This has been going on for a while. How do I limit the memory it uses in Neovim? I would to limit the indexer to 2 or 8 gigs of memory. I checked the documentation on memory, but I am not sure of how to use it to limit it. https://clangd.llvm.org/extensions#memory-usage

My config:

https://gitlab.com/jbwyatt4/vim-megarepo/-/blob/dev/neo-elk/init.lua?ref_type=heads

p00f commented 1 week ago

The memory usage feature is just for viewing where the memory is used.

LLVM and Chromium have online indexes (updated nightly or so), you can see if there's one for the kernel: https://clangd.llvm.org/design/remote-index

p00f commented 1 week ago

You can use cgroups to limit memory usage

jbwyatt4 commented 1 week ago

What command with cgroups would you use to limit memory usage? I would like something that is very automated.

LLVM and Chromium have online indexes (updated nightly or so), you can see if there's one for the kernel: https://clangd.llvm.org/design/remote-index

This talks about project wide indexes, but not the LLVM or Chromium ones. Do you have links to those?

jbwyatt4 commented 1 week ago

If you are talking about the compile_commands.json you generate it with this:

https://gitlab.com/jbwyatt4/vim-megarepo/-/blob/dev/neo-elk/Workflow.md?ref_type=heads#building-the-compile_commandsjson-for-the-upstream-kernel

p00f commented 6 days ago

What command with cgroups would you use to limit memory usage

One-time setup 1) ~/.config/systemd/user/clangd.slice:

[Slice]
MemoryMax=2G

2) systemctl --user daemon-reload

Then, systemd-run --user --slice=clangd.slice clangd

This talks about project wide indexes, but not the LLVM or Chromium ones. Do you have links to those?

https://linux.clangd-index.chromium.org/, https://clangd-index.llvm.org/

If you are talking about the compile_commands.json

?

p00f commented 6 days ago

To automate this, you can use 'exrc' - in the kernel repo, create a .nvim.lua which starts clangd under the slice

p00f commented 6 days ago

Or you could check the cwd when calling vim.lsp.start and change the cmd based on that.

p00f commented 6 days ago

You also need to add --pty to the systemd-run command as the server works over stdio

systemd-run --user --slice=clangd.slice --pty clangd

p00f commented 6 days ago

20240701_15h43m26s_grim

jbwyatt4 commented 1 day ago

Thank you @p00f. I went with the clangd.slice option. I did not need to reload the systemd service as it seems to terminate itself when Neovim is not in use for the way my bundle distribution is setup.

There is still one more problem but I will open another issue for it. Closing this issue.

p00f commented 1 day ago

Yes, with --pty it's like a normal terminal application, it is not backgrounded like a systemd service