Android memory leak with Glide

前端 未结 3 1838
北海茫月
北海茫月 2021-02-09 01:40

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

    Glide.with(ImageVOne.getContext())
            .load(geo         


        
相关标签:
3条回答
  • 2021-02-09 01:55

    Here is a sample of my glide code : Glide.with(ImageVOne.getContext()) .load(geoInfo.getPhotoUrl1()) .skipMemoryCache(true) .priority(Priority.NORMAL) .into(ImageVOne);

    Your problem is most likely stemming from the this call: Glide.with(ImageVOne.getContext()).... You don't want to do this because View.getContext() returns the Application Context. By using Glide with the Application Context you're telling Glide to follow the application's lifecycle and not your Activity's lifecycle, resulting in the Glide image loads never being cancelled or cleared when you back out of said Activity.

    So instead use: Glide.with(MyActivity.this)....

    Note: Always tie your Glide load requests to the nearest possible lifecycle to ensure that Glide follows the lifecycle that it's being used in

    0 讨论(0)
  • 2021-02-09 02:08

    Using Glide would not be the issue of memory leak, you might keep some other reference of your activity like listener or you forgot to unregister something that have been registered while starting the activity which will results your whole activity cant get Garbage collected.

    So every time you starting your activity or fragment it will create new instance while the old instance also keep in memory because of any unregistered culprit Instance.

    use Eclipse MAT to find your leak.

    0 讨论(0)
  • 2021-02-09 02:16

    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.

    0 讨论(0)
提交回复
热议问题