main

square/leakcanary

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

HeapDumpRule.kt

TLDR

The HeapDumpRule.kt file contains a class HeapDumpRule that extends ExternalResource. The class is used to create temporary folders and provides a method dumpHeap() to generate a heap dump file.

Methods

dumpHeap

This method generates a heap dump file by creating a temporary folder and dumping the heap into a file. The method returns the file object representing the generated heap dump.

Classes

HeapDumpRule

This class extends ExternalResource and is used to create temporary folders and generate heap dump files. It provides a method dumpHeap() to generate the heap dump.

package shark

import org.junit.rules.ExternalResource
import org.junit.rules.TemporaryFolder
import java.io.File
import java.io.IOException
import java.util.UUID

class HeapDumpRule : ExternalResource() {
  private val temporaryFolder = TemporaryFolder()

  @Throws(Throwable::class)
  override fun before() {
    temporaryFolder.create()
  }

  override fun after() {
    temporaryFolder.delete()
  }

  @Throws(IOException::class)
  fun dumpHeap(): File {
    val hprof = File(temporaryFolder.root, "heapDump" + UUID.randomUUID() + ".hprof")
    JvmTestHeapDumper.dumpHeap(hprof.absolutePath)
    return hprof
  }
}