Android Question OutOfMemoryError

pantaraf

Member
Licensed User
Longtime User
Hi,
I'm starting to debug my app, when running in Release after using the app for a while switching between the different activities it crashes always with the following exception:

B4X:
java.lang.OutOfMemoryError
   at android.graphics.Bitmap.nativeCreate(Native Method)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
   at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
   at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:76)
   at com.pantasoft.ApiDroid.textscaler._scaletext(textscaler.java:42)
   at com.pantasoft.ApiDroid.arnie._activity_create(arnie.java:373)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
   at com.pantasoft.ApiDroid.arnie.afterFirstLayout(arnie.java:98)
   at com.pantasoft.ApiDroid.arnie.access$100(arnie.java:16)
   at com.pantasoft.ApiDroid.arnie$WaitForLayout.run(arnie.java:76)
   at android.os.Handler.handleCallback(Handler.java:605)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:137)
   at android.app.ActivityThread.main(ActivityThread.java:4424)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:511)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   at dalvik.system.NativeStart.main(Native Method)
All the bitmaps are loaded using LoadBitmapSample.
Things went a little bit better after I've put a call to Activity.RemoveAllViews on each activity Pause event.
This happens on three different devices (Asus eeePad Transformer, LG L5, Google Nexus 7).
The app is far from being stable. Any hint from which I can start to investigate?
This crash doesn't happen at all while running in Debug mode.

Thanks.
Raffaele
 

pantaraf

Member
Licensed User
Longtime User
B4X:
Sub ScaleText(btn As Button, V As View)
  Dim btnCanvas As Canvas
  Dim testo As Float
  Dim fontsize As Int
   
  btnCanvas.Initialize(V)
  testo=btnCanvas.MeasureStringWidth(btn.Text,Typeface.SANS_SERIF,btn.TextSize)
  fontsize=btn.TextSize
  Do While(testo<(btn.Width/1.76))
  fontsize=fontsize+1
    testo=btnCanvas.MeasureStringWidth(btn.text,Typeface.SANS_SERIF,fontsize)
  Loop
  Do While(testo>(btn.Width/1.76))
  fontsize=fontsize-1
    testo=btnCanvas.MeasureStringWidth(btn.text,Typeface.SANS_SERIF,fontsize)
  Loop
  btn.TextSize=fontsize
End Sub
 
Upvote 0

eps

Expert
Licensed User
Longtime User
What size in pixels are the Bitmaps that are used?

Are these defined globally? Counterintuitive, but works well for Android Apps.

Do you reuse bitmap definitions or define lots of them? Once they're on a Canvas you can reuse.
 
Upvote 0

pantaraf

Member
Licensed User
Longtime User
I've commented out that Sub and so far the errors are gone. Of course my sub is absolutely bad.
I guess that using a new canvas for each call leads to memory wasting.
Is there a better method to make text fit views on different devices?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You can do simething like this:
- declare this once in Activity_Create
B4X:
Dim bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
cvsDummy.Initialize2(bmp)

- and in the ScaleText routine:
B4X:
testo = cvsDummy.MeasureStringWidth(btn.text,Typeface.SANS_SERIF,fontsize)
 
Upvote 0

pantaraf

Member
Licensed User
Longtime User
The problem seems to be solved so far.

I've declared
B4X:
Private cvsdummy As Canvas
Private bmp As Bitmap
in Globals

In Activity_Create:
B4X:
If(bmp.IsInitialized=False) Then
  bmp.InitializeMutable(2dip, 2dip)
  cvsdummy.Initialize2(bmp)
End If

This for each activity in my project that needs to call TextScaler.ScaleText.

Thank you for your help.
Raffaele
 
  • Like
Reactions: eps
Upvote 0
Top