views:

117

answers:

3

What would use more memory if it were to output a message that was 15,000 lines?

Would System.out.println us more memory? or Would System.setErr(new PrintStream(new FileOutputStream("tonsoflines.log"))) use more memory?

What uses more process or requires more memory to function? Visual output in a console window or writing to a file with no console visual?

Edit: Also which one would finish the message first?

+2  A: 

I think in terms of memory the difference will be minimal, maybe a slight advantage to System.out.println In terms of processing and speed, writing to a log file will be a lot faster that printing everything out on the screen. Most logging frameworks for Java are optimized for maximum performance during logging operations, so you would definitely want to use a good logging framework such as Log4J for that.

nkr1pt
Thanks nkr1pt:)
Kyle
A: 

System.out is object of PrintStream. So I don't think there would be much difference.

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#out

public static final PrintStream out

The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

If you are considering this for logging, It would always be recomended to use specialized logging framework like Log4J.

YoK
+2  A: 

Console

As some test: printing to System.out the result is:

Printing line : #0
.
.
.
Printing line : #14997
Printing line : #14998
Printing line : #14999
System.out took: 238ms
  • Test #1: 238ms
  • Test #2: 235ms
  • Test #3: 251ms
  • Test #4: 210ms
  • Test #5: 172ms
  • Test #6: 268ms

new Stream

And using a new Stream the result is:

Printing line : #0
.
.
.
Printing line : #14996
Printing line : #14997
Printing line : #14998
Printing line : #14999
System.out with new stream took: 220ms
  • Test #1: 220ms
  • Test #2: 219ms
  • Test #3: 222ms
  • Test #4: 220ms
  • Test #5: 223ms
  • Test #6: 250ms

Code

    try {

        System.setOut(new PrintStream(new FileOutputStream("C:\\TMP\\logs.log")));
                    long startTime = Calendar.getInstance().getTimeInMillis();
        for (int a=0;a<15000;a++) {
            System.out.println("Printing line : #"+a);
        }

        long elapsedTime = (Calendar.getInstance().getTimeInMillis() - startTime);
        System.out.println("System.out took: " + elapsedTime + "ms");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

As you see the difference in speed is not a even a "deal", What I didn't check was the performance (Disk I/O, Memory and CPU) using those methods.

But if you ask me about what i Think, i will tell that the result will be something like the speed test that we just performed... For my logs in any application i code, i use Log4J ( http://logging.apache.org/log4j/1.2/index.html ) to log anything i want and you can set the level of loggin you want (debug, info, error, etc)

Garis Suero
Thank you so much for this:)
Kyle
You are welcome
Garis Suero