OutOfMemory with GameLoop & Sprite

  • Replies:0
Davide Agu
  • Forum posts: 2

Mar 26, 2013, 10:23:27 PM via Website

I'm new in Android and i'm developing the Insult Swordfighting of Monkey Island 1, but i'm facing some noob problems

This is my GameLoop:
1public class MainThread extends Thread {
2
3private static final String TAG = MainThread.class.getSimpleName();
4
5// desired fps
6private final static int MAX_FPS = 50;
7// maximum number of frames to be skipped
8private final static int MAX_FRAME_SKIPS = 5;
9// the frame period
10private final static int FRAME_PERIOD = 1000 / MAX_FPS;
11
12
13// Surface holder that can access the physical surface
14private SurfaceHolder surfaceHolder;
15// The actual view that handles inputs
16// and draws to the surface
17private MainGamePanel gamePanel;
18
19// flag to hold game state
20private boolean running;
21public void setRunning(boolean running) {
22 this.running = running;
23}
24
25public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
26 super();
27 this.surfaceHolder = surfaceHolder;
28 this.gamePanel = gamePanel;
29}
30
31@Override
32public void run() {
33 Canvas canvas;
34 Log.d(TAG, "Starting game loop");
35 // initialise timing elements for stat gathering
36 initTimingElements();
37
38 long beginTime; // the time when the cycle begun
39 long timeDiff; // the time it took for the cycle to execute
40 int sleepTime; // ms to sleep (<0 if we're behind)
41 int framesSkipped; // number of frames being skipped
42
43 sleepTime = 0;
44
45 while (running) {
46 canvas = null;
47
48 // try locking the canvas for exclusive pixel editing
49 // in the surface
50 try {
51 canvas = this.surfaceHolder.lockCanvas();
52 synchronized (surfaceHolder) {
53 beginTime = System.currentTimeMillis();
54 framesSkipped = 0; // resetting the frames skipped
55 // update game state
56 this.gamePanel.update();
57 // render state to the screen
58 // draws the canvas on the panel
59 this.gamePanel.render(canvas);
60 // calculate how long did the cycle take
61 timeDiff = System.currentTimeMillis() - beginTime;
62 // calculate sleep time
63 sleepTime = (int)(FRAME_PERIOD - timeDiff);
64
65 if (sleepTime > 0) {
66 // if sleepTime > 0 we're OK
67 try {
68 // send the thread to sleep for a short period
69 // very useful for battery saving
70 Thread.sleep(sleepTime);
71 } catch (InterruptedException e) {}
72 }
73
74 while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
75 // we need to catch up
76 this.gamePanel.update(); // update without rendering
77 sleepTime += FRAME_PERIOD; // add frame period to check if in next frame
78 framesSkipped++;
79 }
80
81 }
82 } finally {
83 // in case of an exception the surface is not left in
84 // an inconsistent state
85 if (canvas != null) {
86 surfaceHolder.unlockCanvasAndPost(canvas);
87 }
88 } // end finally
89 }
90}

First noob question:

Is it legit that the background or fixed images (like a finished animation frame) are rendered constantly? Is it a waste of memory? I'd like to start with a cheap memory logic before starting to rely heavily on animations.

Second question:

In my render() function I call
1canvas.drawBitmap(background, null,new Rect(0, 0,canvas.getWidth(),canvas.getHeight()), paint);
or
1Rect dest = new Rect(this.getXpos(), getYpos(), getXpos() + (rclip.right - rclip.left),
2 getYpos() + (rclip.bottom - rclip.top));
3canvas.drawBitmap(tileSheet, rclip, dest, null);
Is it right to call all that "new"? GC cleans those after using them?


Update: Indeed, I add the pirate enemy to get a OutofMemory error in my LogCat.
So..I call

1currentGuy = new SpriteTile(R.drawable.guybrush1, R.xml.guybrush,
2 context);
3currentEnemy = new SpriteTile(R.drawable.pirata0, R.xml.pirate1,
4 context);

just in my renderer's constructor.
1tileSheet = BitmapFactory.decodeResource(context.getResources(),
2 spriteid);

to load the huge png spritesheet (1792x1872 363KB guybrush, 2464x2688 89KB pirate).

LogCat:
103-26 22:08:27.120: D/dalvikvm(1890): GC_FOR_ALLOC freed 59K, 14% free 9378K/10819K, paused 19ms, total 19ms
203-26 22:08:27.125: I/dalvikvm-heap(1890): Grow heap (frag case) to 13.170MB for 2949136-byte allocation
303-26 22:08:27.150: D/dalvikvm(1890): GC_CONCURRENT freed <1K, 11% free 12257K/13767K, paused 6ms+2ms, total 25ms
403-26 22:08:27.185: D/GuybrushRenderer(1890): Sfondo CARICATO
503-26 22:08:27.220: D/dalvikvm(1890): GC_FOR_ALLOC freed 1K, 11% free 12258K/13767K, paused 23ms, total 28ms
603-26 22:08:27.240: I/dalvikvm-heap(1890): Grow heap (frag case) to 25.967MB for 13418512-byte allocation
703-26 22:08:27.280: D/dalvikvm(1890): GC_CONCURRENT freed <1K, 6% free 25362K/26887K, paused 12ms+1ms, total 41ms
803-26 22:08:27.720: I/System.out(1890): Start document
903-26 22:08:27.720: I/System.out(1890): Start document
1003-26 22:08:27.720: I/System.out(1890): Start tag animations
1103-26 22:08:27.720: I/System.out(1890): Start tag animation
1203-26 22:08:27.720: I/System.out(1890): Start tag framerect
1303-26 22:08:27.720: I/System.out(1890): Start tag framerect
1403-26 22:08:27.720: I/System.out(1890): Start tag framerect
1503-26 22:08:27.725: I/System.out(1890): Start tag framerect
1603-26 22:08:27.725: I/System.out(1890): Start tag framerect
1703-26 22:08:27.725: I/System.out(1890): Start tag framerect
1803-26 22:08:27.725: I/System.out(1890): Start tag framerect
1903-26 22:08:27.725: I/System.out(1890): Start tag collisionrect
2003-26 22:08:27.725: I/System.out(1890): Start tag animation
2103-26 22:08:27.725: I/System.out(1890): Start tag framerect
2203-26 22:08:27.725: I/System.out(1890): Start tag framerect
2303-26 22:08:27.725: I/System.out(1890): Start tag framerect
2403-26 22:08:27.725: I/System.out(1890): Start tag framerect
2503-26 22:08:27.725: I/System.out(1890): Start tag framerect
2603-26 22:08:27.725: I/System.out(1890): Start tag framerect
2703-26 22:08:27.725: I/System.out(1890): Start tag framerect
2803-26 22:08:27.725: I/System.out(1890): Start tag framerect
2903-26 22:08:27.725: I/System.out(1890): Start tag collisionrect
3003-26 22:08:27.725: I/System.out(1890): Start tag animation
3103-26 22:08:27.725: I/System.out(1890): Start tag framerect
3203-26 22:08:27.725: I/System.out(1890): Start tag framerect
3303-26 22:08:27.725: I/System.out(1890): Start tag framerect
3403-26 22:08:27.725: I/System.out(1890): Start tag framerect
3503-26 22:08:27.725: I/System.out(1890): Start tag framerect
3603-26 22:08:27.725: I/System.out(1890): Start tag framerect
3703-26 22:08:27.725: I/System.out(1890): Start tag framerect
3803-26 22:08:27.725: I/System.out(1890): Start tag framerect
3903-26 22:08:27.725: I/System.out(1890): Start tag collisionrect
4003-26 22:08:27.725: I/System.out(1890): Start tag animation
4103-26 22:08:27.725: I/System.out(1890): Start tag framerect
4203-26 22:08:27.725: I/System.out(1890): Start tag framerect
4303-26 22:08:27.725: I/System.out(1890): Start tag framerect
4403-26 22:08:27.725: I/System.out(1890): Start tag framerect
4503-26 22:08:27.730: I/System.out(1890): Start tag framerect
4603-26 22:08:27.730: I/System.out(1890): Start tag framerect
4703-26 22:08:27.730: I/System.out(1890): Start tag framerect
4803-26 22:08:27.730: I/System.out(1890): Start tag framerect
4903-26 22:08:27.730: I/System.out(1890): Start tag collisionrect
5003-26 22:08:27.730: I/System.out(1890): Start tag animation
5103-26 22:08:27.730: I/System.out(1890): Start tag framerect
5203-26 22:08:27.730: I/System.out(1890): Start tag framerect
5303-26 22:08:27.730: I/System.out(1890): Start tag framerect
5403-26 22:08:27.730: I/System.out(1890): Start tag framerect
5503-26 22:08:27.730: I/System.out(1890): Start tag framerect
5603-26 22:08:27.730: I/System.out(1890): Start tag framerect
5703-26 22:08:27.730: I/System.out(1890): Start tag framerect
5803-26 22:08:27.730: I/System.out(1890): Start tag framerect
5903-26 22:08:27.730: I/System.out(1890): Start tag collisionrect
6003-26 22:08:27.730: I/System.out(1890): Start tag animation
6103-26 22:08:27.730: I/System.out(1890): Start tag framerect
6203-26 22:08:27.730: I/System.out(1890): Start tag framerect
6303-26 22:08:27.730: I/System.out(1890): Start tag framerect
6403-26 22:08:27.730: I/System.out(1890): Start tag framerect
6503-26 22:08:27.730: I/System.out(1890): Start tag framerect
6603-26 22:08:27.730: I/System.out(1890): Start tag framerect
6703-26 22:08:27.730: I/System.out(1890): Start tag framerect
6803-26 22:08:27.730: I/System.out(1890): Start tag framerect
6903-26 22:08:27.730: I/System.out(1890): Start tag collisionrect
7003-26 22:08:27.730: I/System.out(1890): Start tag animation
7103-26 22:08:27.730: I/System.out(1890): Start tag framerect
7203-26 22:08:27.730: I/System.out(1890): Start tag framerect
7303-26 22:08:27.730: I/System.out(1890): Start tag framerect
7403-26 22:08:27.730: I/System.out(1890): Start tag framerect
7503-26 22:08:27.730: I/System.out(1890): Start tag framerect
7603-26 22:08:27.735: I/System.out(1890): Start tag framerect
7703-26 22:08:27.735: I/System.out(1890): Start tag framerect
7803-26 22:08:27.735: I/System.out(1890): Start tag framerect
7903-26 22:08:27.735: I/System.out(1890): Start tag collisionrect
8003-26 22:08:27.735: I/System.out(1890): Start tag animation
8103-26 22:08:27.735: I/System.out(1890): Start tag framerect
8203-26 22:08:27.735: I/System.out(1890): Start tag framerect
8303-26 22:08:27.735: I/System.out(1890): Start tag framerect
8403-26 22:08:27.735: I/System.out(1890): Start tag framerect
8503-26 22:08:27.735: I/System.out(1890): Start tag framerect
8603-26 22:08:27.735: I/System.out(1890): Start tag framerect
8703-26 22:08:27.735: I/System.out(1890): Start tag framerect
8803-26 22:08:27.735: I/System.out(1890): Start tag collisionrect
8903-26 22:08:27.735: I/System.out(1890): Start tag animation
9003-26 22:08:27.735: I/System.out(1890): Start tag framerect
9103-26 22:08:27.735: I/System.out(1890): Start tag framerect
9203-26 22:08:27.735: I/System.out(1890): Start tag framerect
9303-26 22:08:27.735: I/System.out(1890): Start tag framerect
9403-26 22:08:27.735: I/System.out(1890): Start tag framerect
9503-26 22:08:27.735: I/System.out(1890): Start tag framerect
9603-26 22:08:27.735: I/System.out(1890): Start tag collisionrect
9703-26 22:08:27.735: I/System.out(1890): Start tag animation
9803-26 22:08:27.735: I/System.out(1890): Start tag framerect
9903-26 22:08:27.735: I/System.out(1890): Start tag framerect
10003-26 22:08:27.735: I/System.out(1890): Start tag framerect
10103-26 22:08:27.735: I/System.out(1890): Start tag framerect
10203-26 22:08:27.735: I/System.out(1890): Start tag framerect
10303-26 22:08:27.735: I/System.out(1890): Start tag framerect
10403-26 22:08:27.735: I/System.out(1890): Start tag collisionrect
10503-26 22:08:27.735: I/System.out(1890): Start tag animation
10603-26 22:08:27.740: I/System.out(1890): Start tag framerect
10703-26 22:08:27.740: I/System.out(1890): Start tag framerect
10803-26 22:08:27.740: I/System.out(1890): Start tag framerect
10903-26 22:08:27.740: I/System.out(1890): Start tag framerect
11003-26 22:08:27.740: I/System.out(1890): Start tag framerect
11103-26 22:08:27.740: I/System.out(1890): Start tag framerect
11203-26 22:08:27.740: I/System.out(1890): Start tag collisionrect
11303-26 22:08:27.740: I/System.out(1890): Start tag animation
11403-26 22:08:27.740: I/System.out(1890): Start tag framerect
11503-26 22:08:27.740: I/System.out(1890): Start tag framerect
11603-26 22:08:27.740: I/System.out(1890): Start tag framerect
11703-26 22:08:27.740: I/System.out(1890): Start tag framerect
11803-26 22:08:27.740: I/System.out(1890): Start tag framerect
11903-26 22:08:27.740: I/System.out(1890): Start tag framerect
12003-26 22:08:27.740: I/System.out(1890): Start tag collisionrect
12103-26 22:08:27.740: I/System.out(1890): Start tag animation
12203-26 22:08:27.740: I/System.out(1890): Start tag framerect
12303-26 22:08:27.740: I/System.out(1890): Start tag framerect
12403-26 22:08:27.740: I/System.out(1890): Start tag framerect
12503-26 22:08:27.740: I/System.out(1890): Start tag framerect
12603-26 22:08:27.740: I/System.out(1890): Start tag framerect
12703-26 22:08:27.740: I/System.out(1890): Start tag framerect
12803-26 22:08:27.740: I/System.out(1890): Start tag collisionrect
12903-26 22:08:27.740: I/System.out(1890): Sprite Loaded
13003-26 22:08:27.755: D/dalvikvm(1890): GC_FOR_ALLOC freed 20K, 6% free 25373K/26887K, paused 12ms, total 12ms
13103-26 22:08:27.755: I/dalvikvm-heap(1890): Forcing collection of SoftReferences for 26492944-byte allocation
13203-26 22:08:27.770: D/dalvikvm(1890): GC_BEFORE_OOM freed 9K, 6% free 25363K/26887K, paused 17ms, total 18ms
13303-26 22:08:27.770: E/dalvikvm-heap(1890): Out of memory on a 26492944-byte allocation.
13403-26 22:08:27.770: I/dalvikvm(1890): "main" prio=5 tid=1 RUNNABLE
13503-26 22:08:27.770: I/dalvikvm(1890): | group="main" sCount=0 dsCount=0 obj=0x40fc4508 self=0x40ee0a78
13603-26 22:08:27.770: I/dalvikvm(1890): | sysTid=1890 nice=0 sched=0/0 cgrp=apps handle=1074331440
13703-26 22:08:27.770: I/dalvikvm(1890): | schedstat=( 550930779 206532425 613 ) utm=47 stm=7 core=1
13803-26 22:08:27.775: I/dalvikvm(1890): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
13903-26 22:08:27.775: I/dalvikvm(1890): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:625)
14003-26 22:08:27.775: I/dalvikvm(1890): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:478)
14103-26 22:08:27.775: I/dalvikvm(1890): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:501)
14203-26 22:08:27.775: I/dalvikvm(1890): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:531)
14303-26 22:08:27.780: I/dalvikvm(1890): at com.gorgo.pirates.view.SpriteTile.loadSprite(SpriteTile.java:65)
14403-26 22:08:27.780: I/dalvikvm(1890): at com.gorgo.pirates.view.SpriteTile.<init>(SpriteTile.java:60)
14503-26 22:08:27.780: I/dalvikvm(1890): at com.gorgo.pirates.view.GuybrushRenderer.<init>(GuybrushRenderer.java:42)
14603-26 22:08:27.780: I/dalvikvm(1890): at com.gorgo.pirates.MainGamePanel.<init>(MainGamePanel.java:56)
14703-26 22:08:27.780: I/dalvikvm(1890): at com.gorgo.pirates.Pirates.onCreate(Pirates.java:21)
14803-26 22:08:27.785: I/dalvikvm(1890): at android.app.Activity.performCreate(Activity.java:5206)
14903-26 22:08:27.790: I/dalvikvm(1890): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
15003-26 22:08:27.790: I/dalvikvm(1890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
15103-26 22:08:27.790: I/dalvikvm(1890): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
15203-26 22:08:27.790: I/dalvikvm(1890): at android.app.ActivityThread.access$700(ActivityThread.java:140)
15303-26 22:08:27.790: I/dalvikvm(1890): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
15403-26 22:08:27.790: I/dalvikvm(1890): at android.os.Handler.dispatchMessage(Handler.java:99)
15503-26 22:08:27.795: I/dalvikvm(1890): at android.os.Looper.loop(Looper.java:137)
15603-26 22:08:27.795: I/dalvikvm(1890): at android.app.ActivityThread.main(ActivityThread.java:4921)
15703-26 22:08:27.795: I/dalvikvm(1890): at java.lang.reflect.Method.invokeNative(Native Method)
15803-26 22:08:27.805: I/dalvikvm(1890): at java.lang.reflect.Method.invoke(Method.java:511)
15903-26 22:08:27.810: I/dalvikvm(1890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
16003-26 22:08:27.810: I/dalvikvm(1890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
16103-26 22:08:27.810: I/dalvikvm(1890): at dalvik.system.NativeStart.main(Native Method)
16203-26 22:08:27.810: D/skia(1890): --- decoder->decode returned false
16303-26 22:08:27.810: D/AndroidRuntime(1890): Shutting down VM
16403-26 22:08:27.810: W/dalvikvm(1890): threadid=1: thread exiting with uncaught exception (group=0x40fc32a0)
16503-26 22:08:27.810: E/AndroidRuntime(1890): FATAL EXCEPTION: main
16603-26 22:08:27.810: E/AndroidRuntime(1890): java.lang.OutOfMemoryError
16703-26 22:08:27.810: E/AndroidRuntime(1890): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
16803-26 22:08:27.810: E/AndroidRuntime(1890): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:625)
16903-26 22:08:27.810: E/AndroidRuntime(1890): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:478)
17003-26 22:08:27.810: E/AndroidRuntime(1890): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:501)
17103-26 22:08:27.810: E/AndroidRuntime(1890): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:531)
17203-26 22:08:27.810: E/AndroidRuntime(1890): at com.gorgo.pirates.view.SpriteTile.loadSprite(SpriteTile.java:65)
17303-26 22:08:27.810: E/AndroidRuntime(1890): at com.gorgo.pirates.view.SpriteTile.<init>(SpriteTile.java:60)
17403-26 22:08:27.810: E/AndroidRuntime(1890): at com.gorgo.pirates.view.GuybrushRenderer.<init>(GuybrushRenderer.java:42)
17503-26 22:08:27.810: E/AndroidRuntime(1890): at com.gorgo.pirates.MainGamePanel.<init>(MainGamePanel.java:56)
17603-26 22:08:27.810: E/AndroidRuntime(1890): at com.gorgo.pirates.Pirates.onCreate(Pirates.java:21)
17703-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.Activity.performCreate(Activity.java:5206)
17803-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
17903-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
18003-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
18103-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.ActivityThread.access$700(ActivityThread.java:140)
18203-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
18303-26 22:08:27.810: E/AndroidRuntime(1890): at android.os.Handler.dispatchMessage(Handler.java:99)
18403-26 22:08:27.810: E/AndroidRuntime(1890): at android.os.Looper.loop(Looper.java:137)
18503-26 22:08:27.810: E/AndroidRuntime(1890): at android.app.ActivityThread.main(ActivityThread.java:4921)
18603-26 22:08:27.810: E/AndroidRuntime(1890): at java.lang.reflect.Method.invokeNative(Native Method)
18703-26 22:08:27.810: E/AndroidRuntime(1890): at java.lang.reflect.Method.invoke(Method.java:511)
18803-26 22:08:27.810: E/AndroidRuntime(1890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
18903-26 22:08:27.810: E/AndroidRuntime(1890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
19003-26 22:08:27.810: E/AndroidRuntime(1890): at dalvik.system.NativeStart.main(Native Method)

What's wrong with my logic?

Thank you for you help

Reply