Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Java GC logs show MB or GB instead of KB

By default Java GC log output shows the memory details in KB (kilo bytes). I know it might sound silly, but given most of the Jvms I deal with have heap sizes close to 20 to 40GB, I find it quite inconvenient to read the numbers in KB quickly, especially when quickly scanning logs in putty etc.

Is it possible to make Java print these number in fractions of MB or GB instead?

I didn't find any option in JDK documentation.

If it is not possible, are there any ideas around how one can go about adding this feature to GC logging? (not from outside but from with in JVM)

Thank you in advance for your help.

like image 811
endless Avatar asked Sep 21 '16 03:09

endless


2 Answers

There is no runtime option to change this, it depends on what particular garbage collector is using to print the logging data.

Use G1 in JDK 8, and it will print in proper units, depending on heap size:

$ java -XX:+UseG1GC -verbose:gc
[GC pause (Metadata GC Threshold) (young) (initial-mark) 592M->23M(100G), 0.0137879 secs]

Upgrade to JDK 9, and then Unified Logging would print in proper units, depending on the heap size, for all collectors (plus, timestamps are in proper units too):

$ java -XX:+UseParallelGC -Xlog:gc
[0.766s][info][gc] GC(0) Pause Young (Metadata GC Threshold) 4300M->15M(117760M) 6.765ms
like image 56
Aleksey Shipilev Avatar answered Oct 06 '22 06:10

Aleksey Shipilev


Maybe you could just create simple bash program to reformat your log?

In my example I'm assuming you're using linux and that you have groovy on path.

In /usr/bin create file kb2mb with content like this:

#!/usr/bin/env groovy

System.in.readLines().each{
  println(it.replaceAll(/\d+K/) {
    (((it[0..-2] as Integer) / 1024) as Double).round(2) + "M"
  })
}

Then give program permission to execute: chmod +x /usr/bin/kb2mb.

Finally you could be able to run it like this:

cat gc.log|kb2mb

Example output:

2015-05-26T14:45:37.987-0200: 151.126: [GC (Allocation Failure) 151.126: [DefNew: 614.37M->68.25M(614.38M), 0.0584157 secs] 1581.39M-> 1243.41M(1979.75M), 0.0585007 secs] [Times: user=0.06 sys=0.00, real=0.06 secs]

Instead of groovy, you could of course use bash, python etc.

like image 22
Krzysztof Atłasik Avatar answered Oct 06 '22 06:10

Krzysztof Atłasik