Android Question Canvas and out of memory

yiankos1

Active Member
Licensed User
Longtime User
Hello my friends,
I have a little problem with my game. It's simple enough, i mask almost hole screen with a canvas(panel), and you have to touch the hole screen to unmask it(trasparent color). So the problem emerging when you have to initialize in each level the canvas again and again. After a dozen of plays, you can understand that game become slower until it crashes because outofmemory. I read somewhere that canvas, if it's big enough costs a lot of memory. I tried to free memory with java.lang but it does not work cause canvas it is loaded in panel(in order to catch the X and Y). That problem emerging only at slower devices like samsung galaxy S3. I tested at S4, S5, Note 3, Note 4 and it is working like a charm. If you want more details, feel free to ask.
Thank in advance for your time
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
It sounds like you maybe creating a new canvas each time which eventually leads to the out of memory error. You should instead try to reuse the same canvas over and over again for each level.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
RS is right, it smells like dim'ing a new canvas at each level.

Just erase/redraw the canvas at each level.
 
Upvote 0

yiankos1

Active Member
Licensed User
Longtime User
Thanks a lot for your help. I figured it out how to do it:
First i initilize the canvas and in each level i just
B4X:
circle.DrawColor(Colors.rgb(180, 180, 180))
 
Upvote 0

yiankos1

Active Member
Licensed User
Longtime User
If you see the log below, can someone explain the reason for OutOfMemoryError ?

B4X:
java.lang.OutOfMemoryError


    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper.Initialize(CanvasWrapper.java:76)
    at scratch.it.df.main._activity_create(main.java:442)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at scratch.it.df.main.afterFirstLayout(main.java:100)
    at scratch.it.df.main.access$100(main.java:17)
    at scratch.it.df.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

Reviewnow

Active Member
Licensed User
Longtime User
3-27-2015 12-39-45 PM.png


I had this same issue with a blank canvas that is only loaded once when an activity is displayed it would work fine for 6 or 7 times then I would get the out of memory
and it would crash the app, I was able to resolve the issue by going to the designer and set the Animation Duration(ms) to 0
 
Upvote 0

yiankos1

Active Member
Licensed User
Longtime User
View attachment 33127

I had this same issue with a blank canvas that is only loaded once when an activity is displayed it would work fine for 6 or 7 times then I would get the out of memory
and it would crash the app, I was able to resolve the issue by going to the designer and set the Animation Duration(ms) to 0

This is "THE SOLUTION". Thank you very much ReviewNow for your time and your help.
 
Upvote 0
Top