×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Lots of heap memory used by undesired objects.

    Hi.
    I am finishing my first J2ME midlet, a simple 2D scroller, using netbeans 6.9. I've learned a lot by doing this and reading this forum.
    The problem is that my game uses about 4 MB of heap memory, and crashes in most of cellphones (OutOfMemory). This seemed strange, because the game is simple, uses few images, and the code appears (at least to me) to be 'memory sparing'.

    Finally i did a Heap dump and i noted that the most of heap is used by a lot of byte arrays (460! using about 2.5 MB). Those arrays are referenced by 189 ImageData objects, and each of them are referenced finally by an ImageArray referenced by com.sun.midp.chameleon.skins.resources.LoadedSkinResources objects. I have about 5 images loaded at this time, but that LoadedSkinResources object is creating lots of images and wasting the memory.

    I've searched about chameleon and found that is some kind of way to make the high level user interface look like the telephone graphic style. I don't want that feature, because i'm using only the GameCanvas, not the forms. Can i prevent that memory wasting??

    Here is the relevant part of Heap Dump (at splash screen)

    [B number of objects = 460 size = 2529228
    javax.microedition.lcdui.ImageData n= 189 size: 6048
    javax.microedition.lcdui.Image n = 189 Size: 3024
    [Ljavax/microedition/lcdui/Image n= 60 size= 2264
    com.sun.midp.chameleon.skins.resources.LoadedSkinResources

    Thanks in advance. I'm very confused about this and my game isn't running.

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: Lots of heap memory used by undesired objects.

    Cut & Paste your code here, don't import and use any netbeans UI objects.... write pure canvas app to avoid such memory miss-use.
    thanks,
    ~Amitabh
    (Champion of the Month -Aug'13)
    Follow me on my blog for Innovative Mobile Apps

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Lots of heap memory used by undesired objects.

    How many images do you have? And what sizes are they? (Pixel sizes, not PNG file sizes.) I recommend you read this article from the Forum wiki. It might explain where your memory is going.

    Graham.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Re: Lots of heap memory used by undesired objects.

    Hi... Thanks for your answers.
    In the splash screen i have 5 images loaded (176x176, 24 bits color depth = abouts 93 Kb of raw raster data -> 465 Kb of total image data).
    In the heap dump there are 189 image objects (my 5 explicitly created images + 184 'other images'). Those images are contained in 60 image arrays, that are all referenced by ...chameleon.skins.resources.LoadedSkinResources. I don't know what are those images.

    Here is a part of my code...

    The midlet class

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class Atun extends MIDlet implements CommandListener{
    //Revisión 1.1 con gestor de memoria optimizada.
    //Declaraciones
    private CanvasJuego miJuego;


    //El Thread principal
    Thread mainThread;
    //Form formulario;
    Display display;

    public Atun()
    {
    miJuego = new CanvasJuego(this);
    miJuego.setFullScreenMode(false);


    }


    public void startApp() {

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


    if (mainThread == null)
    {
    try
    {
    mainThread = new Thread(miJuego);
    mainThread.start();
    }
    catch (Error e)
    {
    destroyApp (false);
    notifyDestroyed ();
    }
    }




    }

    public void pauseApp() {

    // Not working
    System.out.println("Paused by system request");


    }

    public void destroyApp(boolean unconditional) {

    }

    public void commandAction(Command c, Displayable s)
    {



    }

    public void Exit()
    {
    destroyApp (false);
    notifyDestroyed ();
    }

    }

    The GameCanvas class
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.io.*;
    import java.util.Random;


    public class CanvasJuego extends GameCanvas implements Runnable {
    ....

    public CanvasJuego(Atun midlet)
    {
    super(false); //Llama al constructor base

    this.AtunMidlet = midlet;

    done = true; //Mientras sea false corre el GameLoop
    frameTime = 80;//80 normal, 40 doble; //Duración en ms del frame

    etapa = 13;
    vidas = 5;

    if (RandomGenerator == null)
    RandomGenerator = new Random();

    erizos = new ChunkContainer[29];
    mares = new ChunkContainer[29];

    CargarNivelPorChunks(etapa);



    }

    I hope you can help me....
    Thanks!

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Lots of heap memory used by undesired objects.

    I think you're seeing a lot of objects that are just part of the emulator implementation. Which emulator are you using? And on what devices are you seeing OutOfMemoryErrors?

    I'd reckon on 32 bits per pixel (to include an alpha channel), so guess at around 600k for the images.

    Remember: memory management is very specific to the Java runtime implementation. It's hard to diagnose memory issues accurately with emulation. Nokia emulators will mimmic the behaviour of a real device more than most - certainly more than the WTK emulators.

    If you're using the Java ME SDK 3.0, consider using a Nokia SDK emulator (Series 40, ideally), or the WTK 2.5.2. You might find these emulators more helpful.

    Graham.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Re: Lots of heap memory used by undesired objects.

    Hi...
    I'm using Java Me SDK 3.0 with Netbeans 6.9. I'm getting OutOfMemory in almost all the devices i tried (Nokia 6103, Samsung SGH F275L...) it only runs in Sony Ericsson devices (w610 and other i can't remember now). I know Sonys have dynamic heap size, so i think that's why it runs in them.
    Actually i'm using 24 bits images (no alpha channel, that is just the splash screen, a full screen image).

    May be some objects can be part of implementation, but they are of image type and too heavy.. also, the game doesn't run in real machines, so i think there is something wrong that it's wasting the mem.

    I'll try with series 40 emulator... Thank you all.

Similar Threads

  1. [merged] Lots of errors "Java heap space" on Carbide C++ V2.4
    By stonejustek in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 6
    Last Post: 2010-04-29, 09:40
  2. Replies: 3
    Last Post: 2009-07-02, 12:44
  3. Bad dynamic heap objects found!
    By iecomdev in forum Mobile Java General
    Replies: 4
    Last Post: 2007-11-06, 15:50
  4. Memory Heap & OS Heap
    By earamsey in forum Mobile Java General
    Replies: 0
    Last Post: 2007-08-27, 23:02
  5. Available Heap Memory
    By fesperman in forum Mobile Java General
    Replies: 3
    Last Post: 2005-12-20, 11:20

Posting Permissions

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