Android Question For those who are struggeling with OUTOFMEMORY error

Mrjoey

Active Member
Licensed User
Hello , i would like to share my experience with this error since i was struggeling with it too much without finding a best solution for it.
Im developing a heavy project (Music Player) with a graphical ui , more than 7 activities , 3 services and more, each activity has a background image , has more than 1 layout added to panels , each layout is designed with the designer and has also bitmaps loaded to buttons and panels , so when loading the app with default heap size (64mb) the app crashes due to outofmemory since the activities are loaded more than once and each time a activity is loaded its layout is also loaded wish has bitmaps and those bitmaps are also loaded more than once and still exist in the memory , also in my code im loading bitmaps added to views as background using
B4X:
Loadbitmap(file.dirassets,....)
Now i ve noticed that this call will load the bitmap into memory but dont release it even if the activity was finished and when i load the activity once again the memory grows and calling
B4X:
Bmp = null
is not efficient for me since i have lots of them.
Notice that im monitoring my app with MAT.
Now what i did is this :
Dimming all bitmaps that i have to use in the proccess global.
In the activity create i load them when the Firsttime is true.
And then share those bitmaps with all activities.
Now the bitmaps are loaded once and remain in the memory even if the Main activity was paused , so with that stratigy my app became healthy since my last memory check was over 160mb (heap = true). Now my app is using around 40mb with all activities loaded.
I think Mr Erel has mentioned this stratigy but no one has consider it.
So if u have views and u want to load a bitmap on it dont use Loadbitmap , just use the already loaded bitmap , btw i noticed also that lets say
B4X:
Activity.setbackgroung(bmp)
Did not grows the memory and that was a advantage , i thing thats how android works.

Now i have something to say to Mr @Erel : as im loyal to B4A and as i noticed some changes in the designer , i would suggest to make a better solution in the designer in the next update when loading bitmaps for the activity or views like panel background , buttons drawables , if could find a way when loading thoses bitmap to recycle them for the next load.
I hope my experience will help u and u could now revise ur code and remain the memory happy , thank u , Joe.
 

KMatle

Expert
Licensed User
Hi Joe,

there are tons of apps like music players using a lot of images without going out of memory. I don't know the design of your app but I really think there are other ways to show images. What about a database?, reduce the imagesize, don't load "all the images", remove and re-create them on runtime, etc.

How many images (in whiche size) are we talking about?
 

Mrjoey

Active Member
Licensed User
hey @KMatle for images (cover arts) im using the B4AGallery , im sure u heard about it , im loading a list array of images and it loads each bitmap once and recycle it , so im not afraid using this library , and for databases , yes im using lots of sqls but im not saving images into it , btw im not here talking about imageviews cz there is no problem with them , the only problem for the memory is the bitmaps , eventhough if u null the bitmap of that imageviews nothing happens , and the bitmaps still exist in the memory and i forgot to mention to put this line :
B4X:
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
dim RF as Reflector
RF.RunStaticMethod("java.lang.System", "gc", Null, Null)
Activity.Finish
End If
End sub
 

Cableguy

Expert
Licensed User
Load all imagens needed into an array on APP Start.. This way you'll only load them into memory ONCE, and then call each bitmap by its array position.
 

Cableguy

Expert
Licensed User
Another thing to do is, after creating your layout in the designer, blank out all image file references to avoid them being loaded with the layout.
This, along with an image array to load all "fixed" ui images will improve your memory management.
 

Informatix

Expert
Licensed User
hey @KMatle for images (cover arts) im using the B4AGallery , im sure u heard about it , im loading a list array of images and it loads each bitmap once and recycle it , so im not afraid using this library , and for databases , yes im using lots of sqls but im not saving images into it , btw im not here talking about imageviews cz there is no problem with them , the only problem for the memory is the bitmaps , eventhough if u null the bitmap of that imageviews nothing happens , and the bitmaps still exist in the memory and i forgot to mention to put this line :
B4X:
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
dim RF as Reflector
RF.RunStaticMethod("java.lang.System", "gc", Null, Null)
Activity.Finish
End If
End sub
This subject has been discussed one million times in this forum and I explained many times how memory works under Android, why calling GC or the Recycle function does not help, why LoadBitmap should be avoided, etc. So it is very discouraging to read this thread (except the first advice: reusing bitmap variables). Personnaly I stop contributing on this matter. There's a search function in this forum; please use it.
 

sorex

Expert
Licensed User
is there an actual difference between loadbitmap & initialize to load an image ?
 

WAZUMBi

Well-Known Member
Licensed User
Now the bitmaps are loaded once and remain in the memory even if the Main activity was paused , so with that stratigy my app became healthy since my last memory check was over 160mb (heap = true). Now my app is using around 40mb with all activities loaded.
holy_cannoli_batman_by_zelda_hylainprincess-d3vpag6.jpg
 
Top