Shop OBEX P1 Docs P2 Docs Learn Events
out of memory after about 2 minutes — Parallax Forums

out of memory after about 2 minutes

verobelverobel Posts: 81
edited 2006-06-01 02:32 in General Discussion
Hi fellow javaliners..
I have written a program called multi4.java that currently tries to read the temperature form a DS1620 chip and then displays it on the LCD 2x16. The temperature code is done in a class called temp1 where I assembled code from ShiftDS1620.java. The LCD is done with the SerialLCD class from parallax.

The program seems to run OK for about 2 minutes and then I get an error saying
error IDE-0028 ..java.lang.outOfmemory
Debug says I use 4836 of 32768 bytes.

Any ideas on what is wrong? programs attached
Thanks, John

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-30 21:03
    The problem is in multi4.java, the use of + to concatenate strings.
    This generates a temporary string that is only used once.
    And since the javelin has no garbage collection you run out of memory.

    Change the line
    ····· myLCD.write( "TempC=" + tmp);
    to
    ····· myLCD.write("TempC=");
    ····· myLCD.write(tmp);

    and you should be ok.
    I assume·that an int (eg. tmp) send to your LCD displays the·string representing
    the decimal value of that int.
    I am not sure if String + int is valid at all. String + tmp.toString() would be valid
    but generates a temporary string.

    regards peter
  • verobelverobel Posts: 81
    edited 2006-05-31 04:30
    in normal java (non micro) that sort of thing "TempC="+tmp is done all the time and is valid..in fact it is a feature of working with strings..you can concatenate/convert all types easily. But, I can see how a temporary string would be req'd. I guess I need to do something more along the line of pBasic DATA where characters are store in program data space and pointed at and printed from there. I also seem to remember reading something about using a BUFFER to avoid this. I guess I'll have to try to remember where I saw it. (Manual pg 41,104..)

    I tried you suggestion and it runs without running out off memory but I gete TempC= blank.. ie the tmp variable type int is not really acceptable to SerialLCD class.. in fact if you send and int it will send the its byte to the LCD which appears to do nothing.

    I tried this
    String stmp = new String("000"); // outside loop at start
    ...
    stmp = Integer.toString(tmp); // inside measure and display loop (reuse same String)

    thanks, John
  • verobelverobel Posts: 81
    edited 2006-05-31 04:32
    woops I spoke too soon.. that failed too..but after about 4 minutes
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-31 05:47
    The use of the Format class will definitely solve this.

    char[noparse]/noparse buf = new char[noparse][[/noparse]40]; //set useful size

    then

    Format.sprintf(buf,"TempC = %d",tmp); //generate null terminated ascii string

    If your lcd class does not support asciiz strings add this to your lcd class:

    public void write(char[noparse]/noparse s) {
    · int i=0;
    · while (s[noparse][[/noparse]i]!=0) write(s[noparse][[/noparse]i++]); //print all ascii characters
    }

    In your application you can then use
    myLCD.write(buf);

    The Format class is here:
    http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/util/text/

    regards peter
  • verobelverobel Posts: 81
    edited 2006-05-31 20:49
    It would appear that the 'core' javaeline java libraries do not have StringBuffer.insert(ipos,integer) so that even if you make a reuseable stringer buffer you are going to have some difficulty writing integer values into a text string like "temperat=nn". string = Integer.toString(tmp) produces memory leaks apparently.. so I can see why we need some auxiliary library (adapted from C) to handle text and conversions...

    I've downloaded the Format and put it in the library..path and will try to use that now..
    Thanks, John
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-05-31 21:44
    StringBuffer does have a method append(int val) that appends the ascii (decimal) representation
    of val to the stringbuffer, but you cannot insert it at a specified index.
    Personally I dislike all the String and StringBuffer classes as they are large, use
    temporary strings and do not really help in assembling ascii strings in an easy way.

    The Format class I wrote does not use Strings, just char[noparse]/noparse and also
    has a bprintf() that allows assembling a string for multiple parameters.
    And it does this for binary, octal, decimal, hex, char and unsigned parameters
    and for asciiz strings.

    regards peter
  • verobelverobel Posts: 81
    edited 2006-06-01 01:47
    ok..I have it working now. I had made some method called prepLines(..) to convert ints to chars and then build up 2 line buffers for output to LCD. Then I looked at your last post and tried that (which seems easier) ..so now I have prepLines2(...). So far, no out of memory error. Latest Multi4.java attached. SerialLCD.java updated to include write(char [noparse]/noparse).

    my target display was

    Prog:x 100S:xxx························································································· Lgt:33 Pot:44 29

    which reproduces a pBasic program that allowed 1 of 3 programs to run and would display the time it took to do 100scans (in seconds). Prog:1 was A/D convert 2 voltages related to light sensor and potentiometer and show value 0->99. Prog:3 was to read temperature from DS1620 and display at end of 2nd line (no room for txt). Prog:2 was to run both programs 1 and 3.

    still have a ways to go tot get it all implemented in javalin..

    thanks, John
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-01 02:32
    The assembling code looks good.
    From that code I see line1 and line2 filled with 16 chars.
    line1 and line2 are declared as 20byte arrays.
    If your lcd is a 2x20 type I suggest to define line1 and line2 as
    21byte arrays (you must reserve space for the closing null).

    regards peter
    ·
Sign In or Register to comment.