Out Of Memory Exception
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
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
avoid creating objects within a loop.
I made the lines you need to change appear red.
regards peter
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
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
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
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·.
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