Archived

This topic is now archived and is closed to further replies.

Managing Memory and threads

This topic is 5153 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I play my game and I get killed and I go back to the main-menu and restart the game it runs slower. I think it is because I never kill the thread that was running in the last game and it stays running in the background or something? So if I go back to the main menu and restart the game twice (that’s 3 games in total) , in the memory monitor it show 3 GameCode objects which is my runnable class. So it seems that I need to kill the thread after every game. I’ve looked in my book and the only thing it mentions is .stop(). It says that stop has been deprecated. So is it ok to use stop? And also would this be the solution to the problem? The only other thing could maybe to set the thread to NULL but then this might not be possible ( I’m grasping at straws). My MIDlet layout might be a problem now as it was when I tried to implement a yield statement! I’ve Got 2 classes.

public class Pong extends MIDlet implements CommandListener
	{
	
	public Pong()
		{
		// add commands etc

		}


	public void commandAction(Command c, Displayable s) 
{
		else if ( index == 2)  // index = 2 is Play Game

	{
	mGameCode = new GameCode(this);
	Exit = new Command("Exit", Command.EXIT, 3);                           // adding commands used by pressing the soft buttons.

      NewGame = new Command("New Game", Command.OK, 2);
	MainMenu = new Command("Main Menu", Command.OK, 2);
	PauseGame = new Command("Pause", Command.OK, 1);

	mGameCode.addCommand(Exit);
	mGameCode.addCommand(NewGame);
	mGameCode.addCommand(MainMenu);
	mGameCode.addCommand(PauseGame);
	mGameCode.setCommandListener(this);

		display = Display.getDisplay( this );
		
		display.setCurrent( mGameCode );

		
		Thread t = new Thread(mGameCode);
		t.start();
		
	}

As you can see its my MIDlet class that starts the thread, so it might be a problem stopping it… Or could make a reference back to your midlet class from my runnable class GameCode and try something like MPong.t.stop(); Or Mpong.t = NULL; Also I was wondering should I call the Garbage collector everytime I start a new game or everyloop? 2) To make things faster i load all my graphics images into global image variables and the creat local image variables and set them equal to their cooresponding image variables. When i''m starting the game it now uses up 72000 bytes as per the memory monitor in WTK Image newbackground=background; Image newpaddel2=paddel2; Image newtarget1=target1; Image newtarget2=target2; is the way i''m setting the local image variables equal to the global variables, could instead make the global varialbes = NULL; once i''m finished with them?? Image newbackground=background; background = NULL; Image newpaddel2=paddel2; paddel2 = NULL; Image newtarget1=target1; target1 = NULL; Image newtarget2=target2; target2 = NULL; Thus saving memory and maybe not using so much ? Brian

Share this post


Link to post
Share on other sites
The only safe way to kill a thread is to make it terminate normally in its run method. Most people employ a variable in the run method, something like while (!dead) {...} loop in their run method. You can take that approach to kill off the thread.
However, because of the limited resource nature of j2me, the better option is to ''reuse'' existing objects, and not create them everytime you need one. Applying this to thread, design it so that it reuse the existing thread, not kick off one everytime you need it.

Share this post


Link to post
Share on other sites
I think that would require me to set some variable the first time the thread is run, and then instead of restarting the thread just make the canvas displayable every time the player decides to restart a new game!

Could be tricky .. will mull over it and give it a lash!

Share this post


Link to post
Share on other sites