Shop OBEX P1 Docs P2 Docs Learn Events
Out of Memory — Parallax Forums

Out of Memory

TE44TE44 Posts: 42
edited 2007-01-15 15:25 in General Discussion
Working with a loop - I wanted to print a value - I noticed that if I do the following I get no error

import stamp.core.*;

public class MEM {
· static int val ;

public static void main(){
do{
val = val +1;
System.out.println(val);
}while(true);
}
}

How ever if I do the following - I get a memory error after a count of approx 2000

import stamp.core.*;
public class MEM {
· static int val ;

public static void main(){
do{
val = val +1;
System.out.println(String.valueOf(val));
}while(true);
}
}

Can I clear a buffer or something ?

I have a routine where by I want to send an int·out·via the Uart·- I can not
use the following

txUart.sendString(val);

because val is an integer so I tried the following -

txUart.sendString(String.valueOf(val));

which successfully sent the values but I get out of memory errors after about 2000

I wrote the above routine 'MEM'·to see where my problem was and it was with (String.valueOf(val));
is there a way around this?

Any help greatly appreciated.....
·

Comments

  • TE44TE44 Posts: 42
    edited 2007-01-15 04:10
    I am aware the gc is not present - but is there a way for me to clear this ??

    or is it simply rubbish code ??
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-01-15 04:59
    The problem with String.valueOf() is that it creates a new String that is only
    used once. You can of course use StringBuffer and then reuse that.
    The easiest way is to use the Format class.

    char[noparse]/noparse asciiNum = new char[noparse][[/noparse]7]; //holds ascii representation: sign,5decimals,closing null
    int value = 123;
    Format.sprintf(asciiNum,"%d",value); //convert int to ascii
    tx.sendString(asciiNum); //transmit all characters·exclusive the closing null

    You should use my adapted Uart class.
    Rename the current Uart.java to Uart_org.java
    then copy my Uart.java from
    http://tech.groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/core/
    into folder .../lib/stamp/core
    You will find the Format class here:
    http://tech.groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/util/text/
    copy Format.java into folder .../lib/stamp/util/text
    Format allows you to print binary,octal,decimal,hexadecimal and unsigned values.

    regards peter
  • TE44TE44 Posts: 42
    edited 2007-01-15 15:25
    Peter,
    Thats great - both worked - also tried StringBuffer with a
    buffer.clear();
    and it works fine !

    You the man !
Sign In or Register to comment.