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?
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.
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
238ms
235ms
251ms
210ms
172ms
268ms
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
220ms
219ms
222ms
220ms
223ms
250ms
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)
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.