ProfiledTest.kt
TLDR
The ProfiledTest.kt
file in the LeakCanary project is a Kotlin file containing a single class named ProfiledTest
. It provides a single method, analyzeLargeDump
, which is marked as @Ignore
. This method performs a profile analysis on a large heap dump by calling the profileAnalysis
method.
Methods
analyzeLargeDump
This method is marked with @Ignore
, which means it will not be executed during test runs. It performs a profile analysis on a large heap dump by calling the profileAnalysis
method and passing the name of the heap dump file as a parameter.
profileAnalysis
This private method is responsible for performing a profile analysis on a given heap dump file. It first retrieves the application context using the InstrumentationRegistry
from the AndroidX Test library. It then creates a heapDumpFile
by copying the given heap dump file from the assets directory to the application's files directory.
The method then calls runWithProfilerSampling
to execute the profile analysis within a profiler sampling block. Within this block, it creates a HeapAnalyzer
and analyzes the heap dump using various parameters, such as the leakingObjectFinder
, referenceMatchers
, objectInspectors
, and computeRetainedHeapSize
. Finally, it prints the result of the analysis to the log.
Classes
No classes are defined in this file.
package leakcanary
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import java.io.File
import leakcanary.Profiler.runWithProfilerSampling
import org.junit.Ignore
import org.junit.Test
import shark.AndroidObjectInspectors
import shark.AndroidReferenceMatchers
import shark.HeapAnalyzer
import shark.KeyedWeakReferenceFinder
import shark.SharkLog
class ProfiledTest {
@Ignore
@Test fun analyzeLargeDump() {
profileAnalysis("large-dump.hprof")
}
private fun profileAnalysis(fileName: String) {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val context = instrumentation.targetContext
val heapDumpFile = File(context.filesDir, "ProfiledTest.hprof")
context.assets.open(fileName)
.copyTo(heapDumpFile.outputStream())
runWithProfilerSampling {
val analyzer = HeapAnalyzer { step -> Log.d("LeakCanary", step.name) }
val result = analyzer.analyze(
heapDumpFile = heapDumpFile,
leakingObjectFinder = KeyedWeakReferenceFinder,
referenceMatchers = AndroidReferenceMatchers.appDefaults,
objectInspectors = AndroidObjectInspectors.appDefaults,
computeRetainedHeapSize = true
)
SharkLog.d { result.toString() }
}
}
}