Skip to content

perf: release timeline memory when navigating between rooms#2949

Draft
9clg6 wants to merge 1 commit intomainfrom
perf/timeline-memory-leaks
Draft

perf: release timeline memory when navigating between rooms#2949
9clg6 wants to merge 1 commit intomainfrom
perf/timeline-memory-leaks

Conversation

@9clg6
Copy link
Collaborator

@9clg6 9clg6 commented Mar 20, 2026

Summary

Fix memory leaks and unbounded growth in ChatController that cause increasing memory pressure during normal navigation between rooms.

Changes

  • Fix timeline subscription leaks: timeline!.cancelSubscriptions() is now called BEFORE timeline = null in scrollDown(), scrollToEventId() and _reloadTimelineAndWaitForRender(). Previously, nulling the reference first left 5 orphaned stream subscriptions per timeline reload.
  • Add _trimTimelineIfNeeded(): Caps timeline events at 500 per room after each requestHistory(). Prevents unbounded memory growth during long scroll sessions. Trimmed events remain in the database and are re-fetched if the user scrolls back.
  • Clear Flutter image cache on dispose(): Calls PaintingBinding.instance.imageCache.clear() and clearLiveImages() when leaving a room to free decoded GPU/raster bitmaps.
  • Fix timer leak: Cancel _timestampTimer in dispose().

Performance measurements

Measured with Flutter Web --profile, CPU throttling 4x, Chrome DevTools trace, ~75s navigation (DMs, groups 51-85 members, contacts, settings, scrolling, member lists). Fresh Chrome instance.

Metric main timeline-leaks Delta
JS Heap delta +73.39 MB +21.24 MB -71.1%
JS Heap end 170.94 MB 117.46 MB -31.3%
Long tasks (>50ms) 51 34 -33.3%
GC events 5,538 1,669 -69.9%
GC total time 1,702 ms 923 ms -45.8%
GC max pause 22.4 ms 16.0 ms -28.6%

Raw trace file attached: perf-trace-timeline-leaks.json.gz (load in Chrome DevTools > Performance > Load profile to verify).

Test recommendation

  1. Open a room with many messages, scroll up extensively to load history
  2. Navigate to another room, repeat
  3. Monitor memory in DevTools — should stay bounded instead of growing linearly
  4. Verify that scrolling back to old messages still loads them from the database

Ticket

No ticket — discovered during performance profiling.

- Fix timeline subscription leaks in scrollDown(), scrollToEventId()
  and _reloadTimelineAndWaitForRender() where timeline was set to null
  BEFORE cancelSubscriptions() was called, leaving 5 orphaned stream
  subscriptions per reload
- Add _trimTimelineIfNeeded() to cap events at 500 per timeline,
  trimming oldest events after each requestHistory() to prevent
  unbounded memory growth during long scroll sessions
- Clear Flutter image cache (decoded bitmaps) on ChatController.dispose()
  to free GPU/raster memory when leaving a room
@coderabbitai
Copy link

coderabbitai bot commented Mar 20, 2026

Warning

Rate limit exceeded

@9clg6 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 13 minutes and 15 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 87f43054-9c12-47f1-9cff-fe54a6ef1463

📥 Commits

Reviewing files that changed from the base of the PR and between 844b35b and fa4e9b7.

📒 Files selected for processing (1)
  • lib/pages/chat/chat.dart
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/timeline-memory-leaks
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@9clg6 9clg6 marked this pull request as draft March 20, 2026 12:56
@github-actions
Copy link
Contributor

This PR has been deployed to https://linagora.github.io/twake-on-matrix/2949

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant