Summary of Glide OOM problem solutions

1. Introduce the largeHeap attribute to allow the system to allocate more independent memory for the App.
2. Disable Glide memory cache. Set skipMemoryCache(true).
3. Customize GlideModule. Set the MemoryCache and BitmapPool size.
4. Upgrade to Glide4.0, use asDrawable instead of asBitmap, drawable saves more memory.
5. When the scaleType of ImageView is fitXY, it is changed to fitCenter/centerCrop/fitStart/fitEnd display.
6. Do not use application as context. When the context is application, the life cycle of the imageView will be extended to the entire running process, and the imageView cannot be recycled, resulting in an OOM exception.
7. Use application as context. But use weak reference or soft reference for ImageView, try to use SoftReference, when memory is insufficient, useless ImageView will be recycled in time.
8. When the list is sliding, call Glide's pauseRequests() to cancel the request, and when the sliding stops, call resumeRequests() to resume the request.
9. Try catch some large memory allocation operations. Consider trying a degraded memory allocation operation inside the catch. For example, when decoding bitmap, catch to OOM, you can try to double the sampling ratio and try to decode again.
10. BitmapFactory.Options and BitmapFactory.decodeStream obtain the width and height of the original image, bypass the Java layer to load the Bitmap, and then call the override(width, height) of Glide to control the display.
11. Partial loading of pictures. Reference: SubsamplingScaleImageView, first download the image to the local, then load it, only load the current visible area, and then load another area when the finger is dragged.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325624192&siteId=291194637