`
kerlubasola
  • 浏览: 679626 次
文章分类
社区版块
存档分类
最新评论

System.out.println performance

 
阅读更多

Any system that logs vast amounts of information, needs to think about performance. The activity of logging cannot be a synchronous blocking call that returns only when the message has been logged to a persistence store. Enterprise logging systems usually make use of a message bus to carry messages asynchronously to their target persistence store. Be it a database or a file.

Talking about logging brings us to System.out.println() (Lets call is SOP for short). It is a surprisingly commonly method to “log” messages. SOP is not meant to be used as a logging system, but unfortunately there is no dearth of projects that have these statements scattered around the code base. The adverse effects that this statement can bring on the performance of the system is often not recognized as well as it should be.

Why is SOP a bottleneck for performance ? This is why…

Code excerpt from PrintStream.java:



All SOP calls on a String result in a corresponding call to the private write(String s) method in PrintStream.java. The synchronized block ensures that every thread has to wait for this call to end before it can proceed. Whats more, the calls to flushBuffer() on the BufferedWriter that is textOut result in the execution of more synchronized blocks. These blocks are expensive to execute.

Here is a chart that shows how performance degrades when a program logs 100,000 messages through various threads. The rate of degradation also depends on the number of characters that are passing through the stream.

System.out.println performance:
System.out.println performance

The degradation experienced in ‘Web application X / Y’ may vary, but it cannot be discounted. Avoid using SOPs to log messages in your app. Even one or two that are left over can harm performance under the right conditions. Let us also not forget about e.printStackTrace() and other forms of writing to console output that follow the same synchronized pattern.

分享到:
评论

相关推荐

    日志管理,避免程序中到处是System.out.println();

    统一管理日志打印与通过System.out.println();打印

    倒排System.out.println("请您输入你要倒排的数据和这个数据的位数 : ");

    package day2;... System.out.println("请您输入你要倒排的数据和这个数据的位数 : "); Scanner sca=new Scanner(System.in); long nu=sca.nextInt(); long mm=sca.nextInt(); niXuShuChu(nu,mm);

    java 中 System.out.println()和System.out.write()的区别

    主要介绍了 java 中 System.out.println()和System.out.write()的区别.的相关资料,需要的朋友可以参考下

    细聊java中的System.out.println()

    当我们看到# System.out.println()这行代码的时候,我相信很多人都会回答,这有什么好聊的?不就是一条打印语句吗? 那我问你一句,你如何理解System.out.println() 我相信大多数朋友都回答不出来他到底是个什么...

    idea输入sout无法自动补全System.out.println()的问题

    主要介绍了idea输入sout无法自动补全System.out.println()的问题,本文给大家分享解决方案,供大家参考,需要的朋友可以参考下

    java中for(int i= 0; i<=10;i=i++)System.out.print(i);为什么无限输出0

    Java循环的一个小问题,java中for(int i= 0; i;i=i++)System.out.print(i);为什么无限输出0

    Guid.rar_out_游览图

    System.out.println("where 显示当前在什么地方") System.out.println("list 列出当前位置的物品") System.out.println("list allspots 列出大厦内所有地点") System.out.println("list spots 列出当前位置...

    java基本类型

    System.out.println(3/2); System.out.println(3d/2); System.out.println("------------------------"); System.out.println("s"+"b"); System.out.println('s'+'b');//int+int System.out.println('s'+...

    java通讯录

    System.out.println(" 中国移动 "); System.out.println(" 1.查看 "); System.out.println(" 2.添加 "); System.out.println(" 3.修改 "); System.out.println(" 4.删除 "); System.out.println(" 5....

    小区消防管理知识宣导,用java编写消防知识普及小游戏代码.docx

    下面是一个用Java编写的消防知识... System.out.println("1. 火灾发生时,应该先采取哪些措施?"); System.out.println("A. 拨打119报警电话"); System.out.println("B. 救人"); System.out.println("C. 扑灭火源");

    java单片机通信

    case SerialPortEvent.BI:System.out.println("BI");break; case SerialPortEvent.CD:System.out.println("CD");break; case SerialPortEvent.CTS:System.out.println("CTS");break; case SerialPortEvent.DSR:...

    System.out.println("您输入的身份证号不是18位或不是全数字!")

    System.out.println("您输入的身份证号不是18位或不是全数字!")

    各种Hash函数(JAVA版)

    System.out.println(" 1. RS-Hash Function Value: " + ghl.RSHash(key)); System.out.println(" 2. JS-Hash Function Value: " + ghl.JSHash(key)); System.out.println(" 3. PJW-Hash Function Value: " + ghl....

    javaInteger大数据运算.pdf

    //加法 System.out.println(b1.add(b2)); //减法 System.out.println(b2.subtract(b1)); //乘法 System.out.println(b1.multiply(b2)); //除法 System.out.println(b2.divide(b1)); //BigDecimal ⾼精度⼩数运算 ...

    Java基本语句(注释语句、javadoc、System.out.print和println和printf区别)复习2

    注释语句、javadoc、System.out.print和println和printf区别java特殊语句注释语句print、printf、println区别 java特殊语句 转换符 类型 d 十进制 x 十六进制 o 八进制 f 定点浮点数 e 指数浮点数 ...

    测试文件(java版)

    System.out.println("运行该程序时,请输入以下格式的命令:"); System.out.println("java FileTest &lt;文件名&gt;"); return; } File file = new File(args[0]); System.out.println("文件或目录是否存在:" + ...

    Java实现仓库管理

    System.out.println(""); System.out.println("****************以下输出参考中所有货物的信息***************"); System.out.println(""); System.out.println("goodsTotalNum="+goodsTotalNum); for(int i=0;...

    循环老师讲的代码

    System.out.println("执行"+i); if(i==5){ break;//终止整个for循环. } } //continue的使用 for(int i=1;i;i++){ if(i==5||i==7){ continue;//终止的是循环体中某一次循环。 } ...

    java面试题122道基础题

    System.out.println(s); String s1="abc"; String s2=new String("abc"); System.out.println(s==s1);//false System.out.println(s==s2);//false System.out.println(s1==s2);//false System.out.println...

    switch demo

    System.out.println("请选择功能: 1.显示全部记录 2.查询登录记录 0.退出"); int command = scan.nextInt(); switch(command){ case 1: System.out.println("显示全部记录"); break; case 2: ...

Global site tag (gtag.js) - Google Analytics