在cmd命令行下:
1.设置跟踪GC的临时环境变量
set GOGCTRACE=1
set GODEBUG=gctrace=1
2.将gc写入日志中
xxx.exe 2> gc.log
在生成的gc跟踪日志中查看gc的过程,下面是截取的一部分gc日志:
gc 1 @0.005s 0%: 0+11+0.99 ms clock, 0+0/15/0+7.9 ms cpu, 25->25->24 MB, 26 MB goal, 8 P
gc 2 @0.033s 0%: 0+2.5+0 ms clock, 0+0/5.0/9.0+0 ms cpu, 25->26->25 MB, 49 MB goal, 8 P
gc 3 @0.213s 0%: 0+3.0+0 ms clock, 0+0/6.0/14+0 ms cpu, 37->38->33 MB, 51 MB goal, 8 P
gc 4 @1.197s 0%: 0+4.5+0 ms clock, 0+0.50/9.0/18+0 ms cpu, 57->57->47 MB, 66 MB goal, 8 P
gc 5 @2.162s 0%: 0+5.0+0 ms clock, 0+1.0/8.0/18+0 ms cpu, 88->88->54 MB, 94 MB goal, 8 P
gc 6 @3.048s 0%: 0+4.5+0 ms clock, 0+0.50/8.5/20+0 ms cpu, 104->105->68 MB, 108 MB goal, 8 P
gc 7 @4.424s 0%: 0+6.5+0 ms clock, 0+0.50/9.5/24+0 ms cpu, 133->133->89 MB, 137 MB goal, 8 P
gc 8 @6.326s 0%: 0+4.5+0 ms clock, 0+0/8.5/19+0 ms cpu, 175->175->93 MB, 179 MB goal, 8 P
gc 9 @8.695s 0%: 0+5.5+0 ms clock, 0+0.50/10/22+0 ms cpu, 181->181->100 MB, 186 MB goal, 8 P
scvg4: inuse: 143, idle: 70, sys: 213, released: 0, consumed: 213 (MB)
说明:gc 1 @0.005s 0%: 0+11+0.99 ms clock, 0+0/15/0+7.9 ms cpu, 25->25->24 MB, 26 MB goal, 8 P
gc 1
:说明gc过程,不做解释,1 表示第几次执行gc操作
@0.005s
:表示程序执行的总时间
0%:
:表示gc时时间和程序运行总时间的百分比
0+11+0.99 ms clock,
:wall-clock (还不清楚什么意思)
0+0/15/0+7.9 ms cpu,
:CPU times for the phases of the GC (还不清楚什么意思)
25->25->24 MB,
:gc开始时的堆大小;GC结束时的堆大小;存活的堆大小
26 MB goal,
:整体堆的大小
8 P
:使用的处理器的数量
说明 scvg4: inuse: 143, idle: 70, sys: 213, released: 0, consumed: 213 (MB)
inuse: 143,
:使用多少M内存
idle: 70,
:0 剩下要清除的内存
sys: 213,
: 系统映射的内存
released: 0,
: 释放的系统内存
consumed: 213
: 申请的系统内存
官网部分释义
gctrace: setting gctrace=1 causes the garbage collector to emit a single line to standard
error at each collection, summarizing the amount of memory collected and the
length of the pause. Setting gctrace=2 emits the same summary but also
repeats each collection. The format of this line is subject to change.
Currently, it is:
gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # P
where the fields are as follows:
gc # the GC number, incremented at each GC
@#s time in seconds since program start
#% percentage of time spent in GC since program start
#+...+# wall-clock/CPU times for the phases of the GC
#->#-># MB heap size at GC start, at GC end, and live heap
# MB goal goal heap size
# P number of processors used
The phases are stop-the-world (STW) sweep termination, concurrent
mark and scan, and STW mark termination. The CPU times
for mark/scan are broken down in to assist time (GC performed in
line with allocation), background GC time, and idle GC time.
If the line ends with "(forced)", this GC was forced by a
runtime.GC() call and all phases are STW.
Setting gctrace to any value > 0 also causes the garbage collector
to emit a summary when memory is released back to the system.
This process of returning memory to the system is called scavenging.
The format of this summary is subject to change.
Currently it is:
scvg#: # MB released printed only if non-zero
scvg#: inuse: # idle: # sys: # released: # consumed: # (MB)
where the fields are as follows:
scvg# the scavenge cycle number, incremented at each scavenge
inuse: # MB used or partially used spans
idle: # MB spans pending scavenging
sys: # MB mapped from the system
released: # MB released to the system
consumed: # MB allocated from the system
官方runtime
GODEBUG=gctrace=1 ./xxx
或者
GODEBUG=gctrace=1 ./xxx 2> gc.log