Skip to content
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

TexLab consumes all the memory #856

Closed
jakubkaczor opened this issue Feb 24, 2023 · 26 comments
Closed

TexLab consumes all the memory #856

jakubkaczor opened this issue Feb 24, 2023 · 26 comments
Labels
bug Something isn't working

Comments

@jakubkaczor
Copy link

As in the title, TexLab consumes all the memory. The steps to reproduce are the following.

  1. In the home directory, edit a new or existing file. In this step, mostly default configuration of NeoVim's nvim-lspconfig automatically runs TexLab and attaches.
nvim test.tex
  1. Switch the focus to the other window (in the window manager, not the editor).

Then the memory usage of TexLab increases rapidly. Additionaly, when in the same home directory I create a new subdirectory, “cd” into it and then open the file, the same happens, but the increase in memory usage is much slower. That is

cd ~
mkdir test
cd test
nvim test.tex

I proceeded with git bisection between v5.2.0 (bad) and v4.3.2 (good), and according to this, and if I did everything correctly, f0d3996 is the first bad commit.

I couldn't see anything in NeoVim's LspLog, but I didn't increase its verbosity level.

@jakubkaczor
Copy link
Author

Also, I would like to note, that even on v4.3.2 I notice a big bump in the memory consumption after switching the window focus, to around 3.75 GiB, but it stops there.

@pfoerster
Copy link
Member

@jakubkaczor It looks like texlab is loading/parsing way too many files while trying to figure out the dependency tree. Can you create a texlab log file using texlab -vvvv --log-file=/tmp/texlab.log? The server logs files loaded from the disk.

@pfoerster pfoerster added the bug Something isn't working label Feb 25, 2023
@jakubkaczor
Copy link
Author

@pfoerster, sure. Here is the output.

@pfoerster
Copy link
Member

@jakubkaczor Sorry the long wait. The log file looks very odd. First of all, it looks like texlab does not report loading any additional documents from the disk. Additionally, it appears like it has loaded the distro twice. Maybe this could be the reason and texlabs keeps loading the distro.

I couldn't see anything in NeoVim's LspLog, but I didn't increase its verbosity level.

Can you try increasing the verbosity level and see if Neovim reports something?

@jakubkaczor
Copy link
Author

jakubkaczor commented Mar 9, 2023

@jakubkaczor Sorry the long wait. The log file looks very odd. First of all, it looks like texlab does not report loading any additional documents from the disk. Additionally, it appears like it has loaded the distro twice. Maybe this could be the reason and texlabs keeps loading the distro.

One thing I thought may be obvious, but it wasn't: do you expect me to send the log generated with the latest release, latest main, or the commit I firstly experienced the issue with? I assumed the latter and now, when I compile it, I can see a warning that may be related.

warning: unused variable: `document`
   --> src/server.rs:361:13
    |
361 |         let document = workspace.open(
    |             ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_document`
    |
    = note: `#[warn(unused_variables)]` on by default

Can you try increasing the verbosity level and see if Neovim reports something?

In Neovim configuration I set the running command as

cmd = {"texlab", "-vvvv", "--log-file=/tmp/texlab.log"}

As the file is written, I assume the change was effective, and the verbosity level is as desired. I additionally set NeoVim's LSP log level to trace (the most verbose), and now in :LspLog I mentioned, there is more information. I can see multiple entries with directories outside /home/jakub/test, which is the current working directory I open the test file from. These are siblings of test directory or their descendants. I don't want to expose the directory structure of my home directory, therefore I replaced these with sequences of question marks. Here is the file.
vim-lsp.log

@pfoerster
Copy link
Member

@jakubkaczor Thank you for providing the log file.

These are siblings of test directory or their descendants.

It looks like texlab is trying to load your entire home directory into memory. As of texlab 5.4.0, there is a new workspace command which could be helpful here. Can you inspect the dependency graph using texlab.showDependencyGraph, please?

Sorry to ask about your directory structure, but

  • Is there a file which the top of the directory tree which includes many files? Then, the server loads the transitive closure.
  • Also, is your directory structure very flat so that you have a lot of TeX files in one directory? texlab would load all files in the directory currently.

One thing I thought may be obvious, but it wasn't: do you expect me to send the log generated with the latest release, latest main, or the commit I firstly experienced the issue with?

I think it is more helpful to look at the latest release instead of f0d3996. The server has changed a lot internally in the mean time.

@gi1242
Copy link

gi1242 commented Mar 19, 2023

Hi There,

I'm having the same problem. (I don't know if I can trace it to switching windows.) But when I start off texlab's memory footprint is negligible. After about 20 mins of editing it's climbed to 1.5GB of my system memory.

After a long editing session yesterday I noticed texlab was up to 4GB+ of memory usage.

I'd be happy to give you more info (e.g. output of texlab.showDependencyGraph), except I couldn't figure out how to run it under neovim. (I'm using Arch linux and Neovim in case it makes any difference.)

Any suggestions?

GI

@gi1242
Copy link

gi1242 commented Mar 21, 2023

I just checked that I still have this issue with 5.4.0.

As a temporary work-around, I got my editor to restart texlab periodically. If anyone else uses neovim, and has this issue, then you can use the following in your LSP config file:

local on_attach = function( client, bufnr )
    -- vim.keymap.set(...) and other LSP settings

    -- Kill in texlab periodically to avoid memory leak bug
    if client.name == 'texlab' then
        vim.defer_fn( function ()
            if vim.api.nvim_get_current_buf() == bufnr then
              vim.cmd('LspRestart')
            else
              client.stop()
              print( 'Stopped TexLab' )
            end
          end, 10*60*1000 )
    end
end

and pass it to the setup function in your LSP config.

@pfoerster
Copy link
Member

I'd be happy to give you more info (e.g. output of texlab.showDependencyGraph), except I couldn't figure out how to run it under neovim. (I'm using Arch linux and Neovim in case it makes any difference.)

Unfortunately, I am not that familiar with Neovim but you need a way to bind the workspace/executeCommand request to a Neovim command. Maybe, you can take a look at nvim-lspconfig and see how the forward search request is implemented:

https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/server_configurations/texlab.lua

@gi1242 Can you tell me more about the editing session? Does the RAM usage also increase if you are only editing the same files over and over or are you switching between different files/projects?

@gi1242
Copy link

gi1242 commented Mar 28, 2023

@gi1242 Can you tell me more about the editing session? Does the RAM usage also increase if you are only editing the same files over and over or are you switching between different files/projects?

I'm editing the same file only (not switching files, or projects). The issue always happens on my system. Periodically killing texlab (like I did above) makes it somewhat usable (sometimes I also have to quit the editor and restart). But at least now my system isn't freezing due to lack of memory :).

I will try and figure out how to run workspace/executeCommand and send you output.

@gi1242
Copy link

gi1242 commented Mar 29, 2023

OK, I may have something useful: I ran texlab in a limited memory environment. On Linux I do this by

systemd-run -q --user --scope -p MemoryLimit=500M texlab

Now I go about editing as usual (same file, same setup). I notice that texlab doesn't get killed. (If it uses more than 500M memory, it will automatically get killed.)

My guess is that texlab was aggressively caching something (or there was some garbage collection issue), and it felt comfortable taking up my 8GB RAM + 16 GB swap. (When texlab reached 4GB my system was constantly swapping and really slow.)

I'll post back in a few days if I can confirm it's stable.

@pfoerster
Copy link
Member

My guess is that texlab was aggressively caching something

Yeah, I think the syntax trees built from the related files are an issue here. Currently, texlab keeps the parse trees of "hidden" but needed documents in memory even though they are not being edited. In this case, it makes more sense to index the file first and then discard the parse tree. I am currently working on a solution.

@jakubkaczor
Copy link
Author

@jakubkaczor Thank you for providing the log file.

These are siblings of test directory or their descendants.

It looks like texlab is trying to load your entire home directory into memory. As of texlab 5.4.0, there is a new workspace command which could be helpful here. Can you inspect the dependency graph using texlab.showDependencyGraph, please?

Sorry to ask about your directory structure, but

* Is there a file which the top of the directory tree which includes many files? Then, the server loads the transitive closure.

* Also, is your directory structure very flat so that you have a lot of TeX files in one directory? `texlab` would load all files in the directory currently.

One thing I thought may be obvious, but it wasn't: do you expect me to send the log generated with the latest release, latest main, or the commit I firstly experienced the issue with?

I think it is more helpful to look at the latest release instead of f0d3996. The server has changed a lot internally in the mean time.

I'm sorry I won't be able to dig into it deeper this week. In NeoVim configuration file for the server we have a function determining the root directory. When one of the conditions in the body is satisfied, that is, I have .latexmkrc or .git directory in the directory where the file I am editing is in, then everything works as expected. But when I want to edit a single LaTeX file, it appears texlab walks the directory structure until it reaches what it wants. But I would expect it not to do it at all (single_file_support).

@gi1242
Copy link

gi1242 commented Mar 29, 2023

I am currently working on a solution.

Thank you so much. If it helps: the file I've been noticing issues with is about 200kb (3500 lines). The associated .bib file has about 10,000 entries.

I was working on smaller files on systems with more memory before and never had any issues. Probably because texlab wasn't using enough memory to make my system swap...

@pfoerster
Copy link
Member

Can you try out #875, please? It should help when switching between different, unrelated projects.

@gi1242
Copy link

gi1242 commented Apr 2, 2023

OK, I tried it. I was having the trouble even without switching files before; so I tried it on the same file. The memory growth is more managable, but still present. Now when I'm not doing anything, the memory doesn't grow. The more I type the more the memory grows. If I ask it to complete refs and citations from a large file I can quickly get the memory usage up to some 1.5GB.

I'll use it (without a memory quota) for the next week or so and report back.

@clason
Copy link
Contributor

clason commented Apr 3, 2023

I've been investigating a bit myself on macOS and I cannot quite reproduce this memory growth (with a modest ~450 page project) -- asking for references or citation completion adds a few megabytes of memory, at most, and just typing doesn't change much; the memory usage remains at 100-200MB. The biggest jump comes from citations, so this:

The associated .bib file has about 10,000 entries.

is almost certainly the culprit. Is this a local bib file, or something in your global personal texmf tree? (I discourage people from using "global bibliographies" as these tend to create more problems than add convenience. Also, bibexport or biber --tool are your friends.)

In any case, citation handling looks to be the place to look at for possible optimizations.

@gi1242
Copy link

gi1242 commented Apr 5, 2023

OK, I've been using it for a bit now and the behavior seems consistent: After a long editing session the memory usage climbs to a little less than 1GB. It doesn't seem to increase beyond that (unllike before where it would grow without bound, even when I wasn't using vim).

Memory only grows when I type.

So it's manageable... but probably not ideal. (It's the single highest memory user on my system now; more than firefox, which has some 20 tabs open)

If you think the bib file is the culprit, I'm attaching it. (I know you don't like global bib's, but using this (and bibtool to merge other bibliographies) makes my life a lot simpler...)

If you can reproduce the memory growth, then we have confirmation.

refs.bib.gz

@charleschetty
Copy link

Same issue In ArchLinux and neovim

I've been investigating a bit myself on macOS and I cannot quite reproduce this memory growth

Is it possible that the problem is related to the operation system? Just a guess

@DimitrisZav
Copy link

If it helps I have same issue on ArchLinux (EndeavourOS) using doom emacs. Texlab can reach 8GB on large documents or when writing for about 4hours.

@pfoerster
Copy link
Member

I have found the cause of the memory leak and created a PR (#877) which fixes the issue (quite large diff but most of the changes result from crate splitting). Can you try it out, please?

The memory usage should be stable now. Previously, it was possible to keep increasing the memory usage by repeatedly sending textDocument/didChange notifications to the server.

@pfoerster
Copy link
Member

@clason

In any case, citation handling looks to be the place to look at for possible optimizations.

Yeah, there are still some optimizations to be made here. As these large bibliography files rarely change, we can precompute most of the data required to provide completion.

Discarding the parse tree and relying on the index instead would also help here.

@gi1242
Copy link

gi1242 commented Apr 10, 2023

I checked. Memory footprint doesn't grow like it used to. (I'll keep using it this week).

However, now the old bug about pre-compiled headers in issue #845 seems to have resurfaced. Did that patch get affected (or did I mess up).

@pfoerster
Copy link
Member

@gi1242

However, now the old bug about pre-compiled headers in issue #845 seems to have resurfaced. Did that patch get affected (or did I mess up).

Thanks for catching this one! This should be fixed now.

@gi1242
Copy link

gi1242 commented Apr 10, 2023

Yes #845 is fixed now. If I see uncontrolled growth in memory, I'll let you know. As of now (even with my large bib files) the memory footprint of texlab is negligible.

Thanks a lot for the quick fix.

@pfoerster
Copy link
Member

Closing this issue for now. Please let me know, if you still encounter a problem.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Apr 27, 2023
## [5.5.0] - 2023-04-16

### Added

- Allow optionally passing cursor position to `textDocument/build` request for use in forward search after building.
  Previously, the server had to guess the cursor position ([#475](latex-lsp/texlab#475))
- Add experimental `texlab.experimental.citationCommands` setting to allow extending the list of citation commands
  ([#832](latex-lsp/texlab#832))
- Add support for escaping placeholders in build arguments similar to forward search
- Allow configuring completion matching algorithm ([#872](latex-lsp/texlab#872))

### Fixed

- Fix regression introduced in `v5.4.2` involving `texlab.cleanArtifacts` command.

## [5.4.2] - 2023-04-11

### Fixed

- Fix memory leak when editing documents over a long time ([#856](latex-lsp/texlab#856))
- Fix parsing parentheses in file paths ([#874](latex-lsp/texlab#874))

## [5.4.1] - 2023-03-26

### Fixed

- Do not return symbols with empty names (e. g. sections without name) ([#870](latex-lsp/texlab#870))
- Repair `textDocument/formatting` request ([#871](latex-lsp/texlab#871))

## [5.4.0] - 2023-03-12

### Added

- Add experimental settings to allow extending the list of special environments:
  - `texlab.experimental.mathEnvironments`
  - `texlab.experimental.enumEnvironments`
  - `texlab.experimental.verbatimEnvironments`
- Add `texlab.changeEnvironment` workspace command ([#849](latex-lsp/texlab#849))
- Add `texlab.showDependencyGraph` workspace command

### Changed

- Do not show caption or section names in label inlay hints ([#858](latex-lsp/texlab#858))
- Include more user-defined commands in command completion

### Fixed

- Parse nested `\iffalse` blocks correctly ([#853](latex-lsp/texlab#853))
- Parse commands with multi-byte characters correctly ([#857](latex-lsp/texlab#857))
- Fix checking whether a document can be a root file

## [5.3.0] - 2023-02-25

### Added

- Allow filtering `textDocument/documentSymbols` using regular expressions specified via
  `texlab.symbols.allowedPatterns` and `texlab.symbols.ignoredPatterns`
  ([#851](latex-lsp/texlab#851))

### Fixed

- Do not use percent-encoded path when searching for PDF files during forward search
  ([#848](latex-lsp/texlab#848))
- Always return an empty list of code actions instead of returning "method not found" ([#850](latex-lsp/texlab#850))

## [5.2.0] - 2023-01-29

### Added

- Include line numbers in build warnings when available ([#840](latex-lsp/texlab#840))
- Add `none` formatter to `texlab.latexFormatter` and `texlab.bibtexFormatter` options
  to allow disabling formatting ([#846](latex-lsp/texlab#846))

### Fixed

- Concatenate more than two lines of maximum length in build diagnostics ([#842](latex-lsp/texlab#842))
- Apply the correct range of references to labels when renaming ([#841](latex-lsp/texlab#841))
- Use `document` environment to detect root file instead of `\documentclass` ([#845](latex-lsp/texlab#845))

## [5.1.0] - 2023-01-21

### Added

- Allow manually overriding the root directory using a `texlabroot`/`.texlabroot` marker file.
  See the wiki for more information.
  ([#826](latex-lsp/texlab#826), [#838](latex-lsp/texlab#838))

### Deprecated

- Deprecate `texlab.rootDirectory` setting in favor of `.texlabroot` files

### Fixed

- Do not use `.git`, `.chktexrc`, `.latexmkrc` files/directories to determine the root directory
  ([#826](latex-lsp/texlab#826))
- Fix building documents without an explicit root directory ([#837](latex-lsp/texlab#837))

## [5.0.0] - 2022-12-29

### Changed

- _BREAKING_: `texlab.rootDirectory` is now used as the folder path from which the compiler is executed
  relative to the main document. By default it is equal to `"."`. For more information, please visit the wiki.
- Improve performance of completion by a huge margin due to a faster filtering method used internally
- Do not discover project files beyond the provided workspace folders
- Try to guess the root directory by checking for files such as `.latexmkrc` or `Tectonic.toml` if `texlab.rootDirectory` is not set

### Fixed

- Update positions of reported build diagnostics when editing the affected line
- Do not treat links to files as bidirectional by default. This prevents issues where `texlab` ends up compiling the wrong file
  in projects with shared files ([#806](latex-lsp/texlab#806), [#757](latex-lsp/texlab#757), [#679](latex-lsp/texlab#679))
- Fix coverage of directories which need to be watched for changes ([#502](latex-lsp/texlab#502), [#491](latex-lsp/texlab#491))
- Resolve links of the `import` package correctly
- Use `filterText` of completion items when filtering internally ([#829](latex-lsp/texlab#829))

## [4.3.2] - 2022-11-20

### Fixed

- Do not try to run the TeX engine on package files and fail the build instead ([#801](latex-lsp/texlab#801))
- Handle URIs with URL-encoded drive letters on Windows ([#802](latex-lsp/texlab#802))
- Parse BibTeX entries with unbalanced quotes correctly ([#809](latex-lsp/texlab#809))
- Provide completion for more acronym commands ([#813](latex-lsp/texlab#813))
- Fix parsing acronym definitions ([#813](latex-lsp/texlab#813))

## [4.3.1] - 2022-10-22

### Fixed

- Do not crash with a stack overflow when trying to load packages with many internal dependencies ([#793](latex-lsp/texlab#793))
- Normalize drive letters of all document URIs
- Fix parsing commands that take file paths as arguments ([#789](latex-lsp/texlab#789))
- Use the correct working directory and command line arguments when calling `latexindent` ([#645](latex-lsp/texlab#645))
- Fix publishing to CTAN

## [4.3.0] - 2022-09-25

### Added

- Add inlay hints for `\label{...}` ([#753](latex-lsp/texlab#753))

### Fixed

- Improve accuracy of the error locations reported by the TeX engine ([#738](latex-lsp/texlab#738))
- Reduce number of false positive errors reported by `texlab` ([#745](latex-lsp/texlab#745))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants