main

square/leakcanary

Last updated at: 29/12/2023 09:40

JavaFrames.kt

TLDR

The provided file, JavaFrames.kt, is a Kotlin file located at shark/shark/src/main/java/shark/internal/JavaFrames.kt. It contains a single object, JavaFrames, which provides two methods.

Methods

getByThreadObjectId

This method takes in a HeapGraph object and a threadObjectId of type Long. It retrieves the ThreadObject associated with the given threadObjectId from the graph using the ThreadObjects.getByThreadObjectId() method. If no ThreadObject is found, it returns null. Otherwise, it obtains the mapping of JavaFrame objects grouped by the thread serial number from the JavaFrames.getJavaFramesByThreadSerialNumber() method. Finally, it returns the list of JavaFrame objects associated with the thread serial number of the retrieved ThreadObject.

getJavaFramesByThreadSerialNumber

This private method takes in a HeapGraph object and returns a map of JavaFrame objects grouped by the thread serial number. It uses the HeapGraph's gcRoots property to obtain all JavaFrame objects and then groups them by the javaFrame.threadSerialNumber.

END

package shark.internal

import shark.GcRoot.JavaFrame
import shark.HeapGraph

internal object JavaFrames {

  private fun getJavaFramesByThreadSerialNumber(graph: HeapGraph) =
    graph.context.getOrPut(JavaFrames::class.java.name) {
      graph.gcRoots.asSequence().filterIsInstance<JavaFrame>().groupBy { javaFrame ->
        javaFrame.threadSerialNumber
      }
    }

  fun getByThreadObjectId(graph: HeapGraph, threadObjectId: Long): List<JavaFrame>? {
    val threadObject = ThreadObjects.getByThreadObjectId(graph, threadObjectId) ?: return null
    val javaFrameByThreadSerial = getJavaFramesByThreadSerialNumber(graph)
    return javaFrameByThreadSerial[threadObject.threadSerialNumber]
  }
}