-
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.
ObjectAnimator has an mTarget field that is a weak ref to the target. It's a weak ref as a misguided attempt to prevent leaks. Unfortunately, when an object animator runs forever, the mTarget weak ref will be checked every frame and the target reference held as a java local for a brief moment, which makes it really hard for the GC to collect the targeted object. This changes fixes that by surfacing an additional low priority strong reference from ObjectAnimator to its target. Additionally, this change adds object inspectors for animators that show useful state (such as listeners and running state). ``` ┬─── │ GC Root: Thread object │ ├─ java.lang.Thread instance │ Leaking: NO (the main thread always runs) │ Thread name: 'main' │ ↓ Thread.threadLocals │ ~~~~~~~~~~~~ ├─ java.lang.ThreadLocal$ThreadLocalMap instance │ Leaking: UNKNOWN │ Retaining 1.4 kB in 43 objects │ ↓ ThreadLocal$ThreadLocalMap.table │ ~~~~~ ├─ java.lang.ThreadLocal$ThreadLocalMap$Entry[] array │ Leaking: UNKNOWN │ Retaining 1.3 kB in 42 objects │ ↓ ThreadLocal$ThreadLocalMap$Entry[10] │ ~~~~ ├─ java.lang.ThreadLocal$ThreadLocalMap$Entry instance │ Leaking: UNKNOWN │ Retaining 28 B in 1 objects │ ↓ ThreadLocal$ThreadLocalMap$Entry.value │ ~~~~~ ├─ android.animation.AnimationHandler instance │ Leaking: UNKNOWN │ Retaining 338.6 kB in 3421 objects │ ↓ AnimationHandler.mAnimationCallbacks │ ~~~~~~~~~~~~~~~~~~~ ├─ java.util.ArrayList instance │ Leaking: UNKNOWN │ Retaining 338.5 kB in 3417 objects │ ↓ ArrayList[0] │ ~~~ ├─ android.animation.ObjectAnimator instance │ Leaking: UNKNOWN │ Retaining 338.4 kB in 3415 objects │ mListeners = null │ mPropertyName = null │ mProperty.mName = alpha │ mProperty.mType = java.lang.Float │ mInitialized = true │ mStarted = true │ mRunning = true │ mAnimationEndRequested = false │ mDuration = 100 │ mStartDelay = 0 │ mRepeatCount = INFINITE (-1) │ mRepeatMode = REVERSE (2) │ ↓ ObjectAnimator.mTarget │ ~~~~~~~ ╰→ android.widget.Button instance Leaking: YES (View.mContext references a destroyed activity) ```
- Loading branch information
Showing
7 changed files
with
190 additions
and
2 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
50 changes: 50 additions & 0 deletions
50
shark/src/main/java/shark/internal/InternalSharkCollectionsHelper.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,50 @@ | ||
package shark.internal | ||
|
||
import shark.HeapObject | ||
import shark.HeapObject.HeapClass | ||
import shark.HeapObject.HeapInstance | ||
import shark.HeapObject.HeapObjectArray | ||
import shark.HeapObject.HeapPrimitiveArray | ||
|
||
/** | ||
* INTERNAL | ||
* | ||
* This class is public to be accessible from other LeakCanary modules but shouldn't be | ||
* called directly, the API may break at any point. | ||
*/ | ||
object InternalSharkCollectionsHelper { | ||
|
||
fun arrayListValues(heapInstance: HeapInstance): Sequence<String> { | ||
val graph = heapInstance.graph | ||
val arrayListReader = OpenJdkInstanceRefReaders.ARRAY_LIST.create(graph) | ||
?: ApacheHarmonyInstanceRefReaders.ARRAY_LIST.create(graph) | ||
?: return emptySequence() | ||
|
||
if (!arrayListReader.matches(heapInstance)) { | ||
return emptySequence() | ||
} | ||
|
||
return arrayListReader.read(heapInstance).map { reference -> | ||
val arrayListValue = graph.findObjectById(reference.valueObjectId) | ||
val details = reference.lazyDetailsResolver.resolve() | ||
"[${details.name}] = ${className(arrayListValue)}" | ||
} | ||
} | ||
|
||
private fun className(graphObject: HeapObject): String { | ||
return when (graphObject) { | ||
is HeapClass -> { | ||
graphObject.name | ||
} | ||
is HeapInstance -> { | ||
graphObject.instanceClassName | ||
} | ||
is HeapObjectArray -> { | ||
graphObject.arrayClassName | ||
} | ||
is HeapPrimitiveArray -> { | ||
graphObject.arrayClassName | ||
} | ||
} | ||
} | ||
} |