New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Treesitter: 'query_gc, tree_gc, treecursor_gc, etc' are never called (aka memory leaks) #14216
Comments
Hmm at least |
regarding lua references from c code, we can probably ad a simple counter for |
Looks like the querycursors are not freed because of some functions set using This is slightly problematic though; the decoration providers are meant to be active for pretty much the lifetime of Neovim, meaning that there will be a ton of querycursors that end up never being freed unless we explicitly make a call to |
the decoration_provider callbacks doesn't "own" anything by upvalues or otherwise, they are just global entry points that look up the actual state per bufnr. |
in addition, a _on_end callback could be added to set |
Yeah I had also realized I was wrong re: the decor providers. I will have to think about this some more |
BTW in #14226 I will try to add check for missing |
It seems (ofc, I may be wrong again) that the source of the leaks is from the closure returned from Can be verified by adding the following lines to @@ -383,9 +385,12 @@ function Query:iter_captures(node, source, start, stop)
start, stop = value_or_node_range(start, stop, node)
local raw_iter = node:_rawquery(self.query, true, start, stop)
+ local tmp = newproxy(true) -- create a blank userdata
+ getmetatable(tmp).__gc = function() print("HELP MEEEE") end
local function iter()
local capture, captured_node, match = raw_iter()
local metadata = new_match_metadata()
+ local a = tmp
if match ~= nil then Opening the buffer then wiping it, and running the GC doesn't print anything, even though it should. |
Is this with fixing the above known problems ( i e like in #14226)? I will try to look more into it but if we cannot root case it we can always add a few extra |
nvim --version
: latest masterSteps to reproduce using
nvim -u NORC
In
src/nvim/lua/treesitter.c
, add logging messages to each of the various__gc
methods for treesitter userdata:tree_gc, query_gc, parser_gc, treecursor_gc, querycursor_gc
.Then, run
Actual behaviour
None of the logging messages show up, indicating that none of the treesitter machinery has been garbage collected.
I believe that garbage collection is not being called because there are some dangling references to these items from C code - mostly from
nvim_buf_attach
orset_decoration_provider
. For example,tree_gc
andparser_gc
were not being called because we did not free the nvim_buf_attach functions entirely: making the following modification tobuffer_updates.c
fixes this issue for those two functions:However, for some reason, I can't seem to ever get
query_gc, treeecursor_gc, querycursor_gc
to be called at all, meaning that a ton of memory allocated for lua userdata is never freed. Reloadingeval.c
several times already brings up neovim memory usage to around 10% of my system's memory.I'll need to investigate this issue further, but I would appreciate some help in this - I've been trying to fix this for a while now and I'm still stuck. Alternatively, it's possible that this isn't actually a problem at all and the leaks are coming from elsewhere.
@bfredl
The text was updated successfully, but these errors were encountered: