douyu1656
douyu1656
2019-05-08 15:54

对于GC开始时,GC结束时的堆大小以及gctrace = 1中的活动堆号代表什么?

已采纳

Setting GODEBUG=gctrace=1 causes the Go garbage collector to emit a single line to standard error about internal info about each GC round. Let's say I have this output:

gc 1 @0.021s 0%: 0.15+0.37+0.25 ms clock, 3.0+0.19/0.39/0.60+5.0 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 2 @0.024s 0%: 0.097+0.94+0.16 ms clock, 0.29+0.21/1.3/0+0.49 ms cpu, 4->4->1 MB, 5 MB goal, 48 P
gc 3 @0.027s 1%: 0.10+0.43+0.17 ms clock, 0.60+0.48/1.5/0+1.0 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 4 @0.028s 1%: 0.18+0.41+0.28 ms clock, 0.18+0.69/2.0/0+0.28 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 5 @0.031s 1%: 0.078+0.35+0.29 ms clock, 1.1+0.26/2.0/0+4.4 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 6 @0.032s 1%: 0.11+0.50+0.32 ms clock, 0.22+0.99/2.3/0+0.64 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 7 @0.034s 1%: 0.18+0.39+0.27 ms clock, 0.18+0.56/2.2/0+0.27 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 8 @0.035s 2%: 0.12+0.40+0.27 ms clock, 0.12+0.63/2.2/0+0.27 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 9 @0.036s 2%: 0.13+0.41+0.26 ms clock, 0.13+0.52/2.2/0+0.26 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 10 @0.038s 2%: 0.099+0.51+0.20 ms clock, 0.19+0.56/1.9/0+0.40 ms cpu, 4->5->0 MB, 5 MB goal, 48 P
gc 11 @0.039s 2%: 0.10+0.46+0.20 ms clock, 0.10+0.23/1.3/0.005+0.20 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 12 @0.040s 2%: 0.066+0.46+0.24 ms clock, 0.93+0.40/1.7/0+3.4 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 13 @0.041s 2%: 0.099+0.30+0.20 ms clock, 0.099+0.60/1.7/0+0.20 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 14 @0.042s 2%: 0.095+0.45+0.24 ms clock, 0.38+0.58/2.0/0+0.98 ms cpu, 4->5->0 MB, 5 MB goal, 48 P
gc 15 @0.044s 2%: 0.095+0.45+0.21 ms clock, 1.0+0.78/1.9/0+2.3 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
gc 16 @0.045s 3%: 0.10+0.45+0.23 ms clock, 0.10+0.70/2.1/0+0.23 ms cpu, 4->5->0 MB, 5 MB goal, 48 P
gc 17 @0.046s 3%: 0.088+0.40+0.17 ms clock, 0.088+0.45/1.9/0+0.17 ms cpu, 4->4->0 MB, 5 MB goal, 48 P
.
.
.
.
gc 6789 @9.998s 12%: 0.17+0.91+0.24 ms clock, 0.85+1.8/5.0/0+1.2 ms cpu, 4->6->1 MB, 6 MB goal, 48 P
gc 6790 @10.000s 12%: 0.086+0.55+0.24 ms clock, 0.78+0.30/4.2/0.043+2.2 ms cpu, 4->5->1 MB, 6 MB goal, 48 P

There is a definition of these values in documention:

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

What I am really confused with is #->#-># MB heap size at GC start, at GC end, and live heap.

  • Is that correct, that at each round GC releases some amount of unused(garbage) memory to OS and this must decrease heap size ? If yes, then why some values of heap are increasing ? For example: 4->5->0. We had 4MB memory, including garbage, before the GC start. Then, how it is possible to get 5MB of memory after we cleaned up the garbage from it ?

  • The third value is live heap size. What is the difference between the regular heap size ? I suppose that it is heap w/o garbage.

  • How the goal heap size is calculated ? Is that correct it is a heap size that GC wants to achieve after the cleanup ? Then why this value is greater than heap size before the GC start ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douliang6563 douliang6563 2年前

    The GC cleans some amount of garbage each pass. It does not necessarily release it to the OS (if it thinks it would just have to request it again shortly); and if it does, the OS does not necessarily reclaim it (until there is memory pressure from another process, the OS may leave that memory allocated to your process in case it needs it again).

    Live heap size is how much of the heap is actively in use, less any dead objects and free heap space ready for future allocations. Goal heap size is how much memory GC thinks it needs to get from the OS to handle your process's allocations on an ongoing basis without having to constantly request more memory from the OS (i.e. how much stays alive + how much is allocated & discarded between GC runs).

    The goal of GC is to clean up dead objects in the heap, and to maintain enough free heap space to handle most allocations without having to request more memory from the OS (which is slow), while also not keeping excessive free memory (so that the OS can still allocate to other processes).

    点赞 评论 复制链接分享