Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>Out of memory heap space</code> comes into picture is the program is going in an infinite loop.</p> <p>Your code :</p> <pre><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)); } } </code></pre> <p>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.</p> <p>This leads to <code>Out of Memory : Heap Space.</code></p> <p>Suggesed solution :</p> <pre><code>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)); } } } </code></pre> <p>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)</p> <p><strong>EDIT</strong></p> <p>One area which I see could be the source of the issue could be : </p> <pre><code>lastInput.substring(7) </code></pre> <p>If the <code>lastInput</code> string is of huge size that could almost fill up the <code>heap space of the JVM</code> installed on the system, then calling a <code>substring</code> from the <code>7th character to the last character</code>, would internally trigger the creation of new <code>String</code> (as Strings are immutable), &amp; with not enough heap space left, <code>substring</code> execution gives an <code>OutOfMemory exception</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload