How to solve out of memory exception on orientation changes?

corwin42

Expert
Licensed User
Longtime User
I use many imageviews on a scrollview. The bitmaps are loaded with LoadBitmapSample().

The problem I have is that the app crashes after approximately 10 orientation changes. The issue is discussed many times on different forums and a very good explanation can be found here.

The only real solution seems to be to call recycle() on all used bitmaps (even the ones used in ImageViews) in the onDestroy() method. The problem is that we don't have an equivalent to the onDestroy() method in B4A.

So my question is: Is there any solution how we can handle this issue in B4A?
 

corwin42

Expert
Licensed User
Longtime User
I do this already with nearly all images i load manually. I will expand this method to other bitmaps now, too (action bar icons etc). I think I even will remove static images in layout files and set them manually from the cache.

The problem with this solution is that then the bitmaps will never be freed again, even if they are not needed anymore. So perhaps I have to add an intelligent algorithm to the cache that removes bitmaps if they were not requested for a longer time.

Ok, will play with it.

Gesendet von meinem LG-P500 mit Tapatalk 2
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Removing an image from the cache list will free its memory (assuming that it is not used).

Not really. Thats the problem with this out of memory error. The GC will not free this memory but only the finalizer. So if the system is low on memory and the GC tries to free some more space the bitmaps probably will not be freed. They will only be freed immediately if you call BitMap.recycle(). See the link in my first post for a really good explanation about this problem.

Most developers think they can solve this problem with manually calling the GC but this is not true. It may help a bit but will not solve the problem.
 
Upvote 0

DevBaby

Active Member
Licensed User
Longtime User
Yes. Load the images only once and store them in a process global list. Once they are loaded you should fetch the images from the list instead of loading them again.

How is this done? Is there a variable type i can declare in the process global section and then load the images there?

I am not at my B4A at the moment to see.
 
Upvote 0

DevBaby

Active Member
Licensed User
Longtime User
Something like:
B4X:
Sub Process_Globals
 Dim Image1, Image2 As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
 If FirstTime Then
  Image1 = LoadBitmap(...)
  Image2 = LoadBitmap(...)
 End If
 ImageView1.Bitmap = Image1

Thanks Erel,

One more question on this...

Would it still be beneficial to use LoadBitmapsample using the dimensions (for examplem, 20 X 20) of "ImageView1"? or would I resize both Image1 and image2 to these dimensions and then use LoadBitmapsample?
 
Upvote 0

DevBaby

Active Member
Licensed User
Longtime User
Thanks Erel,

This seems to have solved my longstanding problem with the app failing on a second call of Activity_Create.
 
Upvote 0
Top