Skip to content

Commit

Permalink
[NR-102311] Memory issue fix (#46)
Browse files Browse the repository at this point in the history
1. Turn on ANRMonitor
2. Move waitablerunner outside the loop
3. Add logs
  • Loading branch information
ywang-nr authored Sep 25, 2023
1 parent 3c4be8f commit 42342b4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ open class ANRMonitor {
val anrMonitorRunner = Runnable {
monitorThread.start()

val runner = WaitableRunner()
while (!Thread.interrupted()) {
try {
val runner = WaitableRunner()

synchronized(runner) {
synchronized(runner) {
try {
if (!handler.post(runner)) {
AgentNDK.log.debug("ANR monitor runner returns false")
// post returns false on failure, usually because the
// looper processing the message queue is exiting
return@Runnable
Expand All @@ -48,20 +49,23 @@ open class ANRMonitor {
runner.wait(ANR_TIMEOUT)

if (!runner.signaled) {
notify()
AgentNDK.log.debug("ANR monitor signaled false, ANR detected")
createANRReport()
runner.wait()
}

} catch (e: InterruptedException) {
AgentNDK.log.error("ANR monitor exception caught: " + e.message)
} finally {
runner.signaled = false
runner.notifyAll()
Thread.yield()
}
} catch (e: InterruptedException) {
// e.printStackTrace()
}
}
monitorThread.quitSafely()
}

fun notify(anrAsJson: String? = null) {
fun createANRReport(anrAsJson: String? = null) {
StatsEngine.get()
.inc(AgentNDK.Companion.MetricNames.SUPPORTABILITY_ANR_DETECTED)

Expand Down Expand Up @@ -152,6 +156,7 @@ open class ANRMonitor {
exception.printStackTrace()
}
}

else -> {
handler.postDelayed(this, 50)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class JVMDelegate() {
AgentNDK.log.debug("onApplicationNotResponding: $anrAsString")

if (ANRMonitor.getInstance().isRunning()) {
ANRMonitor.getInstance().notify(anrAsString)
ANRMonitor.getInstance().createANRReport(anrAsString)

} else {
AgentNDK.getInstance().managedContext?.nativeReportListener?.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ManagedContext(val context: Context? = null) {
var buildId: String? = null
var reportsDir: File? = getNativeReportsDir(context?.cacheDir)
var nativeReportListener: AgentNDKListener? = null
var anrMonitor: Boolean = false
var anrMonitor: Boolean = true
var expirationPeriod = DEFAULT_TTL

fun getNativeReportsDir(rootDir: File?): File {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ManagedContextTest : TestCase(), AgentNDKListener {

@Test
fun testGetANRMonitor() {
Assert.assertTrue(managedContext?.anrMonitor == false)
Assert.assertTrue(managedContext?.anrMonitor == true)
}

@Test
Expand Down

0 comments on commit 42342b4

Please sign in to comment.