x.Hm have a client server java program where the server accepts query requests f
ID: 3615372 • Letter: X
Question
x.Hm have a client server java program where the server accepts query requests from clients and sometimes returns the result. I keep getitng outofmemory error. I search online and found you can increase the runtime java heapspace with Xmx128m... Also, something might now be getting deleted properly. I have closed all connections and most other things that I possibly could.The only things I don't close are
//isr.close(); is.close();
//os.close(); osw.close();
but when I try and close them, not all the input goes through and I get a wierd error in the client program.
I would appreciate any ideas-suggestions
Thanks
Ryan
Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2894)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:117)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:589)
at java.lang.StringBuffer.append(StringBuffer.java:337)
at stockserver.MultipleSocketServer.run(MultipleSocketServer.java:57)
at java.lang.Thread.run(Thread.java:636)
public void run() {
StringBuffer process = null;
String returnCode;
returnCode = "" + ((char) 13);
try{
int character;
while (true) {
BufferedInputStream is = new BufferedInputStream(conn.getInputStream());
InputStreamReader isr = new InputStreamReader(is);
process = new StringBuffer();
while((character = isr.read()) != 13) {
process.append((char)character);
}//
//Remove terminating symbol
int y = process.indexOf(Character.toString((char)13));
String x = process.substring(0, process.length()-1);
//Find out what the process should
String type = x.substring(0, 2);
String sqlstmt = x.substring(2, x.length());
System.out.println("sqlstmt is " + sqlstmt + " type is " + type);
if(type.compareTo("01") == 0){
System.out.println("Inside 01 " + sqlstmt);
InsertNewStock(sqlstmt);
}
else if(type.compareTo("02") == 0){
}
else if (type.compareTo("03") == 0)
{System.out.println("Inside 03");
returnCode = ReturnQuery(sqlstmt);
}
System.out.println("process " + process.toString());
//need to wait 10 seconds for the app to update database
try {
Thread.sleep(10000);
}
catch (Exception e){}
BufferedOutputStream os = new BufferedOutputStream(conn.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
System.out.println("Writing return statement " + returnCode);
returnCode = returnCode + (char) 13;
osw.write(returnCode);
osw.flush();
//isr.close(); is.close();
//os.close(); osw.close();
}
}
catch (IOException e) {}
try {
&nb
Explanation / Answer
The OutOfMemoryError is always caused by infinite loop. The following loop only works if the program is running inWindows. The new line character in Linux or Unix is 0x0a ( 10 indecimal), so you cannot detect the end of line if you only checkwhether the character is 0x0d (13 in decimal). while((character = isr.read()) != 13) {process.append((char)character);
}// If that is not the case, you can try to use System.gc() tosuggest Java VM to start garbage collection, which could alleviatethe high memory usage to some extent.