1

I have an activity that uses recyclerView and for each item's view there is an Image.enter image description here

I have used Garbage Collector GC() on Destroy as follows

recyclerView= null; 
                 adapter=null;
                 Runtime.getRuntime().gc();

But the following thing happens while releasing memoryenter image description here


And When I start another activity that loads images from a remote host using Picasso It says
java.lang.OutOfMemoryError: Failed to allocate a 94784012 byte allocation with 4194304 free bytes and 87MB until OOM

I found an answer that works for me to overcome that OutOfMemoryError by Increasing heap size for application But I still want to release and ensure that memory occupied by an Activity is released instead of going to increase heap size for app.Thanks in advance , please help to do that task.

Mashhood Qadeer
  • 482
  • 8
  • 19
  • Read this `https://android.jlelse.eu/memory-leak-patterns-in-android-4741a7fcb570` – Nirmal Prajapat Mar 19 '18 at 08:59
  • Sir! as it is mentioned (2)Traverses all object references in memory from GC roots and marks active objects which has references from GC roots. on referred page then It should manage activity as I have placed recyclerview=null in OnDestroy and Invoked GC to collect it. – Mashhood Qadeer Mar 19 '18 at 09:04

2 Answers2

4

The code that you are using in your onDestroy method is not needed there. If destroy is called your acitivity will be removed from the stack and is free for gc anyway with all the resources in it that are only referenced by the activity.

OnDestroy doesn't always get called that's why your method may not be called at all. You could try and explicitly call for finish() in activity then onDestroy will be called and see how the situation will change.But then the activity will be removed from stack.

Also to call for gc manually is supposed to be bad style. On Android the system nearly always knows when it is the best time to do a garbage collection. Most of the times an activity finishes garbage collection is triggered automatically.

I would look into resizing images you get from Picasso first as they just could be too big in general for you heap even if there is space.

Look into resizing options link

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • Sir as you say that OnDestroy is not called always but I have placed a Log in it and it appears on when activity is destroyed and When we press back then activity is finished implicitly. – Mashhood Qadeer Mar 19 '18 at 09:20
  • ok, if you logged that ondestroy is called so i suggest you look into the size of pictures you load ,(besides of course increasing the heap size as the last option) . Maybe there'll be some other opinions here – Rainmaker Mar 19 '18 at 09:22
  • Thanks for giving suggestion for resizing images using Picasso But I am still interesting in to release memory used by First activity. I already have a solution for Picasso by Invalidating files to prevent cache and managing Network cache etc but I want to release memory used by First Activity that is getting image resources from draw able resource id. – Mashhood Qadeer Mar 19 '18 at 09:27
1

Seems the problem is related to huge images, try to not keep images in memory, load images only on demand(with some cache strategy)

Try to replace picasso with glide. Glide provides better memory performance https://medium.com/@multidots/glide-vs-picasso-930eed42b81d

Try to load all your images with closest context https://stackoverflow.com/a/32887693/6193843

Link182
  • 733
  • 6
  • 15