Android Question OutOfMemory Crash: What does this code do?

Turbo3

Active Member
Licensed User
Longtime User
B4X:
Dim r As Reflector
        r.RunStaticMethod("java.lang.System", "gc", Null, Null)

After releasing a new version of my app I have been getting crash reports of "OutOfMemory" like this invoked after calling canvas.initialize. Why are three bitmaps being created?
B4X:
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
at android.graphics.Bitmap.createBitmap(Bitmap.java:868)
at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:76)

One solution recommended adding the above code to Activity_Pause (UserClosed=true)

Out of 10 crashes 8 are from Galaxy devices which seem to have a problem with memory allocation. The other two are from a Sharp and Fujitsu phone.
 

Turbo3

Active Member
Licensed User
Longtime User
Canvas is used by DrawColor(called once), DrawLine(called 7 times), DrawRect(called 10 times), and DrawText (called 16 times).

I thought the RunStaticMethod might be calling garbage collection because of the"gc". Not sure I see how it creates a bitmap. The "What does this code do?" was not referring to the stack trace but the code someone listed to prevent the outofmemory error.

I know the createBitmap in the stack trace creates a bitmap I just don't understand why it is creating three of them. You only need one for the canvas, right?
 
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
Actually I never did get an answer to what this code does? Does anybody know?
B4X:
Dim r As Reflector
        r.RunStaticMethod("java.lang.System", "gc", Null, Null)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Turbo3

Active Member
Licensed User
Longtime User
Found it.
The java.lang.System.gc() method runs the garbage collector. Calling this suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.
 
Upvote 0
Top