-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #2214
- Loading branch information
Showing
11 changed files
with
333 additions
and
200 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Loaded via reflection & referenced by shark.AndroidReferenceMatchers.LEAK_CANARY_INTERNAL | ||
-keep class leakcanary.internal.InternalLeakCanary { *; } | ||
# Referenced by shark.AndroidReferenceMatchers.LEAK_CANARY_HEAP_DUMPER | ||
-keep class leakcanary.internal.AndroidHeapDumper { *; } | ||
-keep class leakcanary.internal.HeapDumperController { *; } | ||
# Marshmallow removed Notification.setLatestEventInfo() | ||
-dontwarn android.app.Notification |
38 changes: 38 additions & 0 deletions
38
leakcanary-android-core/src/main/java/leakcanary/AndroidDebugHeapDumper.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,38 @@ | ||
package leakcanary | ||
|
||
import android.os.Debug | ||
import leakcanary.HeapDumper.DumpLocation | ||
import leakcanary.HeapDumper.DumpLocation.FileLocation | ||
import leakcanary.HeapDumper.Result | ||
import leakcanary.HeapDumper.Result.Failure | ||
import leakcanary.HeapDumper.Result.HeapDump | ||
import leakcanary.internal.friendly.measureDurationMillis | ||
|
||
/** | ||
* Dumps the heap using [Debug.dumpHprofData]. [dumpHeap] is expected to be passed in a | ||
* [FileLocation] and will otherwise fail. | ||
* | ||
* Note: measures the duration of the call to [Debug.dumpHprofData] using | ||
* [android.os.SystemClock.uptimeMillis]. | ||
*/ | ||
object AndroidDebugHeapDumper : HeapDumper { | ||
override fun dumpHeap(dumpLocation: DumpLocation): Result { | ||
return if (dumpLocation is FileLocation) { | ||
val outputFile = dumpLocation.file | ||
try { | ||
val durationMillis = measureDurationMillis { | ||
Debug.dumpHprofData(outputFile.absolutePath) | ||
} | ||
if (outputFile.length() == 0L) { | ||
Failure(RuntimeException("Dumped heap file is 0 byte length")) | ||
} else { | ||
HeapDump(outputFile, durationMillis) | ||
} | ||
} catch (e: Throwable) { | ||
Failure(e) | ||
} | ||
} else { | ||
Failure(RuntimeException("HeapDumper.DumpLocation is Unspecified")) | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
leakcanary-android-core/src/main/java/leakcanary/HeapDumper.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,33 @@ | ||
package leakcanary | ||
|
||
import java.io.File | ||
|
||
fun interface HeapDumper { | ||
|
||
sealed class DumpLocation { | ||
object Unspecified : DumpLocation() | ||
class FileLocation(val file: File) : DumpLocation() | ||
} | ||
|
||
sealed class Result { | ||
class Failure(val exception: Throwable) : Result() | ||
class HeapDump( | ||
val file: File, | ||
val durationMillis: Long | ||
) : Result() | ||
} | ||
|
||
/** | ||
* Dumps the heap. The implementation is expected to be blocking until the heap is dumped | ||
* or heap dumping failed. | ||
* | ||
* The [HeapDumper] interface is designed for delegation. | ||
* | ||
* [dumpLocation] may be [DumpLocation.Unspecified] or [DumpLocation.FileLocation], and | ||
* it's ok for implementations to know how to deal with one or the other (or both) and otherwise | ||
* return a [Result.Failure]. | ||
* | ||
* @return [Result.HeapDump] if dumping the heap succeeded, and [Result.Failure] otherwise. | ||
*/ | ||
fun dumpHeap(dumpLocation: DumpLocation): Result | ||
} |
32 changes: 32 additions & 0 deletions
32
leakcanary-android-core/src/main/java/leakcanary/HeapDumpers.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,32 @@ | ||
package leakcanary | ||
|
||
import leakcanary.internal.withManagedHeapDumpDirectory | ||
import leakcanary.internal.withNotification | ||
import leakcanary.internal.withResourceIdNames | ||
import leakcanary.internal.withToast | ||
|
||
fun leakCanaryHeapDumper( | ||
/** | ||
* The core [HeapDumper] which will be wrapped by delegates. | ||
*/ | ||
coreHeapDumper: HeapDumper = AndroidDebugHeapDumper, | ||
/** | ||
* Wraps [HeapDumper.dumpHeap] to show cute canary Toast while the heap is being dumped. | ||
*/ | ||
toastOnDump: Boolean = true, | ||
/** | ||
* Wraps [HeapDumper.dumpHeap] to show an Android notification that says "Dumping Heap" while the | ||
* heap is being dumped. | ||
*/ | ||
notificationOnDump: Boolean = true | ||
): HeapDumper { | ||
var heapDumper = coreHeapDumper.withResourceIdNames() | ||
if (notificationOnDump) { | ||
heapDumper = heapDumper.withNotification() | ||
} | ||
if (toastOnDump) { | ||
heapDumper = heapDumper.withToast() | ||
} | ||
heapDumper = heapDumper.withManagedHeapDumpDirectory() | ||
return heapDumper | ||
} |
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
123 changes: 0 additions & 123 deletions
123
leakcanary-android-core/src/main/java/leakcanary/internal/AndroidHeapDumper.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.