Skip to content

fix(android): cap ANR stack traces to reduce OOM risk#207

Draft
robert-northmind wants to merge 2 commits intomainfrom
cursor/anr-tracker-stack-cap-3cb1
Draft

fix(android): cap ANR stack traces to reduce OOM risk#207
robert-northmind wants to merge 2 commits intomainfrom
cursor/anr-tracker-stack-cap-3cb1

Conversation

@robert-northmind
Copy link
Copy Markdown
Collaborator

@robert-northmind robert-northmind commented Apr 10, 2026

Description

This change bounds how much of the main thread stack we stringify when the in-app ANR watchdog fires. Deep Flutter stacks can be very large; building and logging unbounded strings on low-memory devices contributed to OutOfMemoryError during ANR capture (see #174).

  • Cap at 128 frames and 64 KiB for the stored stack trace string.
  • Add JSON fields: stacktrace_truncated, stacktrace_total_frames, stacktrace_included_frames.
  • Replace full-stack Log.w with a short summary (included/total frames, truncated flag).
  • Extract AnrStackTraceFormatter (pure Java) with JUnit tests in android/anr-stack-formatter-tests; CI runs ./gradlew :anr-stack-formatter-tests:test. The formatter guarantees the returned string length never exceeds maxChars (including truncation suffix).

Related Issue(s)

Fixes #174

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation
  • Performance improvement
  • Code refactoring
  • Chore / Housekeeping

Checklist

  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the CHANGELOG.md under the "Unreleased" section

Additional Notes

TDD-style: tests were added for max frame count, max character budget, and global length bound; a follow-up fix ensures the formatted string is hard-capped at maxChars when the truncation note would otherwise exceed the limit.

dart tool/pre_release_check.dart passes locally.

Open in Web Open in Cursor 

Limit main-thread stack string to 128 frames and 64 KiB, add JSON
metadata for truncation, and log a short summary instead of the full
trace. Addresses #174.

Co-authored-by: Robert Magnusson <robert-northmind@users.noreply.github.com>
@cla-assistant
Copy link
Copy Markdown

cla-assistant bot commented Apr 10, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Extract AnrStackTraceFormatter for pure-Java unit tests and a Gradle
subproject that compiles only that file. CI runs
:anr-stack-formatter-tests:test. Enforce final string length <= maxChars
so tiny limits cannot exceed the cap.

Co-authored-by: Robert Magnusson <robert-northmind@users.noreply.github.com>
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.

[BUG] OOM in ANRTracker

2 participants