rust-lang / rust-analyzer

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

Memory Leak #7439

Open alanhkarp opened 3 years ago

alanhkarp commented 3 years ago

The Rust analyzer uses a bit less than 1 GB of memory for each of my 2 projects when I start VSCode. After several weeks, it starts to get slow, no longer keeping up with my typing. When I look, I see it is consuming almost 2 GB. My project defines no macros, but I use lots of crates that do.

lnicola commented 3 years ago

Just to make sure, are you saying you keep it running for a couple of weeks?

alanhkarp commented 3 years ago

Or longer. I think the last time I shut VSCode down before this was for a MacOS update.


Alan Karp

On Mon, Jan 25, 2021 at 7:06 PM Laurențiu Nicola notifications@github.com wrote:

Just to make sure, are you saying you keep it running for a couple of weeks?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rust-analyzer/rust-analyzer/issues/7439#issuecomment-767258235, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFAT43LDUYDB56IU232LZDS3YWTHANCNFSM4WSS5MXQ .

flodiebold commented 3 years ago

Can you try running the "Rust Analyzer: Memory Usage" command the next time?

alanhkarp commented 3 years ago

I'll try to remember.


Alan Karp

On Tue, Jan 26, 2021 at 2:03 AM Florian Diebold notifications@github.com wrote:

Can you try running the "Rust Analyzer: Memory Usage" command the next time?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rust-analyzer/rust-analyzer/issues/7439#issuecomment-767436552, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFAT4ZW7Y2CCIR7ELXXTK3S32HP5ANCNFSM4WSS5MXQ .

alanhkarp commented 3 years ago

Just a data point. According to the Mac Activity Monitor, the analyzer increases its memory use by about 100 MB/day when I'm running my application a lot. The analyzer running with VSCode in another window that runs the web server I use for my visualization hasn't increased its memory use much if at all.

I'm sympathetic. The first Java product I was involved in initially had a 1 KB/hour memory leak, which was a huge amount back in 1998.


Alan Karp

On Tue, Jan 26, 2021 at 2:03 AM Florian Diebold notifications@github.com wrote:

Can you try running the "Rust Analyzer: Memory Usage" command the next time?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rust-analyzer/rust-analyzer/issues/7439#issuecomment-767436552, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFAT4ZW7Y2CCIR7ELXXTK3S32HP5ANCNFSM4WSS5MXQ .

Halkcyon commented 3 years ago

I noticed this as well. Over the course of an evening, ra went from ~100MB baseline to nearly 700MB and the build directory kept locking when I tried to build resulting in slow completions.

@flodiebold running the command reset the ra executable to baseline, but I don't think this is what you expected: https://gist.github.com/Halkcyon/6fbf7bd87a74597b82aba5150bb276fc

alanhkarp commented 3 years ago

Rust analyzer is getting slow, so I ran the memory analyzer. I can send you the output, but it doesn't look useful It's a bunch of lines like

  0b TyQuery
  0b TyQuery (deps)
  0b TyQuery (purge)
  0b ValueTyQuery
  0b ValueTyQuery (deps)
  0b ValueTyQuery (purge)

Every line starts with 0b.

Interesting. While I was typing this email, the memory use went from 1.4 GB to 530 MB in the Mac Activity Monitor.


Alan Karp

On Tue, Jan 26, 2021 at 2:03 AM Florian Diebold @.***> wrote:

Can you try running the "Rust Analyzer: Memory Usage" command the next time?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rust-analyzer/rust-analyzer/issues/7439#issuecomment-767436552, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFAT4ZW7Y2CCIR7ELXXTK3S32HP5ANCNFSM4WSS5MXQ .

bjorn3 commented 3 years ago

Memory usage is only reported on Linux with glibc: https://github.com/rust-analyzer/rust-analyzer/blob/d8dfcc2c0cf87162ec5791121f484dd7fde8d17d/crates/profile/src/memory_usage.rs#L27

alanhkarp commented 3 years ago

I have another data point on slow editing responsiveness. VSCode got very slow for editing after a week or so since the last restart, so I checked and saw it was using about 1.5 GB of memory. I closed and reopened VSCode, and it started running with about 900 MB. Editing responsiveness was good, but I saw that the memory footprint quickly jumped to 2.4 GB and is now over 3.1 GB. Editing performance is still good. My experience is all with the same project.


Alan Karp

On Tue, Jan 26, 2021 at 2:03 AM Florian Diebold @.***> wrote:

Can you try running the "Rust Analyzer: Memory Usage" command the next time?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rust-analyzer/rust-analyzer/issues/7439#issuecomment-767436552, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACFAT4ZW7Y2CCIR7ELXXTK3S32HP5ANCNFSM4WSS5MXQ .

alanhkarp commented 3 years ago

Yet another data point. The Rust analyzer is now taking almost 21 GB, but VSCode is quite responsive.

Veykril commented 3 years ago

This "bigger"(new) leak is due to a mistake that was found in https://github.com/rust-analyzer/rust-analyzer/issues/8181 which got introduced this week

SimonSapin commented 3 years ago

Hi. I have a running rust-analyser process taking 24 GB of resident memory. Is there something I can do to help find out where that space is being spent inside the process? This is in Code OSS (Archlinux’s build of VSCode).

Can you try running the "Rust Analyzer: Memory Usage" command the next time?

I only have a "Rust Analyzer: Memory Usage (Clears Database)" command, which sounds like it would destroy the evidence I’d like to investigate.

bjorn3 commented 3 years ago

That is the right command. It calculates the memory usage by comparing the memory usage before and after clearing certain parts of the internal database used for caching.

SimonSapin commented 3 years ago

Ok, I can try it, but is there another command that can trace memory usage without clearing, in case keeping pathologically caches around is useful for further investigation? It takes a long time of actively using the IDE to get to this point, and I’m not sure how to reproduce.

bjorn3 commented 3 years ago

Not that I know of.

SimonSapin commented 3 years ago

After a couple minutes with one CPU thread busy, I got the report below but resident size of the process is basically unchanged

``` Per-query memory usage: 198mb HygieneFrameQuery 196mb FileItemTreeQuery 76mb MacroArgTextQuery 40mb InternEagerExpansionQuery 35mb CrateDefMapQueryQuery 28mb FileTextQuery (purge) 28mb BodyQuery 27mb ImplDatumQuery 25mb ImplDataQuery 24mb TraitSolveQuery (purge) 22mb BodyWithSourceMapQuery 21mb GenericParamsQuery 20mb InferQueryQuery 19mb TraitSolveQuery (deps) 19mb AttrsQuery 18mb BlockDefMapQuery 17mb ParseQuery 16mb TypeAliasDataQuery 13mb MacroDefQuery 13mb GenericParamsQuery (purge) 12mb FunctionDataQuery 9mb TraitImplsInDepsQuery 8mb ImplTraitQuery (purge) 7mb InternFunctionQuery 7mb ParseMacroExpansionQuery 7mb ImplTraitQuery 7mb AstIdMapQuery 7mb AttrsQuery (purge) 7mb ParseMacroExpansionQuery (purge) 7mb ImplSelfTyQuery (purge) 7mb ImplDataQuery (purge) 7mb InternMacroQuery (purge) 6mb AstIdMapQuery (purge) 6mb MacroExpandQuery (purge) 6mb FileItemTreeQuery (purge) 6mb ImplDatumQuery (purge) 6mb InternFunctionQuery (purge) 5mb MacroArgTextQuery (purge) 5mb InferQueryQuery (deps) 5mb InternMacroQuery 5mb ImplDataQuery (deps) 4mb ImportMapQuery 4mb TyQuery 4mb GenericPredicatesQuery (purge) 3884kb ImplTraitQuery (deps) 3792kb ImplSelfTyQuery (deps) 3570kb MacroExpandQuery 3390kb InternImplQuery 3253kb FileItemTreeQuery (deps) 3136kb InternImplQuery (purge) 3092kb ImplDatumQuery (deps) 2912kb GenericParamsQuery (deps) 2628kb CrateGraphQuery (purge) 2558kb GenericPredicatesQuery 2166kb TyQuery (purge) 2136kb LineIndexQuery 2098kb ParseMacroExpansionQuery (deps) 2080kb HygieneFrameQuery (purge) 2059kb AssociatedTyValueQuery 2025kb BlockDefMapQuery (purge) 2020kb GenericDefaultsQuery (purge) 1863kb TraitImplsInCrateQuery 1696kb InternTypeAliasQuery (purge) 1635kb InternTypeAliasQuery 1629kb MacroExpandQuery (deps) 1462kb MacroArgTextQuery (deps) 1317kb GenericPredicatesQuery (deps) 1309kb TraitSolveQuery 1210kb AttrsQuery (deps) 1149kb CallableItemSignatureQuery 1143kb FunctionDataQuery (purge) 1111kb FnDefDatumQuery 1059kb InternConstQuery 1047kb AstIdMapQuery (deps) 1039kb HygieneFrameQuery (deps) 1038kb BlockDefMapQuery (deps) 979kb BodyWithSourceMapQuery (deps) 961kb FileSymbolsQuery 911kb SourceRootQuery (purge) 903kb TypeAliasDataQuery (purge) 848kb InternConstQuery (purge) 845kb CrateDefMapQueryQuery (deps) 831kb BodyQuery (purge) 829kb BodyWithSourceMapQuery (purge) 790kb InferQueryQuery (purge) 788kb ExprScopesQuery (purge) 785kb TyQuery (deps) 760kb AssociatedTyValueQuery (purge) 702kb GenericDefaultsQuery 537kb ImplSelfTyQuery 527kb CallableItemSignatureQuery (purge) 517kb ValueTyQuery (purge) 481kb GenericDefaultsQuery (deps) 481kb InternStructQuery 421kb FunctionDataQuery (deps) 398kb TraitImplsInCrateQuery (deps) 397kb StructDataQuery 392kb InternStructQuery (purge) 385kb EnumDataQuery 354kb ParseQuery (purge) 317kb ValueTyQuery 274kb FileSourceRootQuery (purge) 274kb CallableItemSignatureQuery (deps) 265kb AssociatedTyValueQuery (deps) 253kb TypeAliasDataQuery (deps) 238kb GenericPredicatesForParamQuery (purge) 216kb GenericPredicatesForParamQuery 210kb InternEagerExpansionQuery (purge) 206kb TraitDataQuery 201kb InternTypeParamIdQuery 194kb MacroDefQuery (purge) 154kb ValueTyQuery (deps) 154kb BodyQuery (deps) 153kb FieldTypesQuery 151kb InternCallableDefQuery (purge) 150kb InternTypeParamIdQuery (purge) 148kb StructDatumQuery 139kb ExprScopesQuery (deps) 139kb FnDefDatumQuery (purge) 133kb InternCallableDefQuery 133kb TraitDatumQuery 130kb StructDatumQuery (purge) 118kb InherentImplsInCrateQuery (deps) 114kb CrateLangItemsQuery (deps) 98kb InternEnumQuery (purge) 98kb InternTraitQuery (purge) 96kb LineIndexQuery (purge) 90kb GenericPredicatesForParamQuery (deps) 89kb InherentImplsInCrateQuery 88kb TraitImplsInCrateQuery (purge) 88kb CrateDefMapQueryQuery (purge) 86kb InternEnumQuery 83kb InternTraitQuery 65kb TraitDatumQuery (purge) 64kb InternStaticQuery 62kb FnDefDatumQuery (deps) 60kb TraitDataQuery (purge) 58kb ParseQuery (deps) 54kb InternClosureQuery 53kb InternClosureQuery (purge) 53kb FieldTypesQuery (purge) 49kb InternStaticQuery (purge) 46kb StructDataQuery (purge) 44kb SourceRootCratesQuery (deps) 40kb LangItemQuery (purge) 37kb TraitDataQuery (deps) 37kb ReturnTypeImplTraitsQuery 37kb MacroDefQuery (deps) 36kb ConstDataQuery 31kb StructDatumQuery (deps) 29kb EnumDataQuery (purge) 28kb FieldTypesQuery (deps) 26kb ConstDataQuery (purge) 24kb TraitDatumQuery (deps) 17kb StaticDataQuery 16kb LineIndexQuery (deps) 13kb FileSymbolsQuery (purge) 12kb AssociatedTyDataQuery 11kb StructDataQuery (deps) 8kb ImportMapQuery (purge) 8kb EnumDataQuery (deps) 7kb LangItemQuery (deps) 7kb StaticDataQuery (purge) 7kb ReturnTypeImplTraitsQuery (purge) 7kb ConstDataQuery (deps) 6kb ReturnTypeImplTraitsQuery (deps) 5kb AssociatedTyDataQuery (purge) 5kb CrateLangItemsQuery 5kb ImportMapQuery (deps) 4kb TraitImplsInDepsQuery (deps) 3712b InherentImplsInCrateQuery (purge) 3360b AssociatedTyDataQuery (deps) 3328b InternImplTraitIdQuery 2832b InternImplTraitIdQuery (purge) 2688b UnionDataQuery 2496b FileSymbolsQuery (deps) 2208b StaticDataQuery (deps) 2064b SourceRootCratesQuery (purge) 2064b CrateLangItemsQuery (purge) 1808b TraitImplsInDepsQuery (purge) 1360b SourceRootCratesQuery 960b InternUnionQuery 928b LibraryRootsQuery (purge) 832b InternUnionQuery (purge) 400b UnionDataQuery (purge) 304b LocalRootsQuery (purge) 96b UnionDataQuery (deps) 0b CrateGraphQuery 0b CrateGraphQuery (deps) 0b FileTextQuery 0b FileTextQuery (deps) 0b FileSourceRootQuery 0b FileSourceRootQuery (deps) 0b SourceRootQuery 0b SourceRootQuery (deps) 0b LangItemQuery 0b AssociatedTyDataQuery 0b AssociatedTyDataQuery (deps) 0b AssociatedTyDataQuery (purge) 0b InternCallableDefQuery (deps) 0b InternTypeParamIdQuery (deps) 0b InternImplTraitIdQuery (deps) 0b InternClosureQuery (deps) 0b LibrarySymbolsQuery 0b LibrarySymbolsQuery (deps) 0b LibrarySymbolsQuery (purge) 0b LocalRootsQuery 0b LocalRootsQuery (deps) 0b LibraryRootsQuery 0b LibraryRootsQuery (deps) 0b InternMacroQuery (deps) 0b InternEagerExpansionQuery (deps) 0b InternFunctionQuery (deps) 0b InternStructQuery (deps) 0b InternUnionQuery (deps) 0b InternEnumQuery (deps) 0b InternConstQuery (deps) 0b InternStaticQuery (deps) 0b InternTraitQuery (deps) 0b InternTypeAliasQuery (deps) 0b InternImplQuery (deps) 0b InternTypeParamIdQuery 0b InternTypeParamIdQuery (deps) 0b InternTypeParamIdQuery (purge) -4090mb ExprScopesQuery (note: database has been cleared) ```
isqad commented 2 years ago

I can confirm the issue:

image

After few seconds memory usage is ~1.8Gb in little project with tokio dependency

lnicola commented 2 years ago

@isqad how much memory does rust-analyzer analysis-stats . use if you run it from your project directory?

Is your code public?

flodiebold commented 2 years ago

Tokio is a big dependency; it doesn't matter how small the project is. While 1.8G is more than we'd like it to use, it doesn't indicate that there's a memory leak.

isqad commented 2 years ago

While 1.8G is more than we'd like it to use, it doesn't indicate that there's a memory leak.

Yes, it is. Sorry for mistake, memory is not increase more than 1.8Gb

abergmeier commented 11 months ago

I have a (near empty) project in combination with Visual Studio Code + Flatpak Extension, where rust-analyzer leaks/takes all available memory (16GB). rust-analyzer has normal memory usage, when run without Flatpak Extension.

At ~4GB I ran Rust Analyzer: Memory Usage - maybe it helps.

Per-query memory usage:
    96mb  11427 HygieneFrameQuery
    92mb  30013 FileItemTreeQuery
    60mb     70 CrateDefMapQueryQuery
    51mb  27903 MacroArgNodeQuery
    46mb 165514 AttrsQuery
    45mb   3629 FileTextQuery
    35mb  68659 ImplDataWithDiagnosticsQuery
    25mb   2139 ParseQuery
    24mb  30030 AstIdMapQuery
    18mb 119917 InternFunctionQuery
    17mb  27907 MacroExpandQuery
    15mb  27907 ParseMacroExpansionQuery
    15mb  68658 ImplTraitQuery
    15mb  68658 ImplSelfTyQuery
    14mb  68659 ImplDataQuery
     9mb  68659 InternImplQuery
     9mb    414 DeclMacroExpanderQuery
     9mb  29928 InternMacroCallQuery
  2930kb  19004 InternTypeAliasQuery
  2404kb  14558 InternConstQuery
  1895kb   3992 FunctionDataQuery
  1044kb     35 ExpandProcMacroQuery
   957kb   1109 TraitDataWithDiagnosticsQuery
   629kb      1 CrateGraphQuery
   586kb    325 EnumDataWithDiagnosticsQuery
   548kb    219 BlockDefMapQuery
   535kb   5071 InternStructQuery
   377kb      1 ProgramClausesForChalkEnvQuery
   338kb    236 BodyWithSourceMapQuery
   270kb    686 InternMacroRulesQuery
   227kb      0 UnionDataWithDiagnosticsQuery
   202kb   1109 TraitDataQuery
   157kb    236 BodyQuery
   141kb     67 ImplDatumQuery
   117kb    612 InternEnumQuery
   113kb      3 ConstDataQuery
   113kb    386 TypeAliasDataQuery
   108kb   1460 InternTraitQuery
   101kb    219 InternBlockQuery
    96kb      1 FnDefVarianceQuery
    85kb     90 InternExternBlockQuery
    75kb      2 AssociatedTyValueQuery
    68kb     57 TraitSolveQueryQuery
    63kb     19 InternUnionQuery
    47kb    264 InternStaticQuery
    44kb      1 FnDefDatumQuery
    43kb      3 ExprScopesQuery
    41kb      0 Macro2DataQuery
    37kb    325 EnumDataQuery
    32kb      1 LibraryRootsQuery
    24kb      1 ProcMacrosQuery
    10kb      0 ModuleSymbolsQuery
   2512b      0 MacroRulesDataQuery
  -2944b     40 InternMacro2Query
    -4kb      1 LocalRootsQuery
    -9kb      0 StaticDataQuery
   -18kb      0 TraitAliasDataQuery
   -37kb      0 LibrarySymbolsQuery
   -54kb      6 StructDatumQuery
   -59kb      1 AssociatedTyDataQuery
   -63kb      1 LineIndexQuery
  -102kb      5 AdtVarianceQuery
  -107kb      0 ProcMacroDataQuery
  -159kb      1 InternTraitAliasQuery
  -173kb      8 StructDataWithDiagnosticsQuery
  -186kb      0 UnionDataQuery
  -265kb     27 InternProcMacroQuery
  -399kb     24 TraitDatumQuery
  -805kb      0 InternGeneratorQuery
 -2314kb     11 CrateLangItemsQuery
 -2551kb   6775 GenericDefaultsQuery
 -2554kb   5435 TyQuery
 -3101kb      3 FieldTypesQuery
 -3196kb      0 LayoutOfAdtQuery
 -3252kb      0 InherentImplsInBlockQuery
 -3300kb     22 CallableItemSignatureQuery
 -3431kb     12 FunctionVisibilityQuery
 -3641kb      0 BorrowckQuery
 -3668kb     70 GenericPredicatesForParamQuery
 -3871kb      2 ReturnTypeImplTraitsQuery
 -3911kb      0 ConstEvalQuery
 -4038kb    113 GenericPredicatesQuery
    -4mb      1 TraitImplsInDepsQuery
    -4mb      4 TargetDataLayoutQuery
    -4mb      0 MirBodyQuery
    -4mb      0 ConstEvalDiscriminantQuery
    -4mb      0 ImportMapQuery
    -4mb     11 InternCallableDefQuery
    -4mb     21 LangItemQuery
    -4mb      6 TraitEnvironmentQuery
    -5mb     70 TraitImplsInCrateQuery
    -5mb    584 ConstParamTyQuery
    -5mb      3 InferQueryQuery
    -5mb      1 ConstVisibilityQuery
    -5mb     12 ValueTyQuery
    -5mb      0 InternClosureQuery
    -6mb      0 InternImplTraitIdQuery
    -6mb      0 TraitImplsInBlockQuery
    -6mb      2 SourceRootCratesQuery
    -6mb      0 IncoherentInherentImplCratesQuery
    -6mb      0 FieldsAttrsSourceMapQuery
    -6mb      3 InternTypeOrConstParamIdQuery
    -7mb   3629 FileSourceRootQuery
    -8mb      0 InternLifetimeParamIdQuery
    -8mb  75483 GenericParamsQuery
    -9mb      0 VariantsAttrsSourceMapQuery
    -9mb      1 InherentImplsInCrateQuery
   -11mb      0 CrateSupportsNoStdQuery
   -12mb     74 SourceRootQuery
   -13mb      0 FieldsAttrsQuery
   -19mb      0 FieldVisibilitiesQuery
   -23mb      8 StructDataQuery
   -39mb    301 VariantsAttrsQuery
  3957mb        Remaining

(note: database has been cleared)