Memory management

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

Moderators: 99jonathan, roger, imaqine

Memory management

Postby socrates » Thu Nov 01, 2007 12:36 pm

I understand there are some issues with memory clean-up.

What guidelines should be followed to ensure that memory leaking is kept to a minimum, or avoided?

Regards,
Rupert
socrates
New User
 
Posts: 12
Joined: Sun Oct 28, 2007 7:36 pm

Postby roger » Thu Nov 01, 2007 3:40 pm

Hi Rupert,
To avoid memory leaks, don't create new temporary objects, arrays or strings. So never use the new keyword in a line of code that can be executed more than once. If you need to reuse an object or array, declare and create it as a class-level field.
String literals are created as new objects every time the code encounters them, such as in LCD.drawString("hello",0,0); You can fill memory quickly by calling this in a loop. So define a field
String HELLO = "hello";
Then
for(int i = 0; i<10000; i++)LCD.drawString(HELLO,0,0);
is perfectly harmless.
I hope this helps.

Roger
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Postby DigitalOne » Fri Nov 02, 2007 7:28 pm

Hi Roger,

I tried that code with my NXT and all I get is a blank screen for several seconds and then it flashes and shuts down. I understand the how/why of the memory management portion of your code but I can't get it to run.

Here is the code I'm using since there is a good chance my n00b Java skills could be the reason.

Code: Select all
import lejos.nxt.LCD;

public class HelloWorld {
   public static void main(String[] args) {
      String s = "hello";
      for(int i = 0; i<10000; i++)LCD.drawString(s,0,0);
   }
}


Any help is appreciated help.

Thanks,
"I would kill everyone in this room for a drop of sweet beer." -Homer Simpson
User avatar
DigitalOne
New User
 
Posts: 7
Joined: Tue Oct 23, 2007 6:20 pm
Location: America's Wang

Postby gloomyandy » Fri Nov 02, 2007 10:07 pm

Hi,
You need to add a LCD.refresh() whenever you want to update the screen. Basically your program was working and completing. Under LeJos the system will shut down after a program completes....

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 4239
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby DigitalOne » Fri Nov 02, 2007 10:59 pm

Thanks Andy,

I new it had to be something dumb. Thanks to Roger as well, with the help of this thread I managed to write a program that reads sensor data and updates it to the LCD screen in real time. I'm going to post that code in the Projects section though.

//Matt
"I would kill everyone in this room for a drop of sweet beer." -Homer Simpson
User avatar
DigitalOne
New User
 
Posts: 7
Joined: Tue Oct 23, 2007 6:20 pm
Location: America's Wang


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff