Android,使用带有Glide库的BitmapPool

时间:2022-01-24 21:21:48

I'm using the glide library in my app for displaying bitmap (from asset and from url). It works well but I get somme Out of memomy issues as I'm displaying a lot of images in each activities. I see I can use the clearMemory() from BitmapPool, but I have no clue how to call it..

我在我的应用程序中使用滑动库来显示位图(来自资产和来自url)。它运作良好但我得到了回忆问题,因为我在每个活动中都显示了很多图像。我看到我可以使用BitmapPool中的clearMemory(),但我不知道如何调用它。

Is someone know how to call it?

有人知道怎么称呼它吗?

Thanks

谢谢

1 个解决方案

#1


5  

You can use clearMemory() or trimMemory() to clear both Glide's memory cache and bitmap pool:

您可以使用clearMemory()或trimMemory()来清除Glide的内存缓存和位图池:

Glide.get(context).clearMemory()
// or:
Glide.get(context).trimMemory(ComponentCallbacks2.TRIM_MEMORY_MODERATE);

That said, you shouldn't need to do either. Two things to check:

那就是说,你不应该这样做。要检查两件事:

  1. Are you loading multiple large images? If so, you need to make sure they are downsampled, so consider setting an explicit size on the View you're loading the image into or using Glide's override() API on your request. Doing so in conjunction with a Transformation like fitCenter will help reduce the memory used per image.
  2. 你在加载多个大图像吗?如果是这样,您需要确保它们是下采样的,因此请考虑在您正在加载图像的视图中设置显式大小,或者在您的请求中使用Glide的override()API。与像fitCenter这样的转换一起使用将有助于减少每个图像使用的内存。
  3. Are there memory leaks in your application? Although Bitmap allocation often throws the actual OutOfMemoryException, most of the time the root cause is a memory leak elsewhere in the application. If #1 doesn't solve your problem, try capturing a heap dump and checking for leaks. The Android developer pages has a great tutorial on how to do so.
  4. 您的应用程序中是否存在内存泄漏?虽然Bitmap分配经常抛出实际的OutOfMemoryException,但大多数时候根本原因是应用程序中其他地方的内存泄漏。如果#1无法解决您的问题,请尝试捕获堆转储并检查泄漏。 Android开发人员页面有一个很好的教程,如何这样做。

#1


5  

You can use clearMemory() or trimMemory() to clear both Glide's memory cache and bitmap pool:

您可以使用clearMemory()或trimMemory()来清除Glide的内存缓存和位图池:

Glide.get(context).clearMemory()
// or:
Glide.get(context).trimMemory(ComponentCallbacks2.TRIM_MEMORY_MODERATE);

That said, you shouldn't need to do either. Two things to check:

那就是说,你不应该这样做。要检查两件事:

  1. Are you loading multiple large images? If so, you need to make sure they are downsampled, so consider setting an explicit size on the View you're loading the image into or using Glide's override() API on your request. Doing so in conjunction with a Transformation like fitCenter will help reduce the memory used per image.
  2. 你在加载多个大图像吗?如果是这样,您需要确保它们是下采样的,因此请考虑在您正在加载图像的视图中设置显式大小,或者在您的请求中使用Glide的override()API。与像fitCenter这样的转换一起使用将有助于减少每个图像使用的内存。
  3. Are there memory leaks in your application? Although Bitmap allocation often throws the actual OutOfMemoryException, most of the time the root cause is a memory leak elsewhere in the application. If #1 doesn't solve your problem, try capturing a heap dump and checking for leaks. The Android developer pages has a great tutorial on how to do so.
  4. 您的应用程序中是否存在内存泄漏?虽然Bitmap分配经常抛出实际的OutOfMemoryException,但大多数时候根本原因是应用程序中其他地方的内存泄漏。如果#1无法解决您的问题,请尝试捕获堆转储并检查泄漏。 Android开发人员页面有一个很好的教程,如何这样做。