BackgroundThreadHeapAnalyzer.kt
TLDR
The file BackgroundThreadHeapAnalyzer.kt
contains an object BackgroundThreadHeapAnalyzer
that implements the EventListener
interface. It starts heap analysis on a background HandlerThread
when receiving a HeapDump
event.
Classes
BackgroundThreadHeapAnalyzer
This class is an object that implements the EventListener
interface. It starts heap analysis on a background HandlerThread
when receiving a HeapDump
event. It uses a lazy-initialized heapAnalyzerThreadHandler
property to create a Handler
on the HandlerThread
for performing the analysis. The onEvent
method is implemented to trigger the heap analysis on the background thread when a HeapDump
event is received. After the analysis finishes, it sends the analyzed HeapDump
event and a doneEvent
event to InternalLeakCanary
.
package leakcanary
import android.os.Handler
import android.os.HandlerThread
import leakcanary.EventListener.Event
import leakcanary.EventListener.Event.HeapDump
import leakcanary.internal.AndroidDebugHeapAnalyzer
import leakcanary.internal.InternalLeakCanary
/**
* Starts heap analysis on a background [HandlerThread] when receiving a [HeapDump] event.
*/
object BackgroundThreadHeapAnalyzer : EventListener {
internal val heapAnalyzerThreadHandler by lazy {
val handlerThread = HandlerThread("HeapAnalyzer")
handlerThread.start()
Handler(handlerThread.looper)
}
override fun onEvent(event: Event) {
if (event is HeapDump) {
heapAnalyzerThreadHandler.post {
val doneEvent = AndroidDebugHeapAnalyzer.runAnalysisBlocking(event) { event ->
InternalLeakCanary.sendEvent(event)
}
InternalLeakCanary.sendEvent(doneEvent)
}
}
}
}