简体   繁体   中英

BufferedReader.readLine() Creating threads()?

I am trying to make a server that can have multiple users, im only creating 2 Threads but my BufferedReader.readLine() seems to be making multiple threads and causing a OutOfMemory Exception, I dont see why its doing this?

Function causing Exception:

public void run() {
    try {
        Username = Input.readLine();
    } catch (IOException e1) {
        disconnect();
    }
    String lastInput = null;
    try {
        while ((lastInput = Input.readLine()) != null) {
            System.out.println(lastInput);
            if (lastInput.startsWith("Chat: ")) {
                sendToAllClients(lastInput.substring(7));
            }
        }
    } catch (IOException e) {
        disconnect();
    }
}

The Exception:

Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Main.User.run(User.java:46)
at java.lang.Thread.run(Unknown Source)

Note: the Username = Input.readLine() is making the Exception

To avoid your infinite loop and therefore your OOM exception:

try{
  while ((currentInput=Input.readLine()) != null) {
     if (currentInput.startsWith("Chat: "))
       sendToAllClients(currentInput.substring(7));
  }
catch (IOException e) { //bad to swallow exception:  let's the method throw it or make something with it here}

readLine() does not create threads.

If 'lastInput' is null you should exit the loop and close the stream.

And if you get an exception, log it or print it, close the stream, and break.

Out of memory heap space comes into picture is the program is going in an infinite loop.

Your code :

 while (true) {
        try {
            lastInput = Input.readLine();
        } catch (IOException e) {}
        if (lastInput != null) {
            System.out.println(lastInput);
            if (lastInput.startsWith("Chat: ")) {
                sendToAllClients(lastInput.substring(7));
            }
        }

says that the code inside the loop will run infinite many times without any condition acting as an exit condition. Even when an issue is coming : you are catching that exception and the code keeps on continuing inside the loop.

This leads to Out of Memory : Heap Space.

Suggesed solution :

while (true) 
{
    try 
    {
        lastInput = Input.readLine();
    } 
    catch (IOException e) 
    {
     break;
    }
    if (lastInput != null) 
        {
           System.out.println(lastInput);
            if (lastInput.startsWith("Chat: ")) 
            {
            sendToAllClients(lastInput.substring(7));
            }
        }
}

This loop would break as soon user enters any name that causes an exception(which actually acts as an exit condition foe the while loop)

EDIT

One area which I see could be the source of the issue could be :

lastInput.substring(7)

If the lastInput string is of huge size that could almost fill up the heap space of the JVM installed on the system, then calling a substring from the 7th character to the last character , would internally trigger the creation of new String (as Strings are immutable), & with not enough heap space left, substring execution gives an OutOfMemory exception .

first: check the loop of while of your program. second: set the parameter set JAVA_OPTS= -Xms32m -Xmx512m.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM