Do Not Use System.Out.Println in Server Side Code

Do not use System.out.println in server side code

System.out.println is an IO-operation and therefor is time consuming.
The Problem with using it in your code is, that your program will wait until the println has finished. This may not be a problem with small sites but as soon as you get load or many iterations, you'll feel the pain.

The better approach is to use a logging framework.
They use a message queue and write only if no other output is going on.

And another benefit is that you can configure separate log files for different purposes.
Something your Ops team will love you for.

Read more here:

  • http://logging.apache.org/log4j/1.2/manual.html
  • Logger vs. System.out.println

Does System.out.println() effect the code efficiency ?

System.out implementation contains a synchronized block on the output stream.

From PrintStream.java :

      /**
* Prints a String and then terminate the line. This method behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>String</code> to be printed.
*/

public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}

Putting System.out.println a lot will make the entire project to run almost single-threaded, because all thread will waiting for synchronization lock, and make your application begin to crawl.

This means your superior is right.

Alternatively, Use logging framework like log4j instead. You can still configure your log4j to still output to System.out.println by just minor changes in the appender configuration.

What is the difference between Java Logger and System.out.println

Usually, because a Logger can be configured to write to a file (and the console). It might also be configured at higher (or lower) granularity as to messaging. For example, you might configure (at runtime) for level of warn. In which case, that logger would not display debug or info messages. It can include information such as the class that is writing, a line number, and a date and time (of the message).

Shall I use System.out or Log4j for normal information printout?

First, System.out is not the target for logging output, except for very tiny tools or some one shot stuff; that's why logging frameworks have been invented. And there it does not really matter at first place, whether you go for Log4J, JDK Logging or something completely different. Although Log4J is established as the de facto standard for logging in the Java world.

As those "code smell detectors" always assume (until informed otherwise) that you write some kind of server software, they warn when you use output to System.out because for such a server, it usually makes no difference whether you print to System.out or /dev/nul

But if you write a command line tool of some kind (perhaps a batch client to a server, just to have another sample than grep …), it is perfectly okay to write the current status or other output to System.out (even when the "smell sniffer" is still moaning). "Status" means here mainly the positive feedback; any error messages should still be logged … to a logging subsystem, although it would be still a good idea to print them to the screen, too.

And usually you can annotate your source code to pacify the "smell detector" …

Logger vs. System.out.println

See this short introduction to log4j.

The issue is in using System.out to print debugging or diagnostic information. It is a bad practice because you cannot easily change log levels, turn it off, customize it, etc.

However if you are legitimately using System.out to print information to the user, then you can ignore this warning.

Where do System.out.println() messages go, when it is called in client-side GWT-module?

System.out.println() are just removed by the compiler in production mode.

If you want to check just create this simple module:

public class Foo implements EntryPoint {

public void onModuleLoad() {

System.out.println("Hello World!");
}
}

And look at the generated javascript.

Java - System.out effect on performance

It can have an impact on your application performance. The magnitude will vary depending on the kind of hardware you are running on and the load on the host.

Some points on which this can translate to performance wise:

-> Like Rocket boy stated, println is synchronized, which means you will be incurring in locking overhead on the object header and may cause thread bottlenecks depending on your design.

-> Printing on the console requires kernel time, kernel time means the cpu will not be running on user mode which basically means your cpu will be busy executing on kernel code instead of your application code.

-> If you are already logging this, that means extra kernel time for I/O, and if your platform does not support asynchronous I/O this means your cpu might become stalled on busy waits.

You can actually try and benchmark this and verify this yourself.

There are ways to getaway with this like for example having a really fast I/O, a huge machine for dedicated use maybe and biased locking on your JVM options if your application design will not be multithreaded on that console printing.

Like everything on performance, it all depends on your hardware and priorities.



Related Topics



Leave a reply



Submit