Android Question Out of memory, expecially with maps

FrankBerra

Active Member
Licensed User
Longtime User
Hi all
My app is experiencing a lot of crashes on some devices due to out-of-memory error.
It is appearing sometimes ad especially when loading maps.
I can't deal with this out-of-memory error and i can't understand where and how to solve this problem.
My app opens some panels, and loads some images (about 10 images) with LoadBitMapSample. When i show also a map it crashes randomly e more frequently with errors like the following ones:

B4X:
I/dalvikvm-heap(28148): Clamp target GC heap from 66.376MB to 64.000MB
D/dalvikvm(28148): GC_FOR_ALLOC freed 84K, 11% free 58531K/65536K, paused 28ms, total 28ms
I/dalvikvm-heap(28148): Forcing collection of SoftReferences for 3240016-byte allocation
I/dalvikvm-heap(28148): Clamp target GC heap from 66.376MB to 64.000MB
D/dalvikvm(28148): GC_BEFORE_OOM freed 0K, 11% free 58531K/65536K, paused 35ms, total 35ms
E/dalvikvm-heap(28148): Out of memory on a 3240016-byte allocation.
I/dalvikvm(28148): "main" prio=5 tid=1 RUNNABLE
I/dalvikvm(28148):   | group="main" sCount=0 dsCount=0 obj=0x418ff5a8 self=0x401436a8
I/dalvikvm(28148):   | sysTid=28148 nice=0 sched=0/0 cgrp=apps handle=1075138556
I/dalvikvm(28148):   | state=R schedstat=( 19914758871 17748812518 35060 ) utm=1679 stm=311 core=3
I/dalvikvm(28148):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
I/dalvikvm(28148):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
I/dalvikvm(28148):   at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.InitializeSample(CanvasWrapper.java:550)
I/dalvikvm(28148):   at anywheresoftware.b4a.keywords.Common.LoadBitmapSample(Common.java:1172)
I/dalvikvm(28148):   at test.app.main._caricaprofiloutentetrovatodacache(main.java:2000)
I/dalvikvm(28148):   at test.app.main._visualizzaprofiloutentetrovatodacontatti(main.java:8084)
I/dalvikvm(28148):   at test.app.main._pannelliutentiinlista_touch(main.java:5658)
I/dalvikvm(28148):   at java.lang.reflect.Method.invokeNative(Native Method)
I/dalvikvm(28148):   at java.lang.reflect.Method.invoke(Method.java:525)
I/dalvikvm(28148):   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
I/dalvikvm(28148):   at anywheresoftware.b4a.BA$1.run(BA.java:293)
I/dalvikvm(28148):   at android.os.Handler.handleCallback(Handler.java:730)
I/dalvikvm(28148):   at android.os.Handler.dispatchMessage(Handler.java:92)
I/dalvikvm(28148):   at android.os.Looper.loop(Looper.java:176)
I/dalvikvm(28148):   at android.app.ActivityThread.main(ActivityThread.java:5419)
I/dalvikvm(28148):   at java.lang.reflect.Method.invokeNative(Native Method)
I/dalvikvm(28148):   at java.lang.reflect.Method.invoke(Method.java:525)
I/dalvikvm(28148):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
I/dalvikvm(28148):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
I/dalvikvm(28148):   at dalvik.system.NativeStart.main(Native Method)
I/dalvikvm(28148): 
D/skia    (28148): --- decoder->decode returned false
I/dalvikvm-heap(28148): Clamp target GC heap from 66.357MB to 64.000MB
D/dalvikvm(28148): GC_EXPLICIT freed 20K, 11% free 58511K/65536K, paused 2ms+5ms, total 40ms
I/B4A     (28148): Downsampling image due to lack of memory: 2
I/Choreographer(28148): Skipped 186 frames!  The application may be doing too much work on its main thread.

My thoughts are: why my "simple" app crashes so frequently and others like Facebook, Google plus, Instagram etc. that are more heavy than mine doesn't crash at all?
Where I am failing in programming?
Anyone that suffered from the same problem can me point in the right direction?

Thanks!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Should i worry about?
In most cases no (unless you get an out of memory exception). The GC will free the memory when it needs to.

If i close a panel (but not the app) holding images should i "Nullify" the imageViews to free resources?
What do you mean with closing a panel? If there are no live references to the panel (you cannot reach it) then the memory of the panel and all its child views will be eventually released.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Nobody has mentioned the Bitmap recycle method yet...
Free the native object associated with this bitmap, and clear the reference to the pixel data.
This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references.
The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing.
This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap.
This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

As the quote says, this method need not normally be called.
But the method is available and may help in some circumstances.

Managing Bitmap Memory

forum search for 'bitmap recycle'
 
Upvote 0
Top