memory management

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

Moderators: 99jonathan, roger, imaqine

memory management

Postby edalquist » Wed Aug 15, 2007 11:51 pm

I have two little programs that do the same thing. One leaks memory very quickly the other doesn't at all. Could someone point out the problem with the leaky version?

Good version:
Code: Select all
import lejos.nxt.LCD;

public class MemTest {
    private static String M = "M:";

    public static void main(String[] args) {
        while (true) {
            LCD.clear();
            LCD.drawString(M, 0, 7);
            LCD.drawInt((int)Runtime.getRuntime().freeMemory(), 5, 3, 7);
            LCD.refresh();
           
            try {
                Thread.sleep(1);
            }
            catch (InterruptedException ie) {
                //oops
            }
        }
    }
}


The leaky version:
Code: Select all
public class MemTest {
    private static Display display = new Display();

    public static void main(String[] args) {
        while (true) {
           display.updateDisplay();

            try {
                Thread.sleep(1);
            }
            catch (InterruptedException ie) {
                //oops
            }
        }
    }
}



import lejos.nxt.LCD;

public class Display {
    private static final String M = "M:";
   
    public void updateDisplay() {
        LCD.clear();
       
        LCD.drawString(M, 0, 7);
        LCD.drawInt((int)Runtime.getRuntime().freeMemory(), 5, 3, 7);
       
        LCD.refresh();
    }
}


Everything is declared static, exact same operations on the lejos APIs, can't see where I'm leaking from.
edalquist
Novice
 
Posts: 48
Joined: Sun Apr 08, 2007 5:55 pm

Postby edalquist » Thu Aug 16, 2007 10:25 pm

So looking at my post again I just noticed something innocuous . The leaky version declares the static field as final where as the good version doesn't. I just did a few more tests and it appears using the 'final' keyword on any variable (field, parameter, local) causes a leak every time the variable is referenced (like creating a new instance each time).

So the moral of the story is .... don't use the final keyword.
edalquist
Novice
 
Posts: 48
Joined: Sun Apr 08, 2007 5:55 pm

Postby bbagnall » Mon Aug 20, 2007 5:36 pm

Thanks for finding that. There must be something in the drawString() method that requires a non-final String. We'll add that to our list.
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby edalquist » Mon Aug 20, 2007 10:58 pm

No problem, is there an appropriate way to report bugs or do the forums work?

PS ... great software, I'm having a lot of fun being able to hack in Java on the NXT. If I could remember any of my C++ from college I'd offer to help with the OSX port but I'm not sure how much help I could be there.
edalquist
Novice
 
Posts: 48
Joined: Sun Apr 08, 2007 5:55 pm

Postby bbagnall » Tue Aug 21, 2007 9:44 pm

There's no real C++ coding required for the OSX port. Just need to be able to build it, which many have already done. We can't seem to get any Mac users to volunteer their binaries (preferably the Universal binaries).
User avatar
bbagnall
Site Admin
 
Posts: 392
Joined: Fri Aug 04, 2006 4:03 pm

Postby edalquist » Wed Aug 22, 2007 12:42 am

Hrm, I may see if I can find some time in a week or two to look at setting up an XCode project to do the build and contribute that back. I could probably handle doing OS X universal builds for the project.
edalquist
Novice
 
Posts: 48
Joined: Sun Apr 08, 2007 5:55 pm


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider], lgjzilpq13 and 4 guests

more stuff