LCD.drawString memory leak

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

LCD.drawString memory leak

Postby PheniX » Thu Apr 12, 2007 8:57 am

Hi
I am using leJos 0.2 alfa.
I have noticed a memory leak in LCD.drawString, here is a small class displaying the memory lost for each call:
Code: Select all
import lejos.nxt.*;
/**
 * Shows the memory leak in LCD.drawString
 * @author Andrea Sterbini
 *
 */
public class LCDdrawStringMemoryLeak {
   public static void main(String[] args) {
      int old, mem;
      while (!Button.ENTER.isPressed()) {
         LCD.clear();
         old = (int)System.getRuntime().freeMemory();
         LCD.drawString("LCD.drawString", 0, 0);
         mem = (int)System.getRuntime().freeMemory();
         LCD.drawInt(mem, 0, 1);
         LCD.drawInt(old - mem, 0, 2);
         LCD.refresh();
         try {Thread.sleep(500);} catch (Exception e){}
      }
      Sound.playTone(440, 100);
   }
}


I don't have an arm toolchain installed, thus I cannot track the error in the VM.
Anyway, the drawString code in the VM file nextvm/platform/nxt/native.c is:
Code: Select all
...
  case drawString_4Ljava_3lang_3String_2II_5V:
    {
      byte *p = word2ptr(paramBase[0]);
      int len, i;
      Object *charArray = (Object *) word2ptr(get_word(p + HEADER_SIZE, 4));

      len = charArray->flags.arrays.length;
      {
        char buff[len + 1];
        char *chars = ((char *) charArray) + HEADER_SIZE;

        for (i = 0; i < len; i++)
          buff[i] = chars[i + i];
        buff[len] = 0;
        display_goto_xy(paramBase[1], paramBase[2]);
        display_string(buff);
      }
    }
    return;
...


Perhaps the problem is with the dinamic allocation of vector 'buff'?
Could someone try to recompile it using a constant-sized buffer (e.g 255 chars)?

AndreaS
PheniX
New User
 
Posts: 4
Joined: Sun Feb 11, 2007 11:30 am

Postby XtaZy » Thu Apr 12, 2007 11:58 am

You are aware that everytime you run that loop, a NEW string is generated? In Java, strings are immutable, that makes

doSomething("string");

an "alias" of doSomething(new String("string));

So try the routine like this
Code: Select all
String text = "Some text";

while {
...
LCD.drawString(text, 0, 0);
...
}


If my theory is correct, that should not use more and more memory for each time...

The same goes for actions like this:
Code: Select all
String s1 = "text1"; // Creates s1
s1 = "text2"; // Re-Creates s1 with "text2". Leaving "text1" in memory, without a reference


-X-
XtaZy
New User
 
Posts: 1
Joined: Thu Apr 12, 2007 11:53 am

Postby PheniX » Mon Apr 23, 2007 6:34 pm

:oops:
PheniX
New User
 
Posts: 4
Joined: Sun Feb 11, 2007 11:30 am

Postby bbagnall » Wed Apr 25, 2007 4:20 pm

This isn't a problem in standard Java so you shouldn't be embarrassed. It's because leJOS NXJ lacks a garbage collector.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 4 guests

more stuff