3

I am facing a strange problem with memory in Java ME.

here is a part of my code:

int variable=1;

while (true) {
  if (variable==2) {
    display = Display.getDisplay(this);         
    MyCanvas mc = new MyCanvas(this);   // MyCanvas is a runnable object
    mcT = new Thread(mc);    // new thread for MyCanvas
    mc.repaint();
    display.setCurrent(mc);  
    mcT.start();  // run thread
    }

  if (variable==1) {
    // Do some other stuff
    }
  }

The problem is that although still the variable is set to 1, so it does not come through the if (variable==2) condition the program consumes 300kB more memory than when I delete the code after condition if (variable==2).

As far as I know the code should by executed and the objects shall be created only when I set variable to value 2. But it consumes the memory also when the code after condition "if (variable==2)" is not executed.

Why does this happen?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • how do you find out about memory consumption? are you testing it in emulator? – gnat Sep 04 '12 at 19:22
  • @gnat he probably prints `Runtime.freeMemory()`. Do you have many classes and resources? Do you use proguard? – Eugen Martynov Sep 05 '12 at 05:50
  • You might initialize other objects in other areas of your code – PHPFan Dec 02 '13 at 08:58
  • My guess is that, when you're not referring to MyCanvas, then maybe it gets obfuscated out? But as soon as there's a reference to it, it'll be kept. 300kb does sound like a lot though. – mr_lou Feb 24 '14 at 16:18

0 Answers0