How to redirect JVM output without tear up output from the application? How to redirect JVM output without tear up output from the application? python python

How to redirect JVM output without tear up output from the application?


I would suggest taking a slight detour and looking at using Java Instrumentation APIs - use (write) a simple Java Agent to do this. From your benchmarking perspective, this will give you far more power as well. You could use your Java Agent to log everything (and hence there would be no contention between different logger threads).

You can read more at http://www.javabeat.net/2012/06/introduction-to-java-agents/ or http://today.java.net/pub/a/today/2008/04/24/add-logging-at-class-load-time-with-instrumentation.html


Try using System.out.println() instead of System.out.print().System.out.println() forces a stream flush inside the syncronized section and at least your output won't be as mixed.


Use Log4J or message-driven logging framework versus System.out.println().

Log4J uses a message event-model that guarantees ordering of messages.Further, various 'appenders' can be used to log to a database or other output/file, allowing for separation by Java package and other attributes so the data are not mixed.

Also, along these lines, consider using a high-performance timer and/or do not attempt to measure very short (millisecond) events. The reason is that a call to System.currentTimeMillis() will just in turn call the operating system clock. On every OS there is some 'clock drift' and caching that goes on such that the underlying system function may return the same value, resulting in +/- 30 ms offsets in the actual time. To remedy this or increase accuracy, group the functions being measured into a large enough sample size and then divide by the number of iterations.

For example, perform 10K operations that average 1-2 milliseconds as one measured operation. Then divide by 10K to get time-per-operation.

Otherwise, again, a high-performance timer would be necessary.