bumptech / bumptech/glide

OutOfMemoryError when loading a narrow image with centerCrop scaleType

Open
#4,534 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

**Glide Version**: 4.12.0
**Device/Android Version**: Samsung Galaxy S10e
**Issue details / Repro steps / Use case background**:

The crash happens each time I try to load a narrow image into a view. The image itself is not big - just 77 Kb. But it has unproportional dimensions: 5 pixels width x 5839 pixels height. Yes, generally it is a meaningless case, but receiving a wrong picture from the Internet should not crash the application with an Error from which it cannot recover.

The ImageView has "centerCrop" scale type. Glide tryes to scale the image before cropping it, and thereby it takes the whole RAM of the phone. While debugging the library, I noticed that in `Downsampler.decodeFromWrappedStreams` `expectedWidth` is set to 824100 and `expectedHeight` to 750. Thit is about 2 Gigabytes of memory for ARGB_8888 matrix! This happened because the scale-factor is large due to the small width of the image (_centerCrop_ strategy tries to align it to the view's width), and this big scale-factor is multiplied on the image height which is big.

**Glide load line**:
```java
Glide.with(fragment).load(file).into(view)
```

**Layout XML**:
```xml

```

**Stack trace**:
```ruby
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1163)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1117)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1067)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1028)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.createBitmap(LruBitmapPool.java:175)
at com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool.getDirty(LruBitmapPool.java:168)
at com.bumptech.glide.load.resource.bitmap.Downsampler.setInBitmap(Downsampler.java:884)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decodeFromWrappedStreams(Downsampler.java:375)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:248)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:207)
at com.bumptech.glide.load.resource.bitmap.Downsampler.decode(Downsampler.java:177)
at com.bumptech.glide.load.resource.bitmap.ByteBufferBitmapDecoder.decode(ByteBufferBitmapDecoder.java:31)
at com.bumptech.glide.load.resource.bitmap.ByteBufferBitmapDecoder.decode(ByteBufferBitmapDecoder.java:14)
at com.bumptech.glide.load.engine.DecodePath.decodeResourceWithList(DecodePath.java:92)
at com.bumptech.glide.load.engine.DecodePath.decodeResource(DecodePath.java:70)
at com.bumptech.glide.load.engine.DecodePath.decode(DecodePath.java:59)
at com.bumptech.glide.load.engine.LoadPath.loadWithExceptionList(LoadPath.java:76)
at com.bumptech.glide.load.engine.LoadPath.load(LoadPath.java:57)
at com.bumptech.glide.load.engine.DecodeJob.runLoadPath(DecodeJob.java:529)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromFetcher(DecodeJob.java:493)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromData(DecodeJob.java:479)
at com.bumptech.glide.load.engine.DecodeJob.decodeFromRetrievedData(DecodeJob.java:430)
at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherReady(DecodeJob.java:394)
at com.bumptech.glide.load.engine.SourceGenerator.onDataReadyInternal(SourceGenerator.java:148)
at com.bumptech.glide.load.engine.SourceGenerator$1.onDataReady(SourceGenerator.java:76)
at com.bumptech.glide.load.model.ByteBufferFileLoader$ByteBufferFetcher.loadData(ByteBufferFileLoader.java:62)
at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:311)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:280)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:235)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)
```

![NarrowImage](https://user-images.githubusercontent.com/81236159/112144740-4156a800-8be2-11eb-919f-bf37b1789c68.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.