LCD.drawstring and threads

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

Moderators: 99jonathan, roger, imaqine

LCD.drawstring and threads

Postby esmetaman » Sat Sep 15, 2007 2:47 pm

Hi, I have developed a example to show how to make a OUTMEMORY using the method LCD.drawstring

Code: Select all
import lejos.nxt.*;

public class NXTCommTest {

   /**
    * @param args
    */
   public static void main(String[] args) {

      try{
         LCDUILite1 LCDUIObj = new LCDUILite1();
         //NXTBotMenu NBM = new NXTBotMenu();
         //NXTCommsListener NCL = new NXTCommsListener();
         LCDUIObj.start();
         //NBM.start();
         //NCL.start();
      }catch(Exception e){
         
      }
   }

}



Code: Select all
import javax.microedition.lcdui.*;
import lejos.nxt.*;

public class LCDUILite1 extends Thread {

   public LCDUILite1(){
      
   }
   
    public void run()
    {
       
        while(true)
        {
            try{
              LCD.drawString("asdf",0,0);
                int battery = (int)(Battery.getVoltageMilliVolt() );
                LCD.drawInt(battery,0,1);
                int memory = (int)(Runtime.getRuntime().freeMemory());
                LCD.drawInt(memory,0,2);
              LCD.refresh();
           }catch(Exception  e){

           }
        }
    }

}
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby esmetaman » Sun Sep 16, 2007 10:29 am

Code: Select all
import lejos.nxt.*;

public class BugLCDDrawString {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
       
        LCD.drawString("Esmeta Labs",0,0);
        LCD.drawString("Bug detected",0,1);
        while(true)
        {
            try{
              LCD.drawString("LCD.drawString()",0,2);
              int battery = (int)(Battery.getVoltageMilliVolt() );
              LCD.drawInt(battery,0,3);
              int memory = (int)(Runtime.getRuntime().freeMemory());
              LCD.drawInt(memory,0,4);
              LCD.refresh();
           }catch(Exception  e){

           }
        }
   }

}
[/code]
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Sun Sep 16, 2007 5:51 pm

It is not a bug in LCD.drawString that leaks memory. Any use of a string literal in a loop creates a new object each time round the loop. You need to create a named string outside the loop.
lawrie
leJOS Team Member
 
Posts: 929
Joined: Mon Feb 05, 2007 1:27 pm

I agree with you

Postby esmetaman » Sun Sep 16, 2007 6:30 pm

Thanks Lawrie,

if you test the following example, LCD.drawString doesnt forget any byte.

Code: Select all
import lejos.nxt.*;

public class BLCDDrawString2 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
       
      String message;
      
      message = "Esmeta Labs";      
        LCD.drawString(message,0,0);
        message = "Bug detected";
        LCD.drawString(message,0,1);
        message = "LCD.drawString()";
        while(true)
        {
            try{
               
              LCD.drawString(message,0,2);
              int battery = (int)(Battery.getVoltageMilliVolt() );
              LCD.drawInt(battery,0,3);
              int memory = (int)(Runtime.getRuntime().freeMemory());
              LCD.drawInt(memory,0,4);
              LCD.refresh();
           }catch(Exception  e){

           }
        }
   }

}



but if you create a string in a loop, The program will forget memory:

Code: Select all
import lejos.nxt.*;

public class BLCDDrawString3 {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
       
      String message;
      
      message = "Esmeta Labs";      
        LCD.drawString(message,0,0);
        message = "Bug detected";
        LCD.drawString(message,0,1);
        while(true)
        {
            try{
               message = "LCD.drawString()";   
              LCD.drawString(message,0,2);
              int battery = (int)(Battery.getVoltageMilliVolt() );
              LCD.drawInt(battery,0,3);
              int memory = (int)(Runtime.getRuntime().freeMemory());
              LCD.drawInt(memory,0,4);
              LCD.refresh();
           }catch(Exception  e){

           }
        }
   }

}



Conclussions: It is neccesary to define the messages.

Thanks Lawrie
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 299
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 1 guest

more stuff