lewis6991 / satellite.nvim

Decorate scrollbar for Neovim
MIT License
542 stars 20 forks source link

Memory leak when switching windows #33

Closed 0xAdk closed 1 year ago

0xAdk commented 1 year ago

Describe the bug Every time I switch windows a little bit of memory is added to the neovim process.

Eventually causing stutters when switching windows and neovim taking up 5 GB+ of memory

To Reproduce

To Reproduce With mini_config.lua

mini_config.lua ```lua local plug_dir = vim.env.HOME .. '/.local/share/nvim/site/pack/packer/start' vim.opt.rtp:append { plug_dir .. '/satellite.nvim', } require 'satellite'.setup {} vim.keymap.set('n', 't', 'jk') vim.api.nvim_buf_set_lines(0, 0, -1, false, vim.fn['repeat']({''}, 200)) vim.cmd.split() ```

Expected behavior When switching windows neovim's memory doesn't permanently increase

justinmk commented 1 year ago

I noticed something related(?), specifically

  1. BufNew is triggered even when visiting existing buffers
  2. [No Name] buffers are created whenever I switch buffers

Steps to reproduce

nvim +"au BufNew * echom localtime()"`
:edit .bashrc
:edit tmux.config
:ls!      
  1 %a   ".bashrc"                      line 198                                                                                      
  2u  -  "[No Name]"                    line 1
  3 #h   "tmux.config"                  line 1
  4u  -  "[No Name]"                    line 1
  5u  -  "[No Name]"                    line 1
  6u  -  "[No Name]"                    line 1
  7u  -  "[No Name]"                    line 1
  8u  -  "[No Name]"                    line 1
  9u  -  "[No Name]"                    line 1
 10u  -  "[No Name]"                    line 1
 11u  -  "[No Name]"                    line 1
 12u  -  "[No Name]"                    line 1
 13u  -  "[No Name]"                    line 1
 14u  -  "[No Name]"                    line 1
 15u  -  "[No Name]"                    line 1
 16u  -  "[No Name]"                    line 1
 17u  -  "[No Name]"                    line 1
 18u  -  "[No Name]"                    line 1
lewis6991 commented 1 year ago

Would it be possible to provide a config with the minimum handlers enabled? Any one of them could be causing the leak.

lewis6991 commented 1 year ago

I was able to determine a memory leak, but not in this plugin.

Min repro:

local bufnr = vim.api.nvim_create_buf(false, true)

local lines = vim.fn['repeat']({''}, 200)
for _ = 1, 100000 do
  vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
end

nvim --clean -u min.lua

lewis6991 commented 1 year ago

Raised: https://github.com/neovim/neovim/pull/24134. Hopefully ASAN will blow up.

lewis6991 commented 1 year ago

Ok, this is not strictly a memory leak. What's happening is the undo information is building. Deleting the buffer or setting undolevels=-1 frees up the memory.

justinmk commented 1 year ago

Thanks for digging into that. The [No Name] and BufNew spam is still an issue though; I guess the use of nvim_create_buf() means that is somewhat expected...? Is it feasible to re-use a dedicated scratch buffer instead?

lewis6991 commented 1 year ago

I'm not able to reproduce that issue with nvim --clean + satellite.nvim. Are you sure it is due to this plugin?

I only get a single scratch buffer, which is what I expect since this plugin creates a scratch buffer per window.

image
0xAdk commented 1 year ago

I was pretty certain this was a memory leak at the time. Double checking with a git bisect turns out this issue was actually resolved along with #40 in e122514.

Also, I am able to recreate the unnamed buffer spam with just satellite.nvim + a mini config

vim.opt.rtp:append { vim.fn.getcwd() .. '/satellite.nvim' }
require 'satellite'.setup {}

nvim --clean -u mini_config.lua ~/.bashrc +enew then holding <c-^>

lewis6991 commented 1 year ago

Many thanks, I could repro that. The thing needed was a buffer small enough to not display the window. Each time <c-^> was pressed the scrollbar was created for .bashrc and deleted for [No Name]. The problem was I had the scrollbar buffer's bufhidden=delete. Changing to wipe stops the spam, though it doesn't stop buffers from being deleted+created.

Not really sure if the incrementing buffer number is a problem or worth fixing.

justinmk commented 1 year ago

The thing needed was a buffer small enough to not display the window.

Ah yes, the [No Name] and BufNew spam only happen if the satellite scrollbar actually appears. If the buffer contents fit in the window, then <c-^> doesn't create a scrollbar thus doesn't create a new buffer.

Not really sure if the incrementing buffer number is a problem or worth fixing.

Visiting a buffer should not trigger BufNew. But satellite may be blocked by Nvim limitations?

lewis6991 commented 1 year ago

Visiting a buffer should not trigger BufNew. But satellite may be blocked by Nvim limitations?

I've blocked this autocmd via 'eventignore'.