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

Out Of Memory Exception

harbiharbi Posts: 10
edited 2009-08-12 06:48 in General Discussion
plz could any one help me ?
this my code ?
after some running time i got Out Of Memory exp?·



·import stamp.core.*;
·import java.lang.*;
public class Servo12 { // COM Port (9-pin serial)
final static int SERIAL_TX_PIN = CPU.pin0; // 2
final static int SERIAL_RTS_PIN = CPU.pin1; // 7
final static int SERIAL_CTS_PIN = CPU.pin2; // 8
final static int SERIAL_RX_PIN = CPU.pin3; // 3
static PWM Servo1 = new PWM(CPU.pin12,175,2304);
static PWM Servo2 = new PWM(CPU.pin13,175,2304);
static PWM Servo3 = new PWM(CPU.pin14,175,2304);
static PWM Servo4 = new PWM(CPU.pin15,175,2304);
static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
SERIAL_RTS_PIN, Uart.dontInvert, Uart.speed9600,
Uart.stop1 );
static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
SERIAL_CTS_PIN, Uart.dontInvert, Uart.speed9600,
Uart.stop1 );
static StringBuffer buffer = new StringBuffer(128);
static· char[noparse]/noparse name = new char[noparse][[/noparse]3] ;
static· char[noparse]/noparse value = new char[noparse][[/noparse]3] ;
static char c;
static String m;
static String Sname;
static String Svalue;
static int svvalue;
static int fsvvalue;
static int pos;
static void bufferMessage(){
c = 0xff;
while (c != '\n'){
if(rxUart.byteAvailable()){
c = (char)rxUart.receiveByte();
buffer.append(c);
}
}
·}// end bufferMessage
public static void main(){
do{
buffer.clear();
bufferMessage();
m= buffer.toString();
name [noparse][[/noparse]0]= m.charAt(0);
name [noparse][[/noparse]1]= m.charAt(1);
name [noparse][[/noparse]2]= m.charAt(2);
Sname= new String(name);
value [noparse][[/noparse]0]= m.charAt(4);
value [noparse][[/noparse]1]= m.charAt(5);
value [noparse][[/noparse]2]= m.charAt(6);
Svalue= new String(value);

svvalue=· Integer.parseInt(Svalue);
if (svvalue<130)
fsvvalue= 130+(svvalue/3);
if (svvalue>=130)
fsvvalue =((svvalue -130)/3)+175 ;
switch ( name [noparse][[/noparse]2] ) {
····· case '1':
······· Servo1.update(fsvvalue,2304);
······· break;
····· case '2':
······· Servo2.update(fsvvalue,2304);
······· break;
····· case '3':
······· Servo3.update(fsvvalue,2304);
······· break;
······ case '4':
······· Servo4.update(fsvvalue,2304);
······· break;
····· }
}while (true);
} // end main
} // end class declaration

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-08-11 08:52
    The javelin has no garbage collection, so you must
    avoid creating objects within a loop.

     import stamp.core.*;
     import java.lang.*;
    public class Servo12 { // COM Port (9-pin serial)
    final static int SERIAL_TX_PIN = CPU.pin0; // 2
    final static int SERIAL_RTS_PIN = CPU.pin1; // 7
    final static int SERIAL_CTS_PIN = CPU.pin2; // 8
    final static int SERIAL_RX_PIN = CPU.pin3; // 3
    static PWM Servo1 = new PWM(CPU.pin12,175,2304);
    static PWM Servo2 = new PWM(CPU.pin13,175,2304);
    static PWM Servo3 = new PWM(CPU.pin14,175,2304);
    static PWM Servo4 = new PWM(CPU.pin15,175,2304);
    static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
    SERIAL_RTS_PIN, Uart.dontInvert, Uart.speed9600,
    Uart.stop1 );
    static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
    SERIAL_CTS_PIN, Uart.dontInvert, Uart.speed9600,
    Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(128);
    static  char[noparse][[/noparse]] name = new char[noparse][[/noparse]3] ;
    static  char[noparse][[/noparse]] value = new char[noparse][[/noparse]3] ;
    static char c;
    static String m;
    static String Sname;
    static String Svalue;
    static int svvalue;
    static int fsvvalue;
    static int pos;
    static void bufferMessage(){
    c = 0xff;
    while (c != '\n'){
    if(rxUart.byteAvailable()){
    c = (char)rxUart.receiveByte();
    buffer.append(c);
    }
    }
     }// end bufferMessage
    public static void main(){
    do{
    buffer.clear();
    bufferMessage();
    [color=red][b]m= buffer.toString();[/b]
    [/color]name [noparse][[/noparse]0]= m.charAt(0);
    name [noparse][[/noparse]1]= m.charAt(1);
    name [noparse][[/noparse]2]= m.charAt(2);
    [color=red][b]Sname= new String(name);[/b]
    [/color]value [noparse][[/noparse]0]= m.charAt(4);
    value [noparse][[/noparse]1]= m.charAt(5);
    value [noparse][[/noparse]2]= m.charAt(6);
    [b][color=red]Svalue= new String(value);[/color][/b]
    
    svvalue=  Integer.parseInt(Svalue);
    if (svvalue<130)
    fsvvalue= 130+(svvalue/3);
    if (svvalue>=130)
    fsvvalue =((svvalue -130)/3)+175 ;
    switch ( name [noparse][[/noparse]2] ) {
          case '1':
            Servo1.update(fsvvalue,2304);
            break;
          case '2':
            Servo2.update(fsvvalue,2304);
            break;
          case '3':
            Servo3.update(fsvvalue,2304);
            break;
           case '4':
            Servo4.update(fsvvalue,2304);
            break;
          }
    }while (true);
    } // end main
    } // end class declaration
    

    I made the lines you need to change appear red.

    regards peter
  • harbiharbi Posts: 10
    edited 2009-08-11 09:34
    could you help me in this
    iam fresh in the filed and i need help ?
    most imprtant is 3nd char in the buffer and last three the rest is not used
    and the last three i need them as int vaule


    thanks 4 u response
  • harbiharbi Posts: 10
    edited 2009-08-11 10:26
    I made some cahnges


    import stamp.core.*;
    import java.lang.*;

    public class Servo12 { // COM Port (9-pin serial)
    final static int SERIAL_TX_PIN = CPU.pin0; // 2
    final static int SERIAL_RTS_PIN = CPU.pin1; // 7
    final static int SERIAL_CTS_PIN = CPU.pin2; // 8
    final static int SERIAL_RX_PIN = CPU.pin3; // 3

    static PWM Servo1 = new PWM(CPU.pin12,175,2304);
    static PWM Servo2 = new PWM(CPU.pin13,175,2304);
    static PWM Servo3 = new PWM(CPU.pin14,175,2304);
    static PWM Servo4 = new PWM(CPU.pin15,175,2304);

    static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
    SERIAL_RTS_PIN, Uart.dontInvert, Uart.speed9600,
    Uart.stop1 );
    static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
    SERIAL_CTS_PIN, Uart.dontInvert, Uart.speed9600,
    Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(128);
    static StringBuffer buffer1 = new StringBuffer(128);

    static char c;
    static char c1;


    static int svvalue;
    static int fsvvalue;

    static int pos;
    static void bufferMessage(){
    c = 0xff;
    while (c != '\n'){
    if(rxUart.byteAvailable()){
    c = (char)rxUart.receiveByte();
    buffer.append(c);
    }
    }
    }// end bufferMessage

    public static void main(){
    do{

    buffer.clear();
    buffer1.clear();
    bufferMessage();


    buffer1=buffer.subString(4,7);
    svvalue= Integer.parseInt(buffer1);
    //System.out.println(svvalue);

    if (svvalue<130)
    fsvvalue= 130+(svvalue/3);
    if (svvalue>=130)
    fsvvalue =((svvalue -130)/3)+175 ;

    c1 = buffer.charAt(2) ;
    // System.out.println(c1);
    switch ( c1 ) {
    case '1':
    Servo1.update(fsvvalue,2304);

    break;

    case '2':
    Servo2.update(fsvvalue,2304);

    break;

    case '3':
    Servo3.update(fsvvalue,2304);

    break;
    case '4':
    Servo4.update(fsvvalue,2304);

    break;
    }
    buffer.delete(0,7);
    }while (true);
    } // end main

    } // end class declaration




    //////////////////////>>>>>>>>>>.
    but another exception came >>>>>>>index out of bound exception


    thanks
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-08-11 13:47
    Your program has several parts.
    You want to input some parameters via the message window,
    convert these parameters to a suitable format (like binary integer values)
    and do something with those converted parameters.

    Attach you will find a class and a test program that deals with the first part.
    It assembles a string from the message window, but unlike bufferMessage()
    you can still do other things if you want to.
    Once a string has been received, you must parse it to divide the string
    into substrings.

    For example, if you input
    The lazy fox jumped 23 times over the gate
    then after parsing the substrings are
    The
    lazy
    fox
    jumped
    23
    times
    over
    the
    gate

    These classes are very useful for input commands.
    Try the test program. It uses the Format class.
    After that, we can use the class for your program,
    which will simplify your program sonsideribly,
    because converting to binary values will be easy.

    Place the classes in folder ...\lib\stamp\core

    regards peter
  • harbiharbi Posts: 10
    edited 2009-08-11 22:32
    <<<<<<<<<You want to input some parameters via the message window,>>>>>>>>
    no my dear, i am not reading from Terminal· I try to recive string from serial port and use it to move some servos in·some ·way· that i need .
    and· that servo is controling by joystick that i already programmed by java.

    I need way to clean mem at every loop if it is Possible.
    becuse the joystick will keep sending commands to keep servos in the right position
    and·
    Again many many thanks for you peter·.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-08-12 06:48
    Sorry, my mistake. Must have happened because I was writing
    code using the JIDE port.

    Attached is a reworked version of your program.
    I removed all the String and StringBuffer types and
    used only char arrays. Saves you alot of·code as you
    don't need to convert between types.

    I also used Format.bscanf() to convert a decimal string into a binary value.

    The memory leak should be resolved now.

    regards peter
Sign In or Register to comment.