Android Question java.lang.Exception: java.lang.OutOfMemoryError

AHilberink

Active Member
Licensed User
Longtime User
Hi,

For the first time I got this error message from an user:
B4X:
java.lang.Exception: java.lang.OutOfMemoryError: Failed to allocate a 24 byte allocation with 141952 free bytes and 138KB until OOM, target footprint 201326592, growth limit 201326592; failed due to fragmentation

Doing the same on my tablet, it works. Same memory, same brand, only older TabA7 (android 11), user has TabA8 (12)

I have searched for memory management and I found this as a Manifest setting:
B4X:
SetApplicationAttribute(android:largeHeap, "true")

Some questions:
- Will this setting helps on the tablet of the user?
- Google does advise to not use this setting, so what to do?
- This is not an Pages project. Will Pages has another memory management and should I do a migration?
- The error exists mostly on images. This is not an image project. Just CLV and text. I do use png on buttons. These are about 750bytes each. Will a lower quality has effect?

Thanks for helping me.

Kind regards,
André
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4XPages will not change anything in this case. Setting largeHeap declaration might help.

Best solution though is to optimize the memory usage. Are you loading the same bitmap multiple times? Are you loading all bitmaps with XUI.LoadBitmapResize?

The image file size doesn't really matter. It is the image actual size (width x height) that matters.
 
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Hi Erel,

The setting for largeHeap has solved the problem for the user for now, but I am affraid for the future. It is a big project (apk size 13Mb) and is still growing to meet our users expectations.

I do load the bitmaps as follow:
B4X:
    btnOpdracht.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"opdracht.png",btnMessages.Height-3dip,btnMessages.Height-3dip,True)).Gravity=Gravity.CENTER_HORIZONTAL+Gravity.TOP
    btnSysteem.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"instellingen.png",btnMessages.Height-3dip,btnMessages.Height-3dip,True)).Gravity=Gravity.CENTER_HORIZONTAL+Gravity.TOP
    btnExit.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"sluiten.png",btnMessages.Height-3dip,btnMessages.Height-3dip,True)).Gravity=Gravity.CENTER_HORIZONTAL+Gravity.TOP
    btnUren.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"uren.png",btnMessages.Height-3dip,btnMessages.Height-3dip,True)).Gravity=Gravity.CENTER_HORIZONTAL+Gravity.TOP
    btnTel.SetBackgroundImage(LoadBitmapResize(File.DirAssets,"kantoor.png",btnMessages.Height-3dip,btnMessages.Height-3dip,True)).Gravity=Gravity.CENTER_HORIZONTAL+Gravity.TOP

I will change these to XUI.LoadBitmapResize and I will check to load some only once (if there are any).

The other images are with ImageView and selected from the Files list within the Designer. Is there also something I can do to reduce memory consumption? Will LoadBitmapResize helps?

Kind regards,
André
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…