Skip to content

Commit

Permalink
change the AnrDetector executor frequency from scheduleAtFixedRate to…
Browse files Browse the repository at this point in the history
… scheduleWithFixedDelay

Summary:
As title.

This is a change requested by Google. Currently, we are utilizing `ExecutorService.scheduleAtFixedRate` to schedule a ANR check task. However, this approach encounters issues with newer Android versions that restrict or freeze the app. A recommended alternative is to use `scheduleWithFixedDelay`, which ensures the libary won't make up for the missed period of time, and keep the system_server healthy.

----
> Newer versions of Android use the Cached App Freezer feature (see https://source.android.com/docs/core/perf/cached-apps-freezer), which pauses the execution of apps once they're in the cached state.

> scheduleAtFixedRate triggers the passed runnable periodically, which means that if an app is frozen for some time, when it's un-frozen that runnable will be called repeatedly, sometimes many (thousands) of times, depending on the requested interval.

> The behavior we actually want in this case is the behavior of scheduleWithFixedDelay, which triggers the runnable after the previous iteration has terminated. In this case, after an app is frozen for a period, we will still call the runnable upon returning from being frozen and at the desired cadence thereafter, but won't try to "make up for" the missed period of time.

Reviewed By: wx0165927473

Differential Revision: D47309581

fbshipit-source-id: 5bb44275e0376088b626bf1a03d4d7864b704c68
  • Loading branch information
Barney Huang authored and facebook-github-bot committed Jul 13, 2023
1 parent ee1476d commit 1ad4bd9
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ANRDetector {
@JvmStatic
@VisibleForTesting
fun start() {
scheduledExecutorService.scheduleAtFixedRate(
scheduledExecutorService.scheduleWithFixedDelay(
anrDetectorRunnable, 0, DETECTION_INTERVAL_IN_MS.toLong(), TimeUnit.MILLISECONDS)
}
}

0 comments on commit 1ad4bd9

Please sign in to comment.