Search code examples
javamultithreadingmemory-leaksrunnable

Detecting memory leak in Java thread


I am working on a runnable that reads in input from a System stream and analyses the string that comes in, the program works well for a few days and then stops working, I am guessing it might be due to memory leak in my thread, but I cant seem to find it, can anyone suggest if there is something wrong with my code.

@Override
public void run() {
    BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
        try {
            String inputStr;
            if ((inputStr = bufReader.readLine()) != null) {
                //do something
            } else {
                break;
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Solution

  • If the standard input is provided via a batch redirection, an EOF would cause the program execute the break, exiting the while loop and ending thus the thread.