Android Question How to detect a OutOf MemoryError ?

antonomase

Active Member
Licensed User
Longtime User
Hi,

One of my users send me the following error when he use my application. I do not have this error on my tablet.

How can I know where the error come from ?

I have an idea : the application uses 5 panels wich fill all the screen : the user can switch from one to another one by buttons.
One of these panels uses a lot of bitmaps. It has no interaction with the other panels
How can I replace something like
B4X:
pnl1.Visible = false
pnlBitmap.Visible = true
by open the panel in an other process ?
 

Attachments

  • Screenshot_2013-12-01-20-56-33.png
    Screenshot_2013-12-01-20-56-33.png
    41.6 KB · Views: 180

antonomase

Active Member
Licensed User
Longtime User
No. I will try it.
But bitmaps are jpeg files about 40-50 Kb.
Can the problem occurs if these files are larger than the screen ?
 
Upvote 0

antonomase

Active Member
Licensed User
Longtime User
The larger image is 1280x800. Even on a tablet 7", it is not a very large image.
I'm not sure that the problem comes from these images. But with such an error message (Bitmap outofMemory, line -2) it is difficult to test and debug.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
What's the memory consumption of your app (use any free tool available on Google Play to get an idea or, better, use the Monitor tool provided with the SDK)? What's exactly the memory taken by your images (you can compute this with the formula width*height*4)? What's the maximum Java heap size of the user's device? With these 3 numbers, you should have a strong hint.
 
Upvote 0

antonomase

Active Member
Licensed User
Longtime User
The memory used by the application is from 110 to 190 Mb.

I use
1 image for the splash screen : 1000x625 for tablets 10" and 800x500 for the tablets 7"
7 icons file png : 5 in 64x64, 1 in 40x40 and 1 in 32x32
1 image png : 256 x 256

There is a slideshow in the application : the default files are 1 file 1280x800 and 1 file 512x597. They are loaded one by one.

Plus the clsWheel (One time, i had a problem of memory with it. But just one time)

The biggest file is a background image 1760x2414 which is resized. I will try to resample it.


I don't know how to get the Java heap size.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
The memory used by the application is from 110 to 190 Mb.

Unless you set LargeHeap to true in your manifest, that's too much for most devices. And even LargeHeap is not the ultimate solution because this setting is ignored before Honeycomb and some devices (I have one in this case) won't allocate more than 128 MB to the large heap (especially if their total RAM is 256 Mb or less). So you should try to reduce your memory consumption under 128 MB if your application is not designed for a specific device.

I use
1 image for the splash screen : 1000x625 for tablets 10" and 800x500 for the tablets 7"
7 icons file png : 5 in 64x64, 1 in 40x40 and 1 in 32x32
1 image png : 256 x 256

There is a slideshow in the application : the default files are 1 file 1280x800 and 1 file 512x597. They are loaded one by one.

I think the main culprit is your slideshow. You probably do not remove the images from memory once they are seen, so the memory usage grows with each new image. I cannot give more details without knowing how you display and handle your images.

The biggest file is a background image 1760x2414 which is resized. I will try to resample it.

Resampling is not enough, you have to rescale it. You can use the LoadScaledBitmap function of my BetterImageView lib.

FYI, this image alone consumes 16 MB of RAM.

I don't know how to get the Java heap size.

You just have to know what the device is. The max VM heap size is publicly available in the specs. Otherwise, you can use my Cache lib or my StrictMode lib to know the max. available memory when you start your app.
 
  • Like
Reactions: eps
Upvote 0

antonomase

Active Member
Licensed User
Longtime User
So you should try to reduce your memory consumption under 128 MB if your application is not designed for a specific device.
I'm OK : the application use a lot of memory.


I will move all the large pictures currently into the application to the disk and load them when needed.
But it will be a problem if the user erases some of these files.

Question : If a file is loaded into an imageview (imageview.bitmap = loadBitmap (...)), is it possible to restore memory space with imageview.bitmap = null after use ?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Question : If a file is loaded into an imageview (imageview.bitmap = loadBitmap (...)), is it possible to restore memory space with imageview.bitmap = null after use ?
You should load your bitmaps into the ImageView with LoadBitmapSample or, better, LoadScaledBitmap (with a BetterImageView). And to clear the memory, set Bitmap to null indeed. It's not immediate because the memory is cleared asynchronously by the garbage collector, but it's the proper way to do it.
 
Upvote 0

antonomase

Active Member
Licensed User
Longtime User
Another question : to export files to the "disk", I do
B4X:
  If File.Exists(File.DirDefaultExternal, "img1.png") = False Then
     File.Copy(File.DirAssets, "img1.png", File.DirDefaultExternal, "img1.png")
   End If
So before to be in DirDefaultExternal, images are in DirAssets. Can this cause an OutOfMemory error ?
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Another question : to export files to the "disk", I do
B4X:
  If File.Exists(File.DirDefaultExternal, "img1.png") = False Then
     File.Copy(File.DirAssets, "img1.png", File.DirDefaultExternal, "img1.png")
   End If
So before to be in DirDefaultExternal, images are in DirAssets. Can this cause an OutOfMemory error ?
I don't think so. I didn't look at the Copy source code but I would be very surprised if it does not use a stream (so its memory consumption is a few KB whatever the file size may be).
 
Upvote 0

antonomase

Active Member
Licensed User
Longtime User
Thanks.
I will try all of this and I will see with that user if it has always problem.

See you again in a few days.
 
Upvote 0
Top