背景

服务在开启了pprof的情况下,可以使用如下方式,简单粗暴的的获取gc情况

实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash

_lastNumGC=0
_first=true

while : ; do
r=$(curl -s 127.0.0.1:6071/debug/pprof/heap?debug=1 | tail -22)

_NumGC=$(echo $r | sed 's/#/\n#/g' | grep "^# NumGC" | awk '{print $NF}')
NumGC=$[ ${_NumGC} - ${_lastNumGC} ]
if ${_first}; then NumGC=0; _first=false; fi
_lastNumGC=${_NumGC}

PauseNs=$(echo $r | sed 's/#/\n#/g' | grep '^# PauseNs' | grep -Eo '\[.*\]' | sed -e 's/\[//' -e 's/\]//' |  sed 's/ /\n/g' | awk '{ sum+=$0; if ($0>0){count+=1} } END {printf "%.f\n",sum/count}')

echo "NumGC: " ${NumGC}
echo "PauseNs_Avg: " ${PauseNs}

sleep 10
done

-EOF-