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)?
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.
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.
Those messages are generated by the JVM native process and not from Java code, so you just can
-Xloggc
(no rotation)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With