Dart Analyzer's Memory Bugs: Double Parsing, Cache Invalidation, and a 11.6 GB Monorepo Isolate
theme: smartblue
Many people no longer open Android Studio, but they still find their computer's memory eaten up? There's a good chance Dart Analyzer is a major contributor.
Recently I've been following several Dart Analyzer Issues, and it feels like the Analyzer is indeed painful in large Flutter projects. Especially now with AI Agents and multiple Git worktrees open, memory is already tight, and the Analyzer adds more. Several bugs stack up:
| Issue | Main problem |
|---|---|
| #64862 | The same Library is parsed twice in one analysis, 665 times becomes 1330 times |
| #64881 | After one API modification, a dependency cycle of 729 Libraries repeatedly re-links, memory goes from 2.7 GB to 4.0 GB |
| #64202 | Plugin lacks persistent cache, causing CI to potentially repeat massive semantic analysis every time |
| #64297 | How SingleFileByteStore reduces cache file I/O overhead |
| #64223 | In a monorepo, plugin incorrectly analyzes the entire workspace, a single isolate's memory reaches 11.6 GB |
| #63244 | Same plugin is AOT-compiled repeatedly under different packages |
| #63846 | Plugin cache conflicts between different Dart SDKs, causing macOS Code Signature Invalid crash |
Why is the same Library counted twice by plugins?
First talk about #64862. Here "Library" doesn't just mean .dart files, e.g.:
// user.dart
library user;
part 'user_model.dart';
part 'user_service.dart';
These three files can make up one Library. The Analyzer needs to process types, symbol references, and the relationships between parts together to get the full semantic result.
The new Analyzer Plugin mechanism introduced in Dart 3.10 lets plugins run in separate isolates, each with its own AnalysisDriver and caches. For example, after enabling Riverpod Lint, the main analysis service checks Dart code, and the plugin also uses its own analysis results to run rules.
The plugin's execution flow is a bit magical:
AnalysisDriver first parses the Library, then sends the ResolvedUnitResult event to the PluginServer. The plugin, to run rules that need whole-Library information, calls:
var libraryResult = await analysisContext.currentSession
.getResolvedLibrary(unitResult.path);
This call is fine because ResolvedUnitResult can represent a compilation unit, and some rules need the complete Library AST and type info.
The problem is that _resolvedLibraryCache at the time only kept results of Libraries containing a priority file (the files the IDE is analyzing first). When CI scans the whole project, most Libraries are not in that scope.
So the Driver just finishes parsing Library A and emits an event, then the plugin immediately asks for A's full result, but the Driver can't find the just-computed result, so it parses again.
For example, in tests with Dart 3.13.7 and analysis_server_plugin 0.3.25, the public Flutter + Riverpod sample had 665 Libraries and analyzeFile was called 1330 times; a private project had 736 Libraries, called 1472 times.
The PR CL 574462 then proposed something interesting:
Add a small cache for non-priority Libraries that keeps only the last 8 full results. It doesn't persist AST long-term, only handles the short window where "just emitted a parse event, plugin immediately fetches result."
In tests, after the change, analyzeFile went from 1330 back to 665 times. Plugin cold run dropped from about 4.4–4.7s to 4.1–4.5s, and warm run with persistent ByteStore went from 1.55s to 1.34s. Doesn't seem like a big speed boost.
But I suspect there's a trap in this fix.
#64881 is even more absurd
#64881 is worse. Even when code hasn't changed, the on-disk analysis cache invalidates. For example, a dependency cycle containing 729 Libraries, after one API modification, every subsequent startup makes Analysis Server re-link and writes a ~10 MB bundle. Even if you revert the change, it doesn't help.
Single-window memory goes from 2.7 GB before the change to 4.0 GB after. Cleaning the cache temporarily restores it, but the next API change triggers it again.
So your memory disappears like this. The Library cycle is a group of Libraries that depend on each other and need to be linked in the same pass. Roughly like:
// dep/lib/kinds.dart
enum Kind { a, b }
// dep/lib/dep.dart
import 'kinds.dart' as kinds;
class Base {
Base.named(this.url, {this.kind = kinds.Kind.a});
final String url;
final kinds.Kind kind;
}
Then in the app, a.dart and b.dart import each other, and b.dart inherits from Base:
// app/lib/a.dart
import 'b.dart';
class A { B? b; }
// app/lib/b.dart
import 'a.dart';
import 'package:dep/dep.dart';
class B extends Base {
B(super.url) : super.named();
A? a;
}
Here the constructor default value kinds.Kind.a references an enum through a prefixed import, and the subclass inherits the parent constructor parameter via super.url. In a real project, this might be triggered by video_player's VideoPlayerController, whose constructor parameters have similar default values like platform_interface.VideoViewType.textureView.
Then using dart language-server's diagnostic status page you can see the link count:
| Operation | link |
libraryFile |
|---|---|---|
| First start with empty cache | 4 | 13 |
| Start again without code changes | 0 | 0 |
Add a public method to a.dart, start again |
1 | 2 |
| Start again without code changes | 1 | 2 |
| Continue without changes, start again | 1 | 2 |
Normally the last two should hit the disk cache like the second one, but both counts are non-zero, meaning it rebuilds every time.
The reason circles back to Analyzer's own cache reading implementation:
When rebuilding the cycle,
depis lazily loaded from an existing bundle, and whenResolutionReader.readElementprocessesElementTag.libraryImportPrefix, it readscurrentLibraryFragment.element.fragments.
The problem is that the fragments getter is recorded by the fine-grained dependency tracking as an opaque API use. That is, when Analyzer deserializes its own saved default value, it gets logged as "external code uses an API that can't be confirmed at fine granularity."
Then the bundle saves this requirement, and at next startup RequirementsManifest.isSatisfied sees opaqueApiUses isn't empty and refuses to reuse. When it re-links, the same read path triggers again, logging again. That's the loop:
First API modification → re-link → read constructor default → wrongly record opaque dependency → save a cache that will definitely be rejected next time → every subsequent startup re-links.
This is truly painful.
There's also a combined version
Then #64202, along with the previous two, forms another complete package. When the plugin Driver uses MemoryCachingByteStore(NullByteStore(), 256 MiB), parsed data can be temporarily stored in memory, but it's gone when the process ends.
The main Analysis Server might have a complete disk cache, so the second dart analyze is fast, but when the plugin isolate starts, it has to re-read files, parse, link, and compute dependency summaries on its own.
For example, with the plugin enabled,
dart analyze --fatal-infostakes about 15 seconds; without the plugin, using existing cache, only 2.4 seconds.
And of that ~15 seconds, reading and parsing 6697 files took 4.3 seconds, linking 3359 Library cycles took 5.4 seconds, computing fine-grained dependencies took 2.6 seconds, and actually executing Riverpod rules only took 0.1 seconds.
So, you think Riverpod Lint is heavy, but the rules themselves are a tiny part; the bulk is the plugin redundantly preparing the entire semantic analysis environment.
Monorepo amplifies the problem
Then there's the advanced version. In a large Flutter Monorepo, the issue gets amplified further. #64223 has a scarier scenario:
In Dart 3.13.2's LSP, the entire workspace is handed to the plugin as the analysis root, causing plugins configured only for a small package to analyze the entire Monorepo.
For example, the project has 14,000 Dart files and 45 analysis contexts. When the same plugin isolate analyzes only a small package, it uses 0.19 GB; when opening the whole repo root, it jumps to 11.6 GB. Oh my, isn't that describing me?!!!!!
With two plugins enabled, the whole process uses 34 GB; without plugins, 10.3 GB. It seems fixed now.
Additionally on macOS, when two projects using different Dart SDKs are open simultaneously, two Analysis Servers may read and write the same plugin plugin.aot cache file. One process's mapped file gets overwritten by the other, and the system SIGKILLs it with Code Signature Invalid. That was also a nasty bug.
Finally
Looking at these Issues together, the Analyzer seems to be a mess possibly written by AI, with too many edge cases. Even 32 GB of memory isn't enough, and it keeps swapping to disk until the system freezes.
Top 1 from juejin.cn, machine-translated. The original thread is authoritative.
Like