title: Possible memory leak (Android, Glide, Activity stack)
body: <p>I am facing a very common and nasty problem as OutOfMemory exception.
  To introduce my APP, it is heavy picture content one. And I discovered, I receive a OOM at 256mb in my device (Xiaomi 5s). I cannot find the memory leak (tried <a href="https://github.com/square/leakcanary" rel="nofollow noreferrer">https://github.com/square/leakcanary</a>). My APP crashes at ~10th activity loaded. Weird thing happens with GC. Most of the memory gets freed up only if I am fully back to the start. </p>
  
  <p>For image loading I am using Glide. I changed Decoding from 8888 to 565 (default). With 8888 OOM happened faster.</p>
  
  <p>What I was thinking is just to start finishing activities from the stack. Let's say if we have 8 activities, at 9th activity open, 1st activity will be finished and so on. 
  Before doing so, I would like to hear your guys suggestions!</p>
  
  <p><strong>EDIT</strong>
  Mostly happens when trying to load a new activity and trying to load images</p>
  
  <pre><code>  java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Failed to allocate a 331212 byte allocation with 254848 free bytes and 248KB until OOM
                                                                            at java.util.concurrent.FutureTask.report(FutureTask.java:94)
                                                                            at java.util.concurrent.FutureTask.get(FutureTask.java:164)
                                                                            at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor.afterExecute(FifoPriorityThreadPoolExecutor.java:96)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1121)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                            at java.lang.Thread.run(Thread.java:818)
                                                                            at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:118)
                                                                         Caused by: java.lang.OutOfMemoryError: Failed to allocate a 331212 byte allocation with 254848 free bytes and 248KB until OOM
                                                                            at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                            at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
                                                                            at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
                                                                            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
                                                                            at com.bumptech.glide.load.resource.bitmap.Downsampler.decodeStream(Downsampler.java:329)
                                                                            at com.bumptech.glide.load.resource.bitmap.Downsampler.downsampleWithSize(Downsampler.java:220)
                                                                            at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:153)
                                                                            at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:50)
                                                                            at com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder.decode(StreamBitmapDecoder.java:19)
                                                                            at com.bumptech.glide.load.resource.bitmap.ImageVideoBitmapDecoder.decode(ImageVideoBitmapDecoder.java:39)
                                                                            at com.bumptech.glide.load.resource.bitmap.ImageVideoBitmapDecoder.decode(ImageVideoBitmapDecoder.java:20)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decodeBitmapWrapper(GifBitmapWrapperResourceDecoder.java:121)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decodeStream(GifBitmapWrapperResourceDecoder.java:94)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:71)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:61)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResourceDecoder.decode(GifBitmapWrapperResourceDecoder.java:22)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperStreamResourceDecoder.decode(GifBitmapWrapperStreamResourceDecoder.java:24)
                                                                            at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperStreamResourceDecoder.decode(GifBitmapWrapperStreamResourceDecoder.java:14)
                                                                            at com.bumptech.glide.load.resource.file.FileToStreamDecoder.decode(FileToStreamDecoder.java:39)
                                                                            at com.bumptech.glide.load.resource.file.FileToStreamDecoder.decode(FileToStreamDecoder.java:17)
                                                                            at com.bumptech.glide.load.engine.DecodeJob.loadFromCache(DecodeJob.java:222)
                                                                            at com.bumptech.glide.load.engine.DecodeJob.decodeResultFromCache(DecodeJob.java:85)
                                                                            at com.bumptech.glide.load.engine.EngineRunnable.decodeFromCache(EngineRunnable.java:108)
                                                                            at com.bumptech.glide.load.engine.EngineRunnable.decode(EngineRunnable.java:99)
                                                                            at com.bumptech.glide.load.engine.EngineRunnable.run(EngineRunnable.java:58)
                                                                            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                            at java.lang.Thread.run(Thread.java:818) 
                                                                            at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:118) 
  </code></pre>
  
  <p>Can happen also when opening an activity while memory is almost dead.</p>
  
  <p>What I was thinking that it is correct, that while opening a new activity, old one stays in memory with images loaded -> which means that memory is being used. But opening only 10 activities is rather little.. Image sizes are 40-100kb. Normally in the activity are >20 images.</p>
  
  <p>I also use lots of programmically created views as well as custom adapters (APP is rather big, we discovered this OOM issue just recently). I was trying to play around with passing different contexts into Glide/ImageViews, but memory usage didn't seem to change.</p>
  
  <p><strong>EDIT2</strong>
  <a href="https://i.stack.imgur.com/9kpUU.png" rel="nofollow noreferrer">Heap dump at 178mb</a></p>