rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.25k stars 1.6k forks source link

abnormal memory usage #13673

Open yukkit opened 1 year ago

yukkit commented 1 year ago

After one or two days of continuous use, the vscode window will restart without responding

image

Analysis of sampling rust-analyzer (pid 58597) every 1 millisecond
Process:         rust-analyzer [58597]
Path:            /Users/USER/*/rust-analyzer
Load Address:    0x104fa4000
Identifier:      rust-analyzer
Version:         0
Code Type:       ARM64
Platform:        macOS
Parent Process:  Code Helper [58569]

Date/Time:       2022-11-25 12:00:53.929 +0800
Launch Time:     2022-11-24 13:44:51.917 +0800
OS Version:      macOS 13.0 (22A380)
Report Version:  7
Analysis Tool:   /usr/bin/sample

Physical footprint:         5.3G
Physical footprint (peak):  5.3G
Idle exit: untracked

rust-analyzer version: (eg. output of "rust-analyzer: Show RA Version" command, accessible in VSCode via Ctrl/⌘+Shift+P)

v0.4.1297

rustc version: (eg. output of rustc -V)

rustc 1.64.0 (a55dd71d5 2022-09-19)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME)

{
    "search.followSymlinks": false,
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/tmp": true,
        "**/node_modules": true,
        "**/bower_components": true,
        "**/dist": true
    },
    "files.watcherExclude": {
        "**/.git/objects/**": true,
        "**/.git/subtree-cache/**": true,
        "**/node_modules/**": true,
        "**/tmp/**": true,
        "**/bower_components/**": true,
        "**/dist/**": true
    },
    "workbench.editor.untitled.hint": "hidden",
    "workbench.startupEditor": "none",
    "explorer.confirmDelete": false,
    "workbench.sideBar.location": "right",
    "cmake.configureOnOpen": true,
    "terminal.integrated.fontFamily": "Meslo LG M for Powerline,Menlo, Monaco, ‘Courier New’, monospace",
    "files.autoSave": "onFocusChange",
    "security.workspace.trust.untrustedFiles": "open",
    "update.mode": "manual",
    "extensions.autoCheckUpdates": false,
    "workbench.colorCustomizations": {
    "[Solarized Light]": {

        "editorInlayHint.typeBackground": "#f8f5f5",
        "editorInlayHint.typeForeground": "#b2afaf",
        // "editor.background": "#c7edcc",
        "editor.background": "#cfe6d1",
    },
    "[One Dark Pro]": {
        // "editorInlayHint.typeBackground": "#090808",
        "editorInlayHint.typeForeground": "#747070"
    }
    },
    "editor.fontSize": 13,
    "workbench.colorTheme": "One Dark Pro",
    "vscodeGoogleTranslate.preferredLanguage": "Chinese (Simplified)",
    "rust-analyzer.inlayHints.closureReturnTypeHints.enable": "always",
    "rust-analyzer.checkOnSave.enable": true,
    "rust-analyzer.cargo.features": "all",
    "git.autofetch": true,
    "diffEditor.ignoreTrimWhitespace": false,
    "gitlens.advanced.messages": {
        "suppressNoRepositoryWarning": true
    },
    "window.zoomLevel": 1,
}
bjorn3 commented 1 year ago

How big is the project you are working on?

yukkit commented 1 year ago

How big is the project you are working on?

image @bjorn3

bjorn3 commented 1 year ago

How many direct and indirect dependencies do you have? You can count the amount of [[package]] in Cargo.lock for this.

yukkit commented 1 year ago

How many direct and indirect dependencies do you have? You can count the amount of [[package]] in Cargo.lock for this.

image

bjorn3 commented 1 year ago

Based on the size of your project I did expect <3GB of ram usage. Would you mind trying the rust analyzer debug: memory usage command? Note that you will have to restart rust-analyzer after running it as it brings rust-analyzer into an inconsistent state.

yukkit commented 1 year ago

Based on the size of your project I did expect <3GB of ram usage. Would you mind trying the rust analyzer debug: memory usage command? Note that you will have to restart rust-analyzer after running it as it brings rust-analyzer into an inconsistent state.

okay, when the memory usage is abnormal, I will execute debug memory usage in the vscode window

lnicola commented 1 year ago

Based on the size of your project I did expect <3GB of ram usage.

Do you usually keep it running for days at a time? I also feel like the memory usage is steadily increasing.

yukkit commented 1 year ago

Do you usually keep it running for days at a time? I also feel like the memory usage is steadily increasing.

image

How can I run these commands from vs code? This doesn't seem to be the result I expected

lnicola commented 1 year ago

I think the memory reporting doesn't work on some platforms.

serkanserttop commented 1 year ago

I have a similar problem, so I would like to contribute my experience, hoping it helps to diagnose the issue a bit more. I was surprised to see that I had 4.5 GB of memory consumed by rust-analyzer. The worst problem was that I did not even have any Rust project open for a week or more. I have about 10 VS Code projects open, but none are related to Rust. That tells me that rust-analyzer is either keeping track of files that it should not (non-Rust), or it is holding onto files even if the related project is closed in VSCode. The Rust projects open were rustbook tutorials, and I can't imagine them to consume so much memory. I disabled the extension, force quit rust-analyzer, but it still came back with 4.5 GB. Then I closed VS Code, which ensured that rust-analyzer did not start on its own. I hope this gives some idea where to look. I am on Mac M1, Monterey 12.3. VSCode rust-analyzer version is v0.3.1325.

bjorn3 commented 1 year ago

The extension should only start if a Cargo.toml or *.rs file is detected. Is there any specific non-rust project for which it starts?

serkanserttop commented 1 year ago

@bjorn3 I found the problematic project, it is https://github.com/aws-samples/serverless-patterns There are many folders in it, and I found 18 Cargo.toml files. I never opened a Rust file there though. I think it is best to enable the extension per project, which I have done already. This solves my problem, but I leave many JS projects open, and I don't run into similar problems. I think an optimization can be done regarding memory usage. 4.5 GB is crazy when I don't have a single Rust file open in VSCode. Few questions come to my mind. Does it run for files that are not Rust? How do you handle monorepo codebases? Maybe rust-analyzer should only be triggered if a Rust file is open in VSCode. Is the only way to work on such a project would be to open only a single Rust folder as a project? I will keep an eye out for this when I work on some Rust stuff.

flodiebold commented 1 year ago

There's lots of discussion on the topic of lazily loading projects and handling big monorepos in #8631 and #8622. This issue is about memory usage in a single project though.

serkanserttop commented 1 year ago

Thanks @flodiebold , I skimmed it quickly, there are some similar comments. I will check those out.