×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    Question Question. Free memory in Game Loop(J2ME)

    Hi

    I have a question that how force to free memory. I put here basic design of my game loop using with only one canvas. Is MIDP 1.0. Next of code my question.

    Code:
    public class MyCanvas
    extends FullCanvas implements Runnable{
     
        ...
       public MyCanvas(){
        ...
        menu = new MENU(....);
        game = new GAME(....);
        ...
      }
    
        run(){
                while(end){
                     switch(STATE)
                     case MENU:
                             menu.doLogic();                  
                     case GAME:
                             game.doLogic();                         
    
                     Repaint();
                     serviceRepaints();
                }
        }
    
        ...
        paint(Graphics g){
    
                     switch(STATE)
                     case MENU:
                             menu.draw(g);                  
                     case GAME:
                             game.draw(g);                         
                    
        }

    For example 'menu' load image of background, big image. I want know as can free 'menu' object when STATE are on GAME for free space in memory, because i think that background image for example is too big for have in memory all the time, for obtain more velocity in GAME state. ¿All this is correct?.

    I know that in java is GC who free the memory, but as i wrote before, isnt good have data of menu when we are in GAME state.

    ¿For example this free the memory?


    public class MyCanvas
    extends FullCanvas implements Runnable{

    Code:
        ...
       public MyCanvas(){
        ...
        menu = new MENU(....);    
        ...
      }
    
        run(){
                while(end){
                     switch(STATE)
                     case MENU:
                             if(firstTime){
                                  game=null;
                                  menu = new MENU(..);
                                  firstTime=false;
                             }
                             menu.doLogic();                  
                             break;
                     case GAME:
                             if(firstTime){
                                  menu=null;
                                  game = new GAME(..);
                                  firstTime=false;
                             }
                             game.doLogic();        
                             break;                 
    
                     Repaint();
                     serviceRepaints();
                }
        }
    
        ...
        paint(Graphics g){
    
                     switch(STATE)
                     case MENU:
                             menu.draw(g);                  
                     case GAME:
                             game.draw(g);                         
                    
        }
    With this code, ¿are loaded in memory the data of 'menu' when STATE is GAME?.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Question. Free memory in Game Loop(J2ME)

    Hi there,

    You should mark the unused references as null and call System.gc() (or Runtime.gc()) but as you may know this is not deterministic, the GC can happen right away or afterwards.

    Also, check that:

    Technical Note: Tuning Up Java™ MIDP Performance
    http://www.forum.nokia.com/info/sw.n..._0_en.pdf.html

    J2ME performance tips
    http://www.javaperformancetuning.com/tips/j2me.shtml

    BR

  3. #3
    Registered User
    Join Date
    Jun 2004
    Location
    Buenos Aires, Argentina
    Posts
    28

    Re: Question. Free memory in Game Loop(J2ME)

    I did not recommend you to include a System.gc() in your main game cycle, in several phone devices this action get down the FPS.
    -------------------------
    Carlos Olivera
    www.carlosoliverablog.com

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Brazil
    Posts
    1,884

    Re: Question. Free memory in Game Loop(J2ME)

    Of course, you must analyze and see the hotspots where you can safely include some calls, anyway you will need some...no doubts.
    Juarez Alvares Barbosa Junior - Brazil

  5. #5
    Regular Contributor
    Join Date
    Jan 2008
    Posts
    387

    Re: Question. Free memory in Game Loop(J2ME)

    hi, if you wana to free memory allocated to object, initialise null to them and request for garbage collection, And do not load both the class at same time, load only menu class and it should not at all depend upon game class and vice-versa and see this
    Code:
    public MyCanvas(){
        ...
        menu = new MENU(....);    
        ...
      }
    it means you are loading menu class in my canvas take care of it.
    Thanks and Regards,
    Manjul.

  6. #6
    Super Contributor
    Join Date
    Apr 2009
    Posts
    572

    Re: Question. Free memory in Game Loop(J2ME)

    yeah in most cases marking unused classes as null do the work but never call system.gc for garbage collection in j2me as in most cases it only reduces the performance and moreover porting issues are also raised so better is to mark them null and wait for system to remove them autonomously
    Regards,
    Saurabh

Similar Threads

  1. question about thread in game loop!
    By cach2008 in forum Mobile Java General
    Replies: 4
    Last Post: 2004-02-09, 01:52
  2. Replies: 2
    Last Post: 2004-01-08, 18:36
  3. Free RAM and Free User Memory
    By koayst in forum Symbian
    Replies: 5
    Last Post: 2003-05-09, 12:47
  4. Game loop question
    By tinkezione in forum Mobile Java General
    Replies: 7
    Last Post: 2002-12-31, 08:14
  5. can not successfully link any sample using .NET
    By lobotomat in forum Symbian Tools & SDKs
    Replies: 2
    Last Post: 2002-08-20, 00:29

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×