Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
831 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...anary-android-instrumentation/src/main/java/leakcanary/AndroidLiveObjectGrowthDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package leakcanary | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import java.io.File | ||
import shark.HeapDumpingObjectGrowthDetector | ||
import shark.LiveObjectGrowthDetector | ||
import shark.RepeatedObjectGrowthDetector | ||
|
||
// TODO This name is wrong, and create method is wrong. | ||
object AndroidLiveObjectGrowthDetector { | ||
|
||
fun create( | ||
maxHeapDumps: Int = 5, | ||
scenarioLoopsPerDump: Int = 5, | ||
heapDumpFileProvider: HeapDumpFileProvider = HeapDumpFileProvider.dateFormatted( | ||
directory = File( | ||
InstrumentationRegistry.getInstrumentation().targetContext.filesDir, | ||
"heap-growth-hprof" | ||
), | ||
prefix = "heap-growth-" | ||
), | ||
heapDumper: HeapDumper = AndroidDebugHeapDumper, | ||
objectRepeatedGrowthDetector: RepeatedObjectGrowthDetector | ||
): LiveObjectGrowthDetector { | ||
val heapGraphProvider = | ||
DumpingDeletingOnCloseHeapGraphProvider(heapDumpFileProvider, heapDumper) | ||
|
||
return HeapDumpingObjectGrowthDetector( | ||
maxHeapDumps = maxHeapDumps, | ||
heapGraphProvider = heapGraphProvider, | ||
scenarioLoopsPerDump = scenarioLoopsPerDump, | ||
detector = objectRepeatedGrowthDetector | ||
) | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
...-instrumentation/src/main/java/leakcanary/internal/InstrumentationHeapDumpFileProvider.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
public final class leakcanary/DateFormatHeapDumpFileProvider : leakcanary/HeapDumpFileProvider { | ||
public static final field Companion Lleakcanary/DateFormatHeapDumpFileProvider$Companion; | ||
public static final field TIME_PATTERN Ljava/lang/String; | ||
public fun <init> (Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Ljava/lang/String;Ljava/lang/String;)V | ||
public synthetic fun <init> (Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function0;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public fun newHeapDumpFile ()Ljava/io/File; | ||
} | ||
|
||
public final class leakcanary/DateFormatHeapDumpFileProvider$Companion { | ||
} | ||
|
||
public final class leakcanary/DumpingDeletingOnCloseHeapGraphProvider : shark/HeapGraphProvider { | ||
public fun <init> (Lleakcanary/HeapDumpFileProvider;Lleakcanary/HeapDumper;)V | ||
public fun openHeapGraph ()Lshark/CloseableHeapGraph; | ||
} | ||
|
||
public abstract interface class leakcanary/HeapDumpFileProvider { | ||
public static final field Companion Lleakcanary/HeapDumpFileProvider$Companion; | ||
public abstract fun newHeapDumpFile ()Ljava/io/File; | ||
} | ||
|
||
public final class leakcanary/HeapDumpFileProvider$Companion { | ||
public final fun dateFormatted (Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function0;)Lleakcanary/HeapDumpFileProvider; | ||
public static synthetic fun dateFormatted$default (Lleakcanary/HeapDumpFileProvider$Companion;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lleakcanary/HeapDumpFileProvider; | ||
} | ||
|
||
public abstract interface class leakcanary/HeapDumper { | ||
public abstract fun dumpHeap (Ljava/io/File;)V | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("com.vanniktech.maven.publish") | ||
} | ||
|
||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
|
||
dependencies { | ||
api projects.shark.shark | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
POM_ARTIFACT_ID=leakcanary-core | ||
POM_NAME=LeakCanary Core | ||
POM_PACKAGING=jar |
39 changes: 39 additions & 0 deletions
39
leakcanary/leakcanary-core/src/main/java/leakcanary/DateFormatHeapDumpFileProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package leakcanary | ||
|
||
import java.io.File | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
class DateFormatHeapDumpFileProvider( | ||
private val heapDumpDirectoryProvider: () -> File, | ||
private val dateProvider: () -> Date = { Date() }, | ||
prefix: String = "", | ||
suffix: String = "" | ||
) : HeapDumpFileProvider { | ||
|
||
private val dateFormatPattern = | ||
"${escape(prefix)}$TIME_PATTERN${escape("$suffix.hprof")}" | ||
|
||
private val timeFormatThreadLocal = object : ThreadLocal<SimpleDateFormat>() { | ||
// Lint is drunk and thinks we use the pattern 'u' | ||
@Suppress("NewApi") | ||
override fun initialValue() = | ||
SimpleDateFormat(dateFormatPattern, Locale.US) | ||
} | ||
|
||
override fun newHeapDumpFile(): File { | ||
val heapDumpDirectory = heapDumpDirectoryProvider() | ||
val date = dateProvider() | ||
val fileName = timeFormatThreadLocal.get()!!.format(date) | ||
return File(heapDumpDirectory, fileName) | ||
} | ||
|
||
private fun escape(string: String) = if (string != "") { | ||
"'$string'" | ||
} else "" | ||
|
||
companion object { | ||
const val TIME_PATTERN = "yyyy-MM-dd_HH-mm-ss_SSS" | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...anary/leakcanary-core/src/main/java/leakcanary/DumpingDeletingOnCloseHeapGraphProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package leakcanary | ||
|
||
import shark.CloseableHeapGraph | ||
import shark.HeapGraphProvider | ||
import shark.HprofHeapGraph.Companion.openHeapGraph | ||
|
||
class DumpingDeletingOnCloseHeapGraphProvider( | ||
private val heapDumpFileProvider: HeapDumpFileProvider, | ||
private val heapDumper: HeapDumper | ||
) : HeapGraphProvider { | ||
override fun openHeapGraph(): CloseableHeapGraph { | ||
val heapDumpFile = heapDumpFileProvider.newHeapDumpFile() | ||
heapDumper.dumpHeap(heapDumpFile) | ||
check(heapDumpFile.exists()) { | ||
"Expected file to exist after heap dump: ${heapDumpFile.absolutePath}" | ||
} | ||
val realGraph = heapDumpFile.openHeapGraph() | ||
return object : CloseableHeapGraph by realGraph { | ||
override fun close() { | ||
realGraph.close() | ||
heapDumpFile.delete() | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
leakcanary/leakcanary-core/src/main/java/leakcanary/HeapDumpFileProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package leakcanary | ||
|
||
import java.io.File | ||
import java.util.Date | ||
|
||
fun interface HeapDumpFileProvider { | ||
|
||
/** | ||
* Returns a [File] that can be passed to a [HeapDumper] to dump the heap. | ||
*/ | ||
fun newHeapDumpFile(): File | ||
|
||
companion object { | ||
fun dateFormatted( | ||
directory: File, | ||
prefix: String = "", | ||
suffix: String = "", | ||
dateProvider: () -> Date = { Date() }, | ||
): HeapDumpFileProvider { | ||
return DateFormatHeapDumpFileProvider( | ||
heapDumpDirectoryProvider = { | ||
directory.apply { | ||
mkdirs() | ||
check(exists()) { | ||
"Expected heap dump folder to exist: $absolutePath" | ||
} | ||
} | ||
}, | ||
dateProvider = dateProvider, | ||
prefix = prefix, | ||
suffix = suffix | ||
) | ||
} | ||
} | ||
} |
File renamed without changes.
16 changes: 16 additions & 0 deletions
16
leakcanary/leakcanary-jvm-test/api/leakcanary-jvm-test.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public final class leakcanary/HotSpotHeapDumper : leakcanary/HeapDumper { | ||
public static final field INSTANCE Lleakcanary/HotSpotHeapDumper; | ||
public fun dumpHeap (Ljava/io/File;)V | ||
} | ||
|
||
public final class leakcanary/JvmLiveObjectGrowthDetector { | ||
public static final field INSTANCE Lleakcanary/JvmLiveObjectGrowthDetector; | ||
public final fun create (IILleakcanary/HeapDumpFileProvider;Lleakcanary/HeapDumper;Lshark/RepeatedObjectGrowthDetector;)Lshark/LiveObjectGrowthDetector; | ||
public static synthetic fun create$default (Lleakcanary/JvmLiveObjectGrowthDetector;IILleakcanary/HeapDumpFileProvider;Lleakcanary/HeapDumper;Lshark/RepeatedObjectGrowthDetector;ILjava/lang/Object;)Lshark/LiveObjectGrowthDetector; | ||
} | ||
|
||
public final class leakcanary/TempHeapDumpFileProvider : leakcanary/HeapDumpFileProvider { | ||
public static final field INSTANCE Lleakcanary/TempHeapDumpFileProvider; | ||
public fun newHeapDumpFile ()Ljava/io/File; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
POM_ARTIFACT_ID=leakcanary-jvm-test | ||
POM_NAME=LeakCanary Jvm Test | ||
POM_PACKAGING=jar |
21 changes: 21 additions & 0 deletions
21
leakcanary/leakcanary-jvm-test/src/main/java/leakcanary/HotSpotHeapDumper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package leakcanary | ||
|
||
import com.sun.management.HotSpotDiagnosticMXBean | ||
import java.io.File | ||
import java.lang.management.ManagementFactory | ||
|
||
object HotSpotHeapDumper : HeapDumper { | ||
private val hotspotMBean: HotSpotDiagnosticMXBean by lazy { | ||
val mBeanServer = ManagementFactory.getPlatformMBeanServer() | ||
ManagementFactory.newPlatformMXBeanProxy( | ||
mBeanServer, | ||
"com.sun.management:type=HotSpotDiagnostic", | ||
HotSpotDiagnosticMXBean::class.java | ||
) | ||
} | ||
|
||
override fun dumpHeap(heapDumpFile: File) { | ||
val live = true | ||
hotspotMBean.dumpHeap(heapDumpFile.absolutePath, live) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
leakcanary/leakcanary-jvm-test/src/main/java/leakcanary/JvmLiveObjectGrowthDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package leakcanary | ||
|
||
import shark.HeapDumpingObjectGrowthDetector | ||
import shark.LiveObjectGrowthDetector | ||
import shark.RepeatedObjectGrowthDetector | ||
|
||
// TODO This name is wrong, and create method is wrong. | ||
object JvmLiveObjectGrowthDetector { | ||
|
||
fun create( | ||
maxHeapDumps: Int = 5, | ||
scenarioLoopsPerDump: Int = 5, | ||
heapDumpFileProvider: HeapDumpFileProvider = TempHeapDumpFileProvider, | ||
heapDumper: HeapDumper = HotSpotHeapDumper, | ||
repeatedObjectGrowthDetector: RepeatedObjectGrowthDetector | ||
): LiveObjectGrowthDetector { | ||
val heapGraphProvider = | ||
DumpingDeletingOnCloseHeapGraphProvider(heapDumpFileProvider, heapDumper) | ||
return HeapDumpingObjectGrowthDetector( | ||
maxHeapDumps = maxHeapDumps, | ||
heapGraphProvider = heapGraphProvider, | ||
scenarioLoopsPerDump = scenarioLoopsPerDump, | ||
detector = repeatedObjectGrowthDetector | ||
) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
leakcanary/leakcanary-jvm-test/src/main/java/leakcanary/TempHeapDumpFileProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package leakcanary | ||
|
||
import java.io.File | ||
|
||
object TempHeapDumpFileProvider : HeapDumpFileProvider { | ||
override fun newHeapDumpFile(): File { | ||
val heapDumpFile = File.createTempFile("heap-growth", ".hprof", null) | ||
check(heapDumpFile.delete()) { | ||
"Could not delete $heapDumpFile, needs to not exist for heap dump" | ||
} | ||
return heapDumpFile | ||
} | ||
} |
Oops, something went wrong.