Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LottieTask leak #2465

Merged
merged 1 commit into from Feb 18, 2024
Merged

Fix LottieTask leak #2465

merged 1 commit into from Feb 18, 2024

Conversation

pyricau
Copy link
Contributor

@pyricau pyricau commented Feb 17, 2024

I investigated leaks with RememberLottieComposition and LottieAnimationView, initially thinking that we might need to stop listening to LottieTask when a view or composition gets detached / removed.

However, after looking at a heap dump, I realized that the LottieTask was actually finished. It had delivered its result so it didn't have any reason to stay in memory.

This moved my suspicion to SynchronousQueue as Android has had issues with queues & worker threads on several occasions (see code for details).

Also updated the cached thread pool to create threads named "lottie".

Leaks:

┬───
│ GC Root: Thread object
│
├─ java.lang.Thread instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181913 objects
│    Thread name: 'pool-88-thread-2'
│    ↓ Thread<Java Local>
│            ~~~~~~~~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181909 objects
│    ↓ SynchronousQueue$TransferStack$SNode.match
│                                           ~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181908 objects
│    ↓ SynchronousQueue$TransferStack$SNode.item
│                                           ~~~~
├─ com.airbnb.lottie.LottieTask$LottieFutureTask instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181907 objects
│    ↓ LottieTask$LottieFutureTask.this$0
│                                  ~~~~~~
├─ com.airbnb.lottie.LottieTask instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181905 objects
│    ↓ LottieTask.failureListeners
│                 ~~~~~~~~~~~~~~~~
├─ java.util.LinkedHashSet instance
│    Leaking: UNKNOWN
│    Retaining 113 B in 5 objects
│    ↓ LinkedHashSet[element()]
│                   ~~~~~~~~~~~
├─ com.airbnb.lottie.compose.RememberLottieCompositionKt$await$2$2 instance
│    Leaking: UNKNOWN
│    Retaining 12 B in 1 objects
│    Anonymous class implementing com.airbnb.lottie.LottieListener
│    ↓ RememberLottieCompositionKt$await$2$2.$cont
│                                            ~~~~~
├─ kotlinx.coroutines.CancellableContinuationImpl instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181892 objects
│    ↓ CancellableContinuationImpl.delegate
│                                  ~~~~~~~~
├─ androidx.compose.ui.test.FrameDeferringContinuationInterceptor$FrameDeferredContinuation instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181888 objects
│    ↓ FrameDeferringContinuationInterceptor$FrameDeferredContinuation.continuation
│                                                                      ~~~~~~~~~~~~
├─ androidx.compose.ui.test.ApplyingContinuationInterceptor$SendApplyContinuation instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181887 objects
│    ↓ ApplyingContinuationInterceptor$SendApplyContinuation.continuation
│                                                            ~~~~~~~~~~~~
├─ com.airbnb.lottie.compose.RememberLottieCompositionKt$lottieComposition$1 instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181886 objects
│    Anonymous subclass of kotlin.coroutines.jvm.internal.ContinuationImpl
│    ↓ BaseContinuationImpl.completion
│                           ~~~~~~~~~~
├─ com.airbnb.lottie.compose.RememberLottieCompositionKt$rememberLottieComposition$3 instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181885 objects
│    Anonymous subclass of kotlin.coroutines.jvm.internal.SuspendLambda
│    $context instance of com.squareup.ui.market.core.theme.MarketContextWrapper, wrapping activity com.squareup.ui.main.MainActivity with mDestroyed = true
│    ↓ RememberLottieCompositionKt$rememberLottieComposition$3.$context
│                                                              ~~~~~~~~
╰→ com.squareup.ui.market.core.theme.MarketContextWrapper instance
​     Leaking: YES (MarketContextWrapper wraps an Activity with Activity.mDestroyed true)
​     Retaining 6.2 MB in 181845 objects
​     mBase instance of com.squareup.ui.market.core.theme.MarketContextWrapper, wrapping activity com.squareup.ui.main.MainActivity with mDestroyed = true
┬───
│ GC Root: Thread object
│
├─ java.lang.Thread instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257497 objects
│    Thread name: 'pool-38-thread-1'
│    ↓ Thread<Java Local>
│            ~~~~~~~~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257493 objects
│    ↓ SynchronousQueue$TransferStack$SNode.match
│                                           ~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257492 objects
│    ↓ SynchronousQueue$TransferStack$SNode.item
│                                           ~~~~
├─ com.airbnb.lottie.LottieTask$LottieFutureTask instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257491 objects
│    ↓ LottieTask$LottieFutureTask.this$0
│                                  ~~~~~~
├─ com.airbnb.lottie.LottieTask instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257489 objects
│    ↓ LottieTask.failureListeners
│                 ~~~~~~~~~~~~~~~~
├─ java.util.LinkedHashSet instance
│    Leaking: UNKNOWN
│    Retaining 101 B in 4 objects
│    ↓ LinkedHashSet[element()]
│                   ~~~~~~~~~~~
├─ com.airbnb.lottie.LottieAnimationView$1 instance
│    Leaking: UNKNOWN
│    Retaining 12 B in 1 objects
│    Anonymous class implementing com.airbnb.lottie.LottieListener
│    ↓ LottieAnimationView$1.this$0
│                            ~~~~~~
╰→ com.airbnb.lottie.LottieAnimationView instance
​     Leaking: YES (View.mContext references a destroyed activity)
​     Retaining 8.1 MB in 257476 objects
​     View not part of a window view hierarchy
​     View.mAttachInfo is null (view detached)
​     View.mWindowAttachCount = 1
​     mContext instance of flow.path.FlowPathContextWrapper, wrapping activity com.squareup.ui.main.MainActivity with mDestroyed = true

I investigated leaks with `RememberLottieComposition`` and `LottieAnimationView`, initially thinking that we might need to stop listening to LottieTask when a view or composition gets detached / removed.

However, after looking at a heap dump, I realized that the LottieTask was actually finished. It had delivered its result so it didn't have any reason to stay in memory.

This moved my suspicion to SynchronousQueue as Android has had issues with queues & worker threads on several occasions (see code for details).

Also updated the cached thread pool to create threads named "lottie".

Leaks:

```
┬───
│ GC Root: Thread object
│
├─ java.lang.Thread instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181913 objects
│    Thread name: 'pool-88-thread-2'
│    ↓ Thread<Java Local>
│            ~~~~~~~~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181909 objects
│    ↓ SynchronousQueue$TransferStack$SNode.match
│                                           ~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181908 objects
│    ↓ SynchronousQueue$TransferStack$SNode.item
│                                           ~~~~
├─ com.airbnb.lottie.LottieTask$LottieFutureTask instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181907 objects
│    ↓ LottieTask$LottieFutureTask.this$0
│                                  ~~~~~~
├─ com.airbnb.lottie.LottieTask instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181905 objects
│    ↓ LottieTask.failureListeners
│                 ~~~~~~~~~~~~~~~~
├─ java.util.LinkedHashSet instance
│    Leaking: UNKNOWN
│    Retaining 113 B in 5 objects
│    ↓ LinkedHashSet[element()]
│                   ~~~~~~~~~~~
├─ com.airbnb.lottie.compose.RememberLottieCompositionKt$await$2$2 instance
│    Leaking: UNKNOWN
│    Retaining 12 B in 1 objects
│    Anonymous class implementing com.airbnb.lottie.LottieListener
│    ↓ RememberLottieCompositionKt$await$2$2.$cont
│                                            ~~~~~
├─ kotlinx.coroutines.CancellableContinuationImpl instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181892 objects
│    ↓ CancellableContinuationImpl.delegate
│                                  ~~~~~~~~
├─ androidx.compose.ui.test.FrameDeferringContinuationInterceptor$FrameDeferredContinuation instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181888 objects
│    ↓ FrameDeferringContinuationInterceptor$FrameDeferredContinuation.continuation
│                                                                      ~~~~~~~~~~~~
├─ androidx.compose.ui.test.ApplyingContinuationInterceptor$SendApplyContinuation instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181887 objects
│    ↓ ApplyingContinuationInterceptor$SendApplyContinuation.continuation
│                                                            ~~~~~~~~~~~~
├─ com.airbnb.lottie.compose.RememberLottieCompositionKt$lottieComposition$1 instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181886 objects
│    Anonymous subclass of kotlin.coroutines.jvm.internal.ContinuationImpl
│    ↓ BaseContinuationImpl.completion
│                           ~~~~~~~~~~
├─ com.airbnb.lottie.compose.RememberLottieCompositionKt$rememberLottieComposition$3 instance
│    Leaking: UNKNOWN
│    Retaining 6.2 MB in 181885 objects
│    Anonymous subclass of kotlin.coroutines.jvm.internal.SuspendLambda
│    $context instance of com.squareup.ui.market.core.theme.MarketContextWrapper, wrapping activity com.squareup.ui.main.MainActivity with mDestroyed = true
│    ↓ RememberLottieCompositionKt$rememberLottieComposition$3.$context
│                                                              ~~~~~~~~
╰→ com.squareup.ui.market.core.theme.MarketContextWrapper instance
​     Leaking: YES (MarketContextWrapper wraps an Activity with Activity.mDestroyed true)
​     Retaining 6.2 MB in 181845 objects
​     mBase instance of com.squareup.ui.market.core.theme.MarketContextWrapper, wrapping activity com.squareup.ui.main.MainActivity with mDestroyed = true
```

```
┬───
│ GC Root: Thread object
│
├─ java.lang.Thread instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257497 objects
│    Thread name: 'pool-38-thread-1'
│    ↓ Thread<Java Local>
│            ~~~~~~~~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257493 objects
│    ↓ SynchronousQueue$TransferStack$SNode.match
│                                           ~~~~~
├─ java.util.concurrent.SynchronousQueue$TransferStack$SNode instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257492 objects
│    ↓ SynchronousQueue$TransferStack$SNode.item
│                                           ~~~~
├─ com.airbnb.lottie.LottieTask$LottieFutureTask instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257491 objects
│    ↓ LottieTask$LottieFutureTask.this$0
│                                  ~~~~~~
├─ com.airbnb.lottie.LottieTask instance
│    Leaking: UNKNOWN
│    Retaining 8.1 MB in 257489 objects
│    ↓ LottieTask.failureListeners
│                 ~~~~~~~~~~~~~~~~
├─ java.util.LinkedHashSet instance
│    Leaking: UNKNOWN
│    Retaining 101 B in 4 objects
│    ↓ LinkedHashSet[element()]
│                   ~~~~~~~~~~~
├─ com.airbnb.lottie.LottieAnimationView$1 instance
│    Leaking: UNKNOWN
│    Retaining 12 B in 1 objects
│    Anonymous class implementing com.airbnb.lottie.LottieListener
│    ↓ LottieAnimationView$1.this$0
│                            ~~~~~~
╰→ com.airbnb.lottie.LottieAnimationView instance
​     Leaking: YES (View.mContext references a destroyed activity)
​     Retaining 8.1 MB in 257476 objects
​     View not part of a window view hierarchy
​     View.mAttachInfo is null (view detached)
​     View.mWindowAttachCount = 1
​     mContext instance of flow.path.FlowPathContextWrapper, wrapping activity com.squareup.ui.main.MainActivity with mDestroyed = true
```
Copy link

Snapshot Tests
API 23: Report Diff
API 31: Report Diff

@gpeal gpeal merged commit fe412fa into airbnb:master Feb 18, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants