Java Runtime Process Memory Leak?

This might be a while back, but I was using Java to call external process (e.g. Bash scripts / Windows Dos Scripts) and over time there were memory leaks that eventually would cause crashes. Read about it in forums do.

Does anyone know if this has been fixed in the latest version of Java and/or if it still occurs and ways to get around it?

Thanks.

The leak is usually due to the called process not freeing resources before being terminated.

See the last comment at https://forums.oracle.com/forums/thread.jspa?threadID=2185235

Similar problems occur when calling C code through JNI. It is easy to forget you have to do your own cleanup and dispose of any allocated buffers.

If you spawn external processes, you need to make sure to use Process.waitFor() to ensure the process properly exits and closes stdin/stdout/stderr. If you don’t, the process may stick around and not only keep consuming overall system memory, it’ll also keep the file descriptor open til you run out.

At the same time, not reading stdout/stderr may block the process, which means waitFor() never returns.

So, read stdout/stderr from two other threads, and block on waitFor() on the current thread.

Can you do it all on the current thread by something like the following pseudo code?

while !eof(stdin) || !eof(stderr) {
if !eof(stdin) read(stdin)
if !eof(stderr) read(stderr)
}
close(stdin)
close(stderr)
wait_for_process_termination_and_get_exit_code()

I suppose there’s a risk that the process could generate some more output after we’ve exited the while loop, which would cause the waitFor to block.

If we put the code to empty stdin and stderr on another thread, I assume that it must loop until after the main thread successfully gets the exit code, at which point the main thread must signal it to exit (needs a semaphore). That does mean that stdin and stderr can’t be explicitly closed, prior to the waitFor. Does the waitFor close the streams automatically?