0

I am writing lot of data to the stdandard output and I remark that according to the output console the program execution time is variable. The program is slower on NetBeans console than in Windows cmd for example.

So i think writing to stdout fill a buffer and writing become blocking when this buffer is full (the console output doesn't consume fast enough).

I reproduce this behavior with a Java program.

Here a program that output data .

public class Output {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        for (int i = 0; i < 10_000; i++) {
            System.out.println("Awesone Output ");
        }

        System.out.println("Total time : " + (System.currentTimeMillis() - start));
    }

}

And here a program that consume data from the above program

public class StdoutBlocking {

    public static void main(String[] args) throws IOException, InterruptedException {

        ProcessBuilder processBuilder = new ProcessBuilder("java", "stackoverflowDemo.StdoutBlocking.Output");
        processBuilder.directory(new File("C:/Users/me/Documents/NetBeansProjects/tmp/build/classes"));
        Process process = processBuilder.start();

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        String lastLine = null;

        while ((line = reader.readLine()) != null) {
            Thread.sleep(10);
            lastLine = line;
        }
        System.out.println("done : " + lastLine);

    }
}

I launch the second one that start output and consume its output. Without sleep the Output program is really quick with it's very slow.

So what is the size of this buffer ??

This is just for my Curiosity i'am not trying to achieve Something particular

user43968
  • 2,049
  • 20
  • 37

2 Answers2

3

Default buffer size for System.out and System.err is 8192. You can see it by looking the source code of it's methods like println(String). They use BufferedWriter with default buffer size.

SomeFire
  • 170
  • 4
  • I think you aren't looking at the right buffer. I think it's an os setting my first program could be C, python bash or anything else. I am not very sure if someone could confirm. – user43968 Jul 03 '18 at 18:30
1

Without sleep the program is really quick with it's very slow. this has no relation to buffer size. Your program sleeps at least 10 milliseconds for ach line of output (10000 lines). So with Thread.sleep(10) your program does nothing for at least 100 seconds. That is why it is slow with Thread.sleep().

Regarding performance of System.out.println() check this question: Why is System.out.println so slow?

The underlying OS operation (displaying chars on a console window) is slow because

1.The bytes have to be sent to the console application (should be quite fast)

2.Each char has to be rendered using (usually) a true type font (that's pretty slow, switching off anti aliasing could improve performance, btw)

3.The displayed area may have to be scrolled in order to append a new line to the visible area (best case: bit block transfer operation, worst case: re-rendering of the complete text area)

Also you could find this in Javadoc for Process class

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

For UNIX you could check this link: https://unix.stackexchange.com/questions/11946/how-big-is-the-pipe-buffer

Community
  • 1
  • 1
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • In my example there is 2 program obviously i am talking about the speed of the first one that as no sleep!!! The first one is only slowed by the second one reading stdout two slowly. The second program is emulating the console. And ot can't be about rendering because in me test there is almost nothing printed on screen – user43968 Jul 03 '18 at 18:24
  • To check if slowness introduced by printing to a screen you could replace `System.out.println()` with printing to a file on a file system in the first program. – Ivan Jul 03 '18 at 18:29
  • I am not printing anywhere "awesome output" i am just consuming it from the second program. If i consume it slowly the first program become slow if i consume it quickly it become faster. I am wondering why ??? I am not trying to optimize anything i am wondering why consumption speed affect producing speed. It's two process independant the only common things should be a buffer managed by the os and i am looking for info about it. have you really understand the phenomenon ??? – user43968 Jul 03 '18 at 18:39