Glide Android内存泄漏 [英] Android memory leak with Glide

查看:2222
本文介绍了Glide Android内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,该活动可以滑动将图像加载到ImageViews中.这是我的滑行代码示例:

    Glide.with(ImageVOne.getContext())
            .load(geoInfo.getPhotoUrl1())
            .skipMemoryCache(true)
            .priority(Priority.NORMAL)
            .into(ImageVOne);

我加载1到35张图片,每张图片应在150ko&之间; 250ko.我不能减少这一点.

在一个活动中,可以从主要活动中多次访问此活动,并且每次加载不同的图片时都可以访问该活动.例如,第一次是华盛顿的照片,然后是伦敦的照片.

我的问题是,每次启动加载图片的活动时,内存的使用都会大量增加:

我可以将活动开始3到5次,然后应用崩溃.错误消息是

java.lang.OutOfMemoryError: Failed to allocate a 1411340 byte allocation with 1126320 free bytes and 1099KB until OOM

我阅读了有关内存泄漏的帖子但我认为Glide可以避免此问题.我的图片活动在另一张图片开始之前已经完成,但是分配给我的应用程序的内存似乎没有减少. 我还在清单中的图片活动中添加了android:noHistory="true",但它没有任何改变.

我在清单中添加了android:largeHeap="true",但是它只是推迟了我的问题(我可以将图片活动启动约10到15次),并且在应用崩溃之前,我的imageviews中没有加载很多图片,因此不是对我来说是一个很好的解决方案.

当我使用glide时,我还尝试添加.skipMemoryCache(true),但是我没有发现任何变化.

我猜我每次从图片活动转到主要活动时,内存使用"都应该减少,然后当我用新图片再次开始我的图片活动时,我的内存使用量"应该增加.但是从我在蓝色图表上看到的来看,它几乎只会增加.你知道我该怎么办吗?

解决方案

您可以采取多种措施来防止出现内存不足错误.它们如下.

  1. 使用GridView/RecycleView显示图像.因为它们仅加载显示的内容.假设您有50张图像,并且屏幕上可见10张图像,那么它将仅加载10张图像.这将减轻内存压力.

  2. 使用 PLACEHOLDER 加载图像而不是空白.您可以在drawable中使用低分辨率图像作为占位符.

  3. 使用缩略图代替实际图像.

  4. 您可以对imageView的高度和宽度使用固定的dp.

  5. 将skipMemoryCache设置为true.

  6. 清除 GLIDE内存onDestroy();

    @Override public void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
    }
    

  7. 覆盖为更小的尺寸:

     .override(500, 600) //as example
    

    以下是使用GLIDE的精确代码:

       Glide.with(this)
            .load(url)
            .thumbnail(0.5f)
            .skipMemoryCache(true) 
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.your_placeHolder)
            .into(imageVOne);
    

您可以在此处.

I have an activity which loads pictures in ImageViews with glide. Here is a sample of my glide code:

    Glide.with(ImageVOne.getContext())
            .load(geoInfo.getPhotoUrl1())
            .skipMemoryCache(true)
            .priority(Priority.NORMAL)
            .into(ImageVOne);

I load from 1 to 35 pictures, each picture should be between 150ko & 250ko. I cannot reduce that.

This activity can be accessed several times in a session from the main activity, and each time it loads different pictures. For example the first time it will be pictures of Washington, then pictures of London etc.

My issue is that the use of memory increases a lot every time the activity that loads the pictures is started:

I can start the activity from 3 to 5 times, then the app crashes. The error message is

java.lang.OutOfMemoryError: Failed to allocate a 1411340 byte allocation with 1126320 free bytes and 1099KB until OOM

I read posts about memory leaks but I thought Glide would avoid this issue. My activity with the pictures is finished before another one is started, but the memory allocated to my app do not seem to drop. I also added android:noHistory="true" to my picture activity in the Manifest but it doesn't change anything.

I added android:largeHeap="true" in my Manifest but it just postpone my issue (I can start the pictures activity about 10 to 15 times) and I get a lot of pictures not loaded in my imageviews before the app crashes, so its not a good solution for me.

I also tried to add .skipMemoryCache (true) when I use glide but I don't notice any change.

I guess my "memory use" should decrease every time I go from the pictures activity to the main activity, then increase when I start my pictures activity again with new pictures. But from what I see on the blue graph it almost only increases. Do you see what I should do?

解决方案

You can take several measures to prevent getting Out of Memory Error. They are as follows.

  1. Using GridView/RecycleView to show images. Because they load only what they show. Suppose you have 50 images and 10 images are visible to your screen, it will load only 10. This will ease the pressure from your memory.

  2. Use PLACEHOLDER to load image instead of black-space. You can use low resolution image in drawable as placeholder.

  3. Use THUMBNAILS instead of actual images.

  4. You may use fixed dp for height and width of imageView.

  5. Set skipMemoryCache to true.

  6. CLEAR GLIDE memory onDestroy();

    @Override public void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
    }
    

  7. Override to smaller-size :

     .override(500, 600) //as example
    

    Here is a refined code for using GLIDE:

       Glide.with(this)
            .load(url)
            .thumbnail(0.5f)
            .skipMemoryCache(true) 
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.your_placeHolder)
            .into(imageVOne);
    

You may look at catching mechanism of Glide here.

这篇关于Glide Android内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆