Android Question Canvas and out of memory

Discussion in 'Android Questions' started by yiankos1, Mar 27, 2015.

  1. yiankos1

    yiankos1 Active Member Licensed 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
     
  2. RandomCoder

    RandomCoder Well-Known Member Licensed 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.
     
  3. sorex

    sorex Expert Licensed User

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

    Just erase/redraw the canvas at each level.
     
  4. yiankos1

    yiankos1 Active Member Licensed 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
    Code:
    circle.DrawColor(Colors.rgb(180180180))
     
    RandomCoder likes this.
  5. yiankos1

    yiankos1 Active Member Licensed User

    If you see the log below, can someone explain the reason for OutOfMemoryError ?

    Code:
    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)
     
  6. Reviewnow

    Reviewnow Active Member Licensed 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
     
    yiankos1 likes this.
  7. yiankos1

    yiankos1 Active Member Licensed User

    This is "THE SOLUTION". Thank you very much ReviewNow for your time and your help.
     
    RandomCoder likes this.
  8. Reviewnow

    Reviewnow Active Member Licensed User

    welcome
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice