Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2621 from square/py/update_heap_growth_apis
Revisit heap growth detection APIs
- Loading branch information
Showing
80 changed files
with
3,023 additions
and
1,556 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
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
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
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,51 @@ | ||
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/DateFormatHeapDumpFileProviderKt { | ||
public static final fun datetimeFormattedFileProvider (Lleakcanary/HeapDumpFileProvider$Companion;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function0;)Lleakcanary/HeapDumpFileProvider; | ||
public static synthetic fun datetimeFormattedFileProvider$default (Lleakcanary/HeapDumpFileProvider$Companion;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lleakcanary/HeapDumpFileProvider; | ||
} | ||
|
||
public final class leakcanary/DumpingAndDeletingHeapGraphProvider : shark/HeapGraphProvider { | ||
public fun <init> (Lleakcanary/HeapDumpFileProvider;Lleakcanary/HeapDumper;)V | ||
public fun openHeapGraph ()Lshark/CloseableHeapGraph; | ||
} | ||
|
||
public final class leakcanary/DumpingAndDeletingHeapGraphProviderKt { | ||
public static final fun dumpingAndDeletingGraphProvider (Lshark/HeapGraphProvider$Companion;Lleakcanary/HeapDumper;Lleakcanary/HeapDumpFileProvider;)Lleakcanary/DumpingAndDeletingHeapGraphProvider; | ||
public static synthetic fun dumpingAndDeletingGraphProvider$default (Lshark/HeapGraphProvider$Companion;Lleakcanary/HeapDumper;Lleakcanary/HeapDumpFileProvider;ILjava/lang/Object;)Lleakcanary/DumpingAndDeletingHeapGraphProvider; | ||
} | ||
|
||
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 abstract interface class leakcanary/HeapDumper { | ||
public static final field Companion Lleakcanary/HeapDumper$Companion; | ||
public abstract fun dumpHeap (Ljava/io/File;)V | ||
} | ||
|
||
public final class leakcanary/HeapDumper$Companion { | ||
} | ||
|
||
public final class leakcanary/TempHeapDumpFileProvider : leakcanary/HeapDumpFileProvider { | ||
public static final field INSTANCE Lleakcanary/TempHeapDumpFileProvider; | ||
public fun newHeapDumpFile ()Ljava/io/File; | ||
} | ||
|
||
public final class leakcanary/TempHeapDumpFileProviderKt { | ||
public static final fun tempFileProvider (Lleakcanary/HeapDumpFileProvider$Companion;)Lleakcanary/HeapDumpFileProvider; | ||
} | ||
|
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,11 @@ | ||
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 |
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
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" | ||
} | ||
} | ||
|
||
fun HeapDumpFileProvider.Companion.datetimeFormattedFileProvider( | ||
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 | ||
) | ||
} |
30 changes: 30 additions & 0 deletions
30
leakcanary/leakcanary-core/src/main/java/leakcanary/DumpingAndDeletingHeapGraphProvider.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,30 @@ | ||
package leakcanary | ||
|
||
import shark.CloseableHeapGraph | ||
import shark.HeapGraphProvider | ||
import shark.HprofHeapGraph.Companion.openHeapGraph | ||
|
||
class DumpingAndDeletingHeapGraphProvider( | ||
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() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun HeapGraphProvider.Companion.dumpingAndDeletingGraphProvider( | ||
heapDumper: HeapDumper, | ||
heapDumpFileProvider: HeapDumpFileProvider = TempHeapDumpFileProvider, | ||
) = DumpingAndDeletingHeapGraphProvider(heapDumpFileProvider, heapDumper) |
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
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 | ||
|
||
/** | ||
* This allows external modules to add factory methods for implementations of this interface as | ||
* extension functions of this companion object. | ||
*/ | ||
companion object | ||
} |
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
Oops, something went wrong.