Java system.out 与新的 PrintStream
如果要输出 15,000 行的消息,什么会使用更多内存?
System.out.println 会占用更多内存吗? 或者 System.setErr(new PrintStream(new FileOutputStream("tonsoflines.log"))) 会使用更多内存吗?
什么使用更多进程或需要更多内存才能运行?控制台窗口中的可视化输出或写入没有控制台可视化的文件?
编辑:还有哪一个会先完成消息?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
控制台
作为一些测试:打印到 System.out 结果是:
238ms
235ms
251ms
210ms
172ms
268ms
new Stream
并使用新的
Stream
结果是:220ms
219ms
222ms
220ms
code>223ms
250ms
代码
如您所见,速度差异甚至不是“交易”,我没有检查的是使用这些方法的性能(磁盘 I/O、内存和 CPU)。
但如果你问我我的想法,我会告诉你结果将类似于我们刚刚执行的速度测试......对于我编码的任何应用程序中的日志,我使用 Log4J ( http://logging.apache.org/log4j/1.2/index.html )来记录我想要的任何内容,您可以设置您想要的登录级别(调试、信息、错误) , ETC)
Console
As some test: printing to System.out the result is:
238ms
235ms
251ms
210ms
172ms
268ms
new Stream
And using a new
Stream
the result is:220ms
219ms
222ms
220ms
223ms
250ms
Code
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.println 稍有优势
就处理和速度而言,写入日志文件比在屏幕上打印所有内容要快得多。
大多数 Java 日志框架都针对日志操作期间的最大性能进行了优化,因此您肯定希望使用一个好的日志框架(例如 Log4J)来实现这一点。
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.
System.out是PrintStream的对象。所以我认为不会有太大区别。
http://download.oracle。 com/javase/1.4.2/docs/api/java/lang/System.html#out
如果您正在考虑将其用于日志记录,则始终建议使用专门的日志记录框架,例如 Log4J 。
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
If you are considering this for logging, It would always be recomended to use specialized logging framework like Log4J.