Coil leaks Activity Context when an Activity with an ongoing request is destroyed
See original GitHub issueDescribe the bug
Coil causes a memory leak when finish()
is called on an Activity that has an ongoing request.
To Reproduce Download & run the attached project: CoilMemoryLeak.zip
ActivityA
tries to load an βimageβ from a URL that takes 60 seconds to respond. If you click the βGo to Activity Bβ button during that time, then LeakCanary should detect retained objects (check the notification). This does not happen if the request has finished before going to ActivityB
.
Expected behavior Any references to the Activity should always be cleared when it is destroyed.
Logs/Screenshots
2019-08-12 14:51:11.619 8859-9256/com.example.coilmemoryleak D/LeakCanary: HeapAnalysisSuccess(heapDumpFile=/data/user/0/com.example.coilmemoryleak/files/leakcanary/2019-08-12_14-50-53_530.hprof, createdAtTimeMillis=1565610671616, analysisDurationMillis=14783, applicationLeaks=[ApplicationLeak(className=com.example.coilmemoryleak.ActivityA, leakTrace=
β¬
ββ android.os.HandlerThread
β Leaking: NO (PathClassLoaderβ is not leaking)
β Thread name: 'LeakCanary-Heap-Dump'
β GC Root: Thread object
β β thread HandlerThread.contextClassLoader
ββ dalvik.system.PathClassLoader
β Leaking: NO (Object[]β is not leaking and A ClassLoader is never leaking)
β β PathClassLoader.runtimeInternalObjects
ββ java.lang.Object[]
β Leaking: NO (Coilβ is not leaking)
β β array Object[].[870]
ββ coil.Coil
β Leaking: NO (a class is never leaking)
β β static Coil.imageLoader
β ~~~~~~~~~~~
ββ coil.RealImageLoader
β Leaking: UNKNOWN
β β RealImageLoader.loaderScope
β ~~~~~~~~~~~
ββ kotlinx.coroutines.internal.ContextScope
β Leaking: UNKNOWN
β β ContextScope.coroutineContext
β ~~~~~~~~~~~~~~~~
ββ kotlin.coroutines.CombinedContext
β Leaking: UNKNOWN
β β CombinedContext.left
β ~~~~
ββ kotlinx.coroutines.SupervisorJobImpl
β Leaking: UNKNOWN
β β SupervisorJobImpl._state
β ~~~~~~
ββ kotlinx.coroutines.NodeList
β Leaking: UNKNOWN
β β NodeList._next
β ~~~~~
ββ kotlinx.coroutines.ChildHandleNode
β Leaking: UNKNOWN
β β ChildHandleNode.childJob
β ~~~~~~~~
ββ kotlinx.coroutines.StandaloneCoroutine
β Leaking: UNKNOWN
β β StandaloneCoroutine._state
β ~~~~~~
ββ kotlinx.coroutines.ChildHandleNode
β Leaking: UNKNOWN
β β ChildHandleNode.childJob
β ~~~~~~~~
ββ kotlinx.coroutines.UndispatchedCoroutine
β Leaking: UNKNOWN
β β UndispatchedCoroutine.uCont
β ~~~~~
ββ coil.RealImageLoader$load$job$1
β Leaking: UNKNOWN
β Anonymous subclass of kotlin.coroutines.jvm.internal.SuspendLambda
β β RealImageLoader$load$job$1.$request
β ~~~~~~~~
ββ coil.request.LoadRequest
β Leaking: UNKNOWN
β β LoadRequest.context
β ~~~~~~~
β°β com.example.coilmemoryleak.ActivityA
β Leaking: YES (Activity#mDestroyed is true and ObjectWatcher was watching this)
β key = 0893b148-6f03-4932-ad48-e07e204abb86
β watchDurationMillis = 336257
β retainedDurationMillis = 331257
, retainedHeapByteSize=69591)], libraryLeaks=[])
Library version 0.6.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Why using Activity context cause Context memory leak
If you use your activity as the context for something which is not tied to the life cycle of your activity and then...
Read more >Application Context, Activity Context and Memory leaks
When activity is destroyed that context also be destroyed with it and it prevents memory leaks.
Read more >International Standard Industrial Classification of All Economic ...
Its main purpose is to provide a set of activity categories that can be utilized for the collection and report- ing of statistics...
Read more >Work Activities β Repairing and Maintaining Mechanical ...
Importance Level Job Zone Code
97 86 3 49β9081.00
94 91 2 49β9044.00
94 86 3 47β4021.00
Read more >The Mechanism of Double-Strand DNA Break Repair ... - NCBI
XRCC4:DNA ligase IV is able to stimulate DNA-PKcs kinase activity (96). The ligase complex also stimulates the pol mu and lambda activities in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Was able reproduce this with the sample application. Iβve verified that Coil is calling the OkHttp
Call.cancel
API as soon as the lifecycle is destroyed, however theRequest
object is being held onto longer than needed. Going to keep looking tomorrow.For those following this ticket, Iβve been prioritizing other issues since the fix for this isnβt straightforward.
Coil
βsOkHttpClient
is holding onto the request object slightly longer than the containing lifecycle, but it will ultimately be GCβd.Going to circle back on this when some of the other issues have been fixed, but if anyone from the community has a clean way to fix this, feel free to submit a PR!