Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging GC information in the application

Is there a way to forward the garbage collection information (for example the output of -XX:+PrintGCDetails or -verbose:gc) to a logger within the Java application (sl4j + logback in my case)?

like image 847
assylias Avatar asked Apr 11 '12 11:04

assylias


People also ask

What is GC logging?

Java Garbage Collector (GC) logs are used for memory management and object allocation and promotion. GC logs consist of critical information such as the duration of the GC process, the number of objects promoted, and more. It includes details of the entire GC process and the resources it uses.

Does GC logging affect performance?

Yes it does. When you find yourself in a point where you have to tune the JVM and/or GC, that probably means that your system is having problems handling the load.


2 Answers

Those messages are generated by the JVM native process and not from Java code, so you just can

  1. Redirect output to file using -Xloggc (no rotation)
    • How to redirect verbose garbage collection output to a file?
  2. Pipe stdout directly (several rotation options)
    • Log rotation of stdout?
    • Rolling garbage collector logs in java
    • Garbage collector log (loggc) file rotation with logrotate does not work properly
like image 81
fglez Avatar answered Oct 23 '22 07:10

fglez


An interesting approach would be to redirect gc.log to a named pipe -Xloggc:/my/named/pipe How to write GC log to named pipe

then read that pipe form the application itself: How to open a Windows named pipe from Java?

and log to an arbitrary (e.g. async rolling) logback logger from the code.

Tried that on a Windows machine. Unfortunately, it is trickier to setup on Windows than on Linux.

On Windows it works basically with help of an additional Powershell script (can be a dedicated application as well). This sample project also contains a demo application that can be used right away to test the GC logs redirection to Logback via SLF4J.

like image 32
Sergey Shcherbakov Avatar answered Oct 23 '22 08:10

Sergey Shcherbakov