Nokia s 40 (Out of memory exception)

Forum for discussion on Game porting for mobile phones and j2me supported devices including device information.

Nokia s 40 (Out of memory exception)

Postby hasharts » Tue Dec 23, 2008 12:38 pm

HI,

In my game i have different classes which extends GameCanves for different screens( Help, About Us, Game ect). I move from screen to screen by making an object of that class and by setting it to the current display. This works well in sony ericsson phones and some Nokia phones like N 73, N 70 ect. But in Nokia 6300 , 6280 (s 40 series) it gives out of memory exception when I make an additional object.

ex- for main menu and game i call these methods in the main Midlet
MainMenu mm;
Game game;

showMainMenu() {
if(mm == null) {
mm = new MainMenu();
}
setCurrentDisplay(mm);
}

showGame() {
if(game == null) {
game = new MainMenu();
}
setCurrentDisplay(game);
}

when i call showGame() after calling showMainMenu() it gives out of memory exception.

Thanks,
Hashani
User avatar
hasharts
 
Posts: 8
Joined: Fri May 16, 2008 5:30 am

Re: Nokia s 40 (Out of memory exception)

Postby DevelopmentTeam » Wed Dec 24, 2008 8:33 am

1) Don't use many gamecanvas extended classes in your program. Instead what we usually do is, we have an abstract class named screens. Each screen is designed to display each window (or menu). The game canvas calls the functions that is set to display and switching the screens must be much simpler.

Screen abstract class
Code: Select all
import javax.microedition.lcdui.Graphics;

public abstract class Screen {

    private byte keyState;

    public abstract void drawScreen(Graphics g);

    public abstract void GameCycle(long ticks);

    public abstract void GamePausedTime(long secs);

//    public abstract boolean canPause();

    public byte GetKeyState() {
        return KeyConstant.getGameCodeKey(keyState);
    }

    public void SetKeyState(byte key) {
        keyState = key;
    }
}


GameCanvas class

Code: Select all
package BeesFlowers;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class GameCanvas extends Canvas implements Runnable {
    private Screen currentScreen;
    private static final long MS_PER_FRAME = 60; //  Hz
    private static final long MS_PER_FRAME1 = 90;
    private boolean paused = false;
    private boolean endGame = false;
    public static boolean running = true;
    private byte keyStates;
    private byte oldkey;
    private BeesFlowers midlet;
    private long pausedTime = 0;
    private long kp;
    public Thread thread;
    public GameCanvas(BeesFlowers midlet) {       
        this.midlet = midlet;
        setFullScreenMode(true);
        thread = new Thread(this);
        thread.start();       
    }
    public void SetScreen(Screen screen) {
        currentScreen = null;
        System.gc();
        currentScreen = screen;       
            }
    protected void paint(Graphics g) {
        g.setClip(0, 0, getWidth(), getHeight());
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setClip(0, 0, Store.DisplayWidth, Store.DisplayHeight);
        if (currentScreen != null && !paused) {
            currentScreen.drawScreen(g);
        } else {
            g.setColor(0, 0, 0);
            g.fillRect(0, 0, Store.DisplayWidth, Store.DisplayHeight);
            g.setColor(255, 255, 255);
            g.drawString("Game Paused", 55, 100, 0);
        } 
        g.setColor(255, 255, 255);     
    }
    public void Quit() {
        midlet.Quit();
        this.endGame = true;
    }
    public void run() {
        long midTime;
        long delta = 0;
        try {
            midTime = System.currentTimeMillis();
            while (running) {
                //System.out.println("Used Memory : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/1024);
                if (endGame) {
                    return;
                }
                if (!paused) {
                    synchronized (this) {
                        if (currentScreen != null) {
                            currentScreen.SetKeyState(this.keyStates);
                            currentScreen.GameCycle(midTime);
                        }
                        delta = System.currentTimeMillis() - midTime;
                        midTime = System.currentTimeMillis();
                    }
                    if (Store.splashScreen != null) {
                        if (delta < MS_PER_FRAME1) {
                            synchronized (this) {
                                wait(MS_PER_FRAME1 - delta);
                            }
                        } else {
                            Thread.yield();
                        }
                    } else {
                        if (delta < MS_PER_FRAME) {
                            synchronized (this) {
                                wait(MS_PER_FRAME - delta);
                            }
                        } else {
                            Thread.yield();
                        }
                    }
                }
                repaint();
                serviceRepaints();
            }
        } catch (Exception e) {
        }
    }
    public void togglePause() {
//        if (!paused) {
//            pausedTime = System.currentTimeMillis();
//        } else {
//            currentScreen.GamePausedTime(System.currentTimeMillis() - pausedTime);
//        }
//        paused = !paused;
    }
   
    protected void keyPressed(int keyCode) {
        this.keyStates = (byte) keyCode;
        if (KeyConstant.getGameCodeKey(this.keyStates) == KeyConstant.KEY_POUND) {
            togglePause();
        }
    }
    protected void keyReleased(int keyCode) {
        Store.ReleasedKey = keyCode;
        this.keyStates = 0;
    }
    protected void keyRepeated(int keyCode) {
        Store.PressedKey = keyCode;
    }
}


2) Outofmemory is an error that pops when you run out of memory and it depends on the capacity of the handset. So you have to optimize each classes like free the memory in the old gamecanvas before constructing another.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Nokia s 40 (Out of memory exception)

Postby hasharts » Sat Feb 14, 2009 4:17 am

Hi CodeTiger,

Sorry for the late reply . Thank you very much for the details and the code. That was really useful for me.

Regards,
Hashani
User avatar
hasharts
 
Posts: 8
Joined: Fri May 16, 2008 5:30 am

Re: Nokia s 40 (Out of memory exception)

Postby DevelopmentTeam » Fri Feb 27, 2009 5:19 am

Welcome...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Nokia s 40 (Out of memory exception)

Postby escapee626 » Wed May 06, 2009 1:56 am

Wow, I actually found this pretty useful as well. Thanks to the both of you! :)
escapee626
 
Posts: 1
Joined: Wed May 06, 2009 1:51 am


Return to Mobile Game Porting and Testing

Who is online

Users browsing this forum: No registered users and 2 guests

cron