Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically intercept the GC to print information to my log

I am aware that you can use the -verbosegc to print information about the GC to the sysout but I don't want that. I want to intercept whenever GC triggers and print information about it to my custom logger, perhaps save last GC timestamp on a internal variable, etc.

Any hope here?

like image 830
chrisapotek Avatar asked Oct 26 '25 19:10

chrisapotek


1 Answers

You can't hook the GC logging mechanism from within a Java program because when the GC thread(s) run the JVM is effectively stopped. Doing so would cause a deadlock so its a good thing we can't. Even using a custom launcher to try and hook via some handle reopen technique and calling up to Java would deadlock. Maybe if a reopen from within a custom launcher was possible (I don't know one way or the other) buffering the data and jumping from Java to native via JNI to retrieve the buffer might work.

like image 78
Java42 Avatar answered Oct 29 '25 09:10

Java42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!