Shark
作者:互联网
1.LeakNodeStatus 三个状态,没用到
internal enum class LeakNodeStatus {
NOT_LEAKING,
LEAKING,
UNKNOWN;
}
2.HeapAnalysisException 封装throwable
class HeapAnalysisException(cause: Throwable) : RuntimeException(cause) {
override fun toString(): String {
val stringWriter = StringWriter()
cause!!.printStackTrace(PrintWriter(stringWriter))
return stringWriter.toString()
}
companion object {
private const val serialVersionUID: Long = -2522323377375290608
}
}
3.AppSingletonInspector App范围内的单例,标记为不泄漏
/**
* Inspector that automatically marks instances of the provided class names as not leaking
* because they're app wide singletons.
* 检查器,自动将提供的类名的实例标记为不泄漏,因为它们是应用程序范围的单例。
*
*/
class AppSingletonInspector(private vararg val singletonClasses: String) : ObjectInspector {
//检查
override fun inspect(
reporter: ObjectReporter//todo ObjectReporter是干哈的
) {
if (reporter.heapObject is HeapInstance) {
reporter.heapObject.instanceClass
.classHierarchy//他自己的类+父类
.forEach { heapClass ->
if (heapClass.name in singletonClasses) {
reporter.notLeakingReasons += "${heapClass.name} is an app singleton"
}
}
}
}
}
4.MetadataExtractor 元数据提取器接口
/**
* Extracts metadata from a hprof to be reported in [HeapAnalysisSuccess.metadata].
*
* This is a functional interface with which you can create a [MetadataExtractor] from a lambda.
* 从 [HeapAnalysisSuccess.metadata] 分析成功的 hprof 中提取元数据。
* 这是一个功能接口,您可以使用它从 lambda 创建 [MetadataExtractor]。
*/
fun interface MetadataExtractor {
fun extractMetadata(graph: HeapGraph): Map<String, String>
companion object {
/**
* A no-op [MetadataExtractor]
*/
val NO_OP = MetadataExtractor { emptyMap() }
/**
* Utility function to create a [MetadataExtractor] from the passed in [block] lambda instead of
* using the anonymous `object : MetadataExtractor` syntax.
*
* Usage:
*
* ```kotlin
* val inspector = MetadataExtractor { graph ->
*
* }
* ```
*/
inline operator fun invoke(crossinline block: (HeapGraph) -> Map<String, String>): MetadataExtractor =
object : MetadataExtractor {
override fun extractMetadata(graph: HeapGraph): Map<String, String> = block(graph)
}
}
}
5.LeakingObjectFinder查找泄漏的对象id们
/**
* Finds the objects that are leaking, for which Shark will compute
* leak traces.
*
* This is a functional interface with which you can create a [LeakingObjectFinder] from a lambda.
* 查找泄漏的对象,Shark 将为其计算泄漏跟踪。
* 这是一个函数式接口,您可以使用它从 lambda 创建 [LeakingObjectFinder]。
*/
fun interface LeakingObjectFinder {
/**
* For a given heap graph, returns a set of object ids for the objects that are leaking.
* 对于给定的堆图,返回一组泄漏对象的对象 ID。
*/
fun findLeakingObjectIds(graph: HeapGraph): Set<Long>
companion object {
/**
* Utility function to create a [LeakingObjectFinder] from the passed in [block] lambda
* instead of using the anonymous `object : LeakingObjectFinder` syntax.
*
* Usage:
*
* ```kotlin
* val listener = LeakingObjectFinder {
*
* }
* ```
*/
inline operator fun invoke(crossinline block: (HeapGraph) -> Set<Long>): LeakingObjectFinder =
object : LeakingObjectFinder {
override fun findLeakingObjectIds(graph: HeapGraph): Set<Long> = block(graph)
}
}
}
6.FilteringLeakingObjectFinder通过扫描堆转储中的所有对象并将决策委托给[FilteringLeakingObjectFinder.LeakingObjectFilter]列表来查找泄漏的对象
/**
* Finds the objects that are leaking by scanning all objects in the heap dump
* and delegating the decision to a list of [FilteringLeakingObjectFinder.LeakingObjectFilter]
* 通过扫描堆转储中的所有对象并将决策委托给[FilteringLeakingObjectFinder.LeakingObjectFilter]列表来查找泄漏的对象
*/
class FilteringLeakingObjectFinder(private val filters: List<LeakingObjectFilter>) :
LeakingObjectFinder {
/**
* Filter to be passed to the [FilteringLeakingObjectFinder] constructor.
* 要传递给[FilteringLeakingObjectFinder]构造函数的筛选器。
*/
interface LeakingObjectFilter {
/**
* Returns whether the passed in [heapObject] is leaking. This should only return true
* when we're 100% sure the passed in [heapObject] should not be in memory anymore.
* 返回传入的[heapObject]是否泄漏。只有当我们100%确定传入的[heapObject]不应再在内存中时,才会返回true。
*/
fun isLeakingObject(heapObject: HeapObject): Boolean
}
override fun findLeakingObjectIds(graph: HeapGraph): Set<Long> {
return graph.objects
.filter { heapObject ->
filters.any { filter ->//any表示至少有一个
filter.isLeakingObject(heapObject)
}
}
.map { it.objectId }
.toSet()
}
}
7.ObjectInspector为 LeakCanary 提供 堆中对象(类、实例和数组)更多的信息。
/**
* Provides LeakCanary with insights about objects (classes, instances and arrays) found in the
* heap. [inspect] will be called for each object that LeakCanary wants to know more about.
* The implementation can then use the provided [ObjectReporter] to provide insights for that
* object.
*
* 为 LeakCanary 提供 堆中对象(类、实例和数组)更多的信息。
* LeakCanary可以调用[inspect]方法,了解对象更多的信息。
* 实现类可以使用提供的 [ObjectReporter] 来提供该对象的信息。
*
* This is a functional interface with which you can create a [ObjectInspector] from a lambda.
*/
fun interface ObjectInspector {
/**
* @see [ObjectInspector]
*/
fun inspect(reporter: ObjectReporter)//todo ObjectReporter干哈的
companion object {
/**
* Utility function to create a [ObjectInspector] from the passed in [block] lambda instead of
* using the anonymous `object : OnHeapAnalyzedListener` syntax.
*
* Usage:
*
* ```kotlin
* val inspector = ObjectInspector { reporter ->
*
* }
* ```
*/
inline operator fun invoke(crossinline block: (ObjectReporter) -> Unit): ObjectInspector =
object : ObjectInspector {
override fun inspect(
reporter: ObjectReporter
) {
block(reporter)
}
}
}
}
标签:MetadataExtractor,Shark,graph,object,LeakingObjectFinder,fun,heapObject 来源: https://blog.csdn.net/stone_cold_cool/article/details/120645631