Skip to content

perf: Schedule session end on shared executor (JAVA-653)#5819

Merged
runningcode merged 2 commits into
mainfrom
no/perf/lifecycle-watcher-timer-executor
Jul 22, 2026
Merged

perf: Schedule session end on shared executor (JAVA-653)#5819
runningcode merged 2 commits into
mainfrom
no/perf/lifecycle-watcher-timer-executor

Conversation

@runningcode

Copy link
Copy Markdown
Contributor

📜 Description

LifecycleWatcher created a java.util.Timer (a dedicated thread) on the first app-background transition, and that thread lived for the rest of the process. The session-end task now runs on the shared timer executor (SentryOptions#getTimerExecutorService), already used for transaction timeouts, whose single worker thread is reused and self-terminates when idle.

The task is scheduled/cancelled as the app moves between background and foreground via Future#cancel(false) (the same pattern SentryTracer uses for idle/deadline timeouts). If scheduling fails (executor already shut down), the session is ended immediately instead of being left open.

💡 Motivation and Context

Part of reducing the number of threads created by the SDK: JAVA-653.

On Android this timer thread was created on the first background transition and never went away. Reusing the shared timer executor removes it.

💚 How did you test it?

Existing LifecycleWatcherTest (updated to assert on the scheduled Future instead of the removed Timer/TimerTask internals) and SessionTrackingIntegrationTest. Both fixtures now wire a real executor into options so scheduled tasks run.

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Related PRs in this effort: RateLimiter (#5814), performance collector (#5816), HostnameCache (#5817), batch processors (#5818).

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Jul 22, 2026

Copy link
Copy Markdown

JAVA-653

@sentry

sentry Bot commented Jul 22, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.50.0 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 312.46 ms 362.45 ms 49.99 ms
Size 0 B 0 B 0 B

Baseline results on branch: main

Startup times

Revision Plain With Sentry Diff
eb95ded 317.51 ms 369.08 ms 51.57 ms
2124a46 319.19 ms 415.04 ms 95.85 ms
c3ee041 310.64 ms 361.90 ms 51.26 ms
dcc6bbf 382.58 ms 462.13 ms 79.54 ms
7a19fee 315.46 ms 368.62 ms 53.16 ms
9054d65 330.94 ms 403.24 ms 72.30 ms
d500866 326.13 ms 378.70 ms 52.58 ms
ad8da22 365.86 ms 427.00 ms 61.14 ms
ed33deb 337.52 ms 484.06 ms 146.54 ms
d15471f 310.26 ms 377.04 ms 66.78 ms

App size

Revision Plain With Sentry Diff
eb95ded 0 B 0 B 0 B
2124a46 1.58 MiB 2.12 MiB 551.51 KiB
c3ee041 0 B 0 B 0 B
dcc6bbf 1.58 MiB 2.12 MiB 553.10 KiB
7a19fee 0 B 0 B 0 B
9054d65 1.58 MiB 2.29 MiB 723.38 KiB
d500866 0 B 0 B 0 B
ad8da22 1.58 MiB 2.29 MiB 719.83 KiB
ed33deb 1.58 MiB 2.13 MiB 559.52 KiB
d15471f 1.58 MiB 2.13 MiB 559.54 KiB

Previous results on branch: no/perf/lifecycle-watcher-timer-executor

Startup times

Revision Plain With Sentry Diff
bb75e5a 322.13 ms 363.14 ms 41.01 ms

App size

Revision Plain With Sentry Diff
bb75e5a 0 B 0 B 0 B

@runningcode
runningcode marked this pull request as ready for review July 22, 2026 14:15

@romtsn romtsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

runningcode and others added 2 commits July 22, 2026 17:52
LifecycleWatcher created a java.util.Timer whose thread was spawned on
the first background transition and lived for the rest of the process.
Schedule the session-end task on the shared timer executor instead,
whose single worker thread is reused and self-terminates when idle. If
scheduling fails (executor already shut down), the session is ended
right away instead of leaking.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@runningcode
runningcode force-pushed the no/perf/lifecycle-watcher-timer-executor branch from c27cd5e to 0db0e3d Compare July 22, 2026 15:53
@runningcode
runningcode enabled auto-merge (squash) July 22, 2026 15:57
@runningcode
runningcode merged commit 023ef00 into main Jul 22, 2026
68 of 72 checks passed
@runningcode
runningcode deleted the no/perf/lifecycle-watcher-timer-executor branch July 22, 2026 16:00
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.

2 participants