Skip to content

fix: session replay memory leak#345

Draft
marandaneto wants to merge 1 commit intomainfrom
fix/session-replay-memory-leak
Draft

fix: session replay memory leak#345
marandaneto wants to merge 1 commit intomainfrom
fix/session-replay-memory-leak

Conversation

@marandaneto
Copy link
Copy Markdown
Member

💡 Motivation and Context

Fixes #142

Session replay on Flutter has multiple memory leak vectors:

  1. Overlapping snapshot captures: _generateSnapshot() was fire-and-forget (async but not awaited), so if a capture took longer than the 1s throttle, multiple captures would run concurrently — each allocating large ui.Image objects, raw RGBA byte arrays (~8MB each), PictureRecorders, etc.
  2. Full raw RGBA bytes stored permanently for diffing: Each captured frame stored a full raw RGBA Uint8List (~8MB for a 1080×1920 screen) in ViewTreeSnapshotStatus.imageBytes just to compare with the next frame.
  3. Native resources leaked on errors: The catchError handler didn't dispose ui.Image, Picture, or PictureRecorder objects when exceptions occurred mid-processing.
  4. Per-frame callbacks: ChangeDetector registered an addPostFrameCallback on every single frame (~60/s), even though all but ~1/s were immediately rejected by the throttle.
  5. No cancellation of in-flight work: Stopping recording didn't cancel ongoing captures, which continued allocating and processing images.

💚 How did you test it?

  • All 93 existing unit tests pass
  • flutter analyze reports no issues
  • Manual verification of code paths for resource disposal

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file
  • Added the release label to the PR

@marandaneto marandaneto requested a review from a team as a code owner March 26, 2026 17:08
@marandaneto marandaneto marked this pull request as draft March 26, 2026 17:09
@marandaneto
Copy link
Copy Markdown
Member Author

didnt test this yet but this is what claude thinks about the linked issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak with Flutter Session Replay

1 participant