Android Question Out Of Memory Error

danijel

Active Member
Licensed User
Longtime User
hi,

How can I solve problem with Out Of Memory Error?
I think the problem is with high resolution and low RAM memory phones.

Principle of loading pictures is as follows:
For example let's initial Assets image size is 1080x1920 and i want it to be the size of the screen.
B4X:
P1.initializeSample(File.DirAssets,"p1.png", Activity.Width, Activity.Height)
There are many images that I am loading at Activity_Create.
Code works on most devices. But I noticed an error on a phone with 1GB RAM and 720 x 1280 resolution.

With this way of loading images, it loads much bigger image than it needs to be.
(because initializeSample only devides it by 2 and 1080/2 is less number than 720 and)

What am I doing wrong?
 

danijel

Active Member
Licensed User
Longtime User
I know that but then picture looks ugly (on phones with enough memory)

Is there way to see how much memory is left...?
something like this:
if LeftMemory<xxx then
load small image
else
load bigimage
End if
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Have you made the bitmap definition a global variable and are you reusing the same bitmap as opposed to using multiple bitmaps? Once they're placed on the View or however you're using them the bitmap definition can then be reused.

You can also force the App to try and use the LargeHeapSize, but that only stalls the issue from arising..
 
Upvote 0

danijel

Active Member
Licensed User
Longtime User
bitmaps are global and i using them to draw on canvas with:
DrawScaledImage(canvas1, P1, destrect.Left, destrect.Top, destrect.Right, destrect.Bottom)
'after change destrect...
DrawScaledImage(canvas1, P2, destrect.Left, destrect.Top, destrect.Right, destrect.Bottom)
'after change destrect...
DrawScaledImage(canvas1, P3, destrect.Left, destrect.Top, destrect.Right, destrect.Bottom)
...
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
hi,

How can I solve problem with Out Of Memory Error?
I think the problem is with high resolution and low RAM memory phones.

Principle of loading pictures is as follows:
For example let's initial Assets image size is 1080x1920 and i want it to be the size of the screen.
B4X:
P1.initializeSample(File.DirAssets,"p1.png", Activity.Width, Activity.Height)
There are many images that I am loading at Activity_Create.
Code works on most devices. But I noticed an error on a phone with 1GB RAM and 720 x 1280 resolution.

With this way of loading images, it loads much bigger image than it needs to be.
(because initializeSample only devides it by 2 and 1080/2 is less number than 720 and)

What am I doing wrong?
Look at the LoadScaledBitmap function of my Accelerated Surface library. It will load the images with the exact size that you need. And look also at the ReduceColors function. If your images have no transparent parts, it will reduce their size in memory by 2.
If you want to know how much memory is still available, you can use my Cache or StrictMode libraries.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
I was going to say I thought Informatix had a library which would help, but I couldn't find it whilst on the train this morning!
 
Upvote 0
Top