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

Discussion in 'Java' started by dh, Feb 2, 2004.

  1. dh

    dh Guest

    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....]
     
    dh, Feb 2, 2004
    #1
    1. Advertising

  2. dh wrote:
    > 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
     
    Thomas Weidenfeller, Feb 3, 2004
    #2
    1. Advertising

  3. dh

    Dario Guest

    dh wrote:

    > 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
     
    Dario, Feb 3, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mathias Herrmann

    disable stdout buffering ?

    Mathias Herrmann, Oct 4, 2005, in forum: C Programming
    Replies:
    7
    Views:
    744
    Michael Wojcik
    Oct 4, 2005
  2. Cecil Westerhof

    Non-buffering stdout

    Cecil Westerhof, Mar 25, 2008, in forum: Python
    Replies:
    0
    Views:
    265
    Cecil Westerhof
    Mar 25, 2008
  3. Robert Wells
    Replies:
    4
    Views:
    666
    Default User
    Jun 24, 2008
  4. Chris McDonald
    Replies:
    5
    Views:
    269
    Kenny McCormack
    Feb 22, 2009
  5. Qu0ll
    Replies:
    42
    Views:
    1,250
    Thufir Hawat
    Apr 13, 2009
Loading...

Share This Page