From 5e08794bbfc2c57b5b0522c6e2b22b419f6aa115 Mon Sep 17 00:00:00 2001 From: Jens Johansen Date: Fri, 16 Jun 2023 06:42:41 +0000 Subject: [PATCH] [analyzer] Plug "temporary leak" when starting new rebuilds before `afterContextsCreated` is called If again and again editing, say, a `pubspec.yaml` with proper timing there is a "temporary leak" that repairs itself once the analyzer finishes (some time after the editing stops). What happens is that old contexts are saved in the `declarationsTracker` but eventually cleared in the `afterContextsCreated` call. In a test on Windows (on Linux I'd currently run into https://dartbug.com/52703) I opened the entire "pkg" folder and edited a `pubspec.yaml` file every 5 seconds. The analyzer went from using something along the lines of 700MB of heap to using around 2.5 GB of heap after 25 edits and 17GB (!) of heap shortly before stopping at 250 `pubspec.yaml` edits. After the editing stopped (and clicking the GC button in observatory) the heap usage went down to ~650MB heap used. This fixes the problem by clearing the `declarationsTracker` on the `afterContextsDestroyed` call too. In the same test it stays at around 300-700MB of heap. Possibly related to: https://github.com/dart-lang/sdk/issues/48788 https://github.com/dart-lang/sdk/issues/52447 Change-Id: Ia38cc946a1f36fa8c5b6804f79cbc8dd96c21030 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/309722 Reviewed-by: Brian Wilkerson Reviewed-by: Konstantin Shcheglov Commit-Queue: Jens Johansen --- pkg/analysis_server/lib/src/analysis_server.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/analysis_server/lib/src/analysis_server.dart b/pkg/analysis_server/lib/src/analysis_server.dart index 482f28ed632a..1a3b5d3c3937 100644 --- a/pkg/analysis_server/lib/src/analysis_server.dart +++ b/pkg/analysis_server/lib/src/analysis_server.dart @@ -325,6 +325,10 @@ abstract class AnalysisServer { addContextsToDeclarationsTracker(); } + void afterContextsDestroyed() { + declarationsTracker?.discardContexts(); + } + /// Broadcast a request built from the given [params] to all of the plugins /// that are currently associated with the context root from the given /// [driver]. Return a list containing futures that will complete when each of @@ -762,6 +766,7 @@ abstract class CommonServerContextManagerCallbacks void afterContextsDestroyed() { flushResults(filesToFlush.toList()); filesToFlush.clear(); + analysisServer.afterContextsDestroyed(); } @override