IBM Java Process.exec() ... mad buffering of stdout

D

dh

IBM Java Process.exec() ... mad buffering of stdout

It buffers stdout without limit ...
so if your Java program doesn't keep up with the Process output then
memory fills up!
I wish it just blocked instead because my java program is loosing
the race against the C program that it exec()s.

what can I do?

Here is a small example that illustrates the issue ...

5523:fosters:~: cat Test.java

class Test {
public static void main(String[] args) {
try {
Process p =
Runtime.getRuntime().exec("/usr/bin/yes");
for (;;) {
Thread.sleep(1000);
System.out.println("tick");
Thread.sleep(1000);
System.out.println("tock");
}
}
catch (Exception x) {
x.printStackTrace();
}
}
}


5524:fosters:~: rm *.class
5525:fosters:~: /usr/local/IBMJava2-13/bin/javac Test.java
5526:fosters:~: /usr/local/IBMJava2-13/bin/java Test
tick
tock
tick
tock
java.lang.OutOfMemoryError
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
[etc....]
 
T

Thomas Weidenfeller

dh said:
IBM Java Process.exec() ... mad buffering of stdout

It buffers stdout without limit ...
so if your Java program doesn't keep up with the Process output then
memory fills up!
I wish it just blocked instead because my java program is loosing
the race against the C program that it exec()s.

what can I do?

Here is a small example that illustrates the issue ...

5523:fosters:~: cat Test.java

class Test {
public static void main(String[] args) {
try {
Process p =
Runtime.getRuntime().exec("/usr/bin/yes");

You could try to work around that on the Unix site. Try piping your
stdout through something like

until expr "`dd bs=4k count=32 3>&2 2>&1 1>&3`" : '0\+0 records in'
/dev/null 3>&1
do
sleep 1
done

/Thomas
 
D

Dario

dh said:
IBM Java Process.exec() ... mad buffering of stdout

It buffers stdout without limit ...
so if your Java program doesn't keep up with the Process output then
memory fills up!

To avoid buffering without limit
you have to consume or to close
p.getInputStream() and p.getErrorStream().

- Dario
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top