Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User fervus's Avatar
    Join Date
    Sep 2008
    Posts
    2
    Hello!
    I am working on a J2ME game with MIDP2 and m3g.
    My midlet starts more than one Thread which contains a canvase. When I exit one canvas, I stop the thread (return from it's main loop) and then I start a new Thread width a new canvas to take me to the next screen of the game. However, I noticed that the device (tested on Nokia N95) doesn't free the memory for the old thread and eventually runs out of mem.
    Calling System.gc doesn't help. The wierd thing is that the emulator from SUN DOES free the memory, but not the Nokia Device:

    Here is a bit of my code:

    baseCanvas.exitThread();
    baseCanvas=null;
    baseThread=null;
    System.gc();

    battleCanvas = new BattleCanvas3D(this);
    battleCanvas.setCommandListener(this);
    battleCanvas.addCommand(exitCommand);
    battleCanvas.addCommand(baseCommand);
    battleThread=new Thread(battleCanvas);
    battleThread.start();


    where exitThread() contains:
    exit=true;
    and the run() method of BattleCanvas contains:
    do {
    if (exit) {
    return;
    }
    .....
    }

    So... in this example I would like to free all the memory occupied by baseThread(baseCanvas). It does so on the emulator from SUN but not on the Nokia implementation.

    Can you help me solve this problem?
    Am I doing something wrong?
    Thanks!

  2. #2
    Registered User grahamhughes's Avatar
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,394
    My first suggestions (for anyone writing a MIDP game):

    Use only one Canvas. Switching between Displayable objects causes problems if you want to detect when your game is interrupted by an incoming call.

    Create only one Thread. Multiple threads will make your life a nightmare.

    So, create one Canvas instance and one Thread when the application starts, then keep them.

    As to your memory leaks... are you dumping and re-loading 3D models? Or creating models dynamically? Series 60 implementations have problems garbage collecting 3D models, and tend to leak.

    Load or create each model only once, and keep it.

  3. #3
    Registered User fervus's Avatar
    Join Date
    Sep 2008
    Posts
    2
    Quote Originally Posted by grahamhughes View Post
    My first suggestions (for anyone writing a MIDP game):

    Use only one Canvas. Switching between Displayable objects causes problems if you want to detect when your game is interrupted by an incoming call.

    Create only one Thread. Multiple threads will make your life a nightmare.

    So, create one Canvas instance and one Thread when the application starts, then keep them.

    As to your memory leaks... are you dumping and re-loading 3D models? Or creating models dynamically? Series 60 implementations have problems garbage collecting 3D models, and tend to leak.

    Load or create each model only once, and keep it.
    Yes, thank you!
    I'll try that! The only problem is that every module fit in about 7-8 MB. If I preload all of them, I'll need about 15 MB. I was hoping to do with less.
    Still, thank you for your reply.

  4. #4
    Registered User grahamhughes's Avatar
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,394
    You might have to do with less.

    Reduce the number of polygons in each model (this will increase performance too). This is a little outside my expertise, but I think there are tools that will optimize models by merging triangles into triangle-strips... this also reduces size and increases performance. Have a look around for different converters/exporters.

  5. #5
    Nokia Developer Expert rdrincon's Avatar
    Join Date
    May 2007
    Location
    Mexico D.F
    Posts
    351
    One more suggestion

    Once a variable or object is no longer used, reference it to null, this will help much more than calling System.gc
    :Ruben

Similar Threads

  1. Question. Free memory in Game Loop(J2ME)
    By videador in forum Mobile Java General
    Replies: 5
    Last Post: 2009-08-24, 06:05
  2. Replies: 1
    Last Post: 2005-10-30, 12:29
  3. How to free the memory
    By gadkii in forum Mobile Java General
    Replies: 3
    Last Post: 2004-04-04, 09:32
  4. How to set the memory size of emulator to match with the actual device ?
    By akokchai in forum Mobile Java Tools & SDKs
    Replies: 2
    Last Post: 2003-05-08, 10:32

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved