golang 釋放記憶體機制的探索

ON10N發表於2019-03-04

結論

GO在執行時,即使GC了,堆記憶體減小了,向系統申請的記憶體在一段時間後才會釋放。

This process of returning memory to the system is called scavenging.

如果想要立即釋放,可以嘗試通過呼叫runtime/debug裡的FreeOSMemory() (然而個人覺得沒有這個必要

本文沒有涉及到如何調節GC和pprof的使用,不過環境變數GODEBUG也是一個很好的除錯工具,另外可以適當的在日誌中列印記憶體狀態或者使用expvar包檢視伺服器執行狀態

廢話開始

環境

  • go 1.9.2
  • os osx/ubuntu

最近業務上有個用go寫的server可能存在記憶體佔用過高的問題,於是週末趁機探索了下go的gc機制。

server實現了臨時存放生成好的音訊檔案,到期後刪除這樣一個功能,由於觀察到server記憶體佔用過高,所以懷疑是物件沒有被gc(然而只是一場誤會

由於測試程式只實現了一個簡單邏輯,所以使用pprof沒得想要的觀測結果,等go 1.10內建了火焰圖後,再專門寫一篇pprof的教程吧

htop

觀察系統佔用記憶體的時候,個人比較喜歡比top更強大一點的htop,順便附上使用教程

可以通過htop -pid xxx,或者在htop中filter程式名,指定要觀察的程式

由於要檢查記憶體,所以比較關注以下2個值

  • VIRT:程式佔用的虛擬記憶體值
  • RES:程式佔用的實體記憶體值

Go 管理記憶體的方式,是在一開始就保留一大塊 VIRT,而 RES 與實際記憶體用量接近。

環境變數GODEBUG

最後是通過設定環境變數GODEBUG=gctrace=1幫我解除疑惑的

設定後會列印出gc相關資訊,這裡翻譯下,更多內容可以去看runtime package

垃圾回收資訊

gc 1 @2.104s 0%: 0.018+1.3+0.076 ms clock, 0.054+0.35/1.0/3.0+0.23 ms cpu, 4->4->3 MB, 5 MB goal, 4 P。

1 表示第一次執行

@2.104s 表示程式執行的總時間

0% 垃圾回收時間佔用的百分比

0.018+1.3+0.076 ms clock 垃圾回收的時間,分別為STW(stop-the-world)清掃的時間, 併發標記和掃描的時間,STW標記的時間

0.054+0.35/1.0/3.0+0.23 ms cpu 垃圾回收佔用cpu時間

4->4->3 MB 堆的大小,gc後堆的大小,存活堆的大小

5 MB goal 整體堆的大小

4 P 使用的處理器數量

系統記憶體回收資訊

scvg後的數字從0開始,表示次數

scvg0: inuse: 426, idle: 0, sys: 427, released: 0, consumed: 427 (MB)

inuse 使用多少M記憶體

idle 剩下要清除的記憶體

sys 系統對映的記憶體

released 釋放的系統記憶體

consumed 申請的系統記憶體

執行記錄

# 開始執行程式,瘋狂的向記憶體寫入
gc 1 @0.008s 3%: 0.10+0.48+0.050 ms clock, 0.20+0.14/0.30/0.37+0.10 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 2 @0.059s 1%: 0.029+0.80+0.026 ms clock, 0.059+0.17/0.58/0.72+0.052 ms cpu, 6->6->6 MB, 7 MB goal, 2 P
gc 3 @0.214s 1%: 1.9+0.71+0.045 ms clock, 3.8+0.39/0/2.6+0.091 ms cpu, 12->13->11 MB, 13 MB goal, 2 P
gc 4 @0.525s 0%: 0.014+0.50+0.025 ms clock, 0.028+0/0.49/0.48+0.051 ms cpu, 21->21->17 MB, 22 MB goal, 2 P
gc 5 @0.936s 0%: 0.005+0.52+0.007 ms clock, 0.010+0.13/0.35/0.42+0.015 ms cpu, 33->33->33 MB, 34 MB goal, 2 P
gc 6 @1.811s 0%: 0.051+0.72+0.12 ms clock, 0.10+0.38/0.17/0.71+0.25 ms cpu, 66->66->58 MB, 67 MB goal, 2 P
gc 7 @3.147s 0%: 0.023+0.87+0.050 ms clock, 0.047+0.70/0.050/0.79+0.10 ms cpu, 114->117->108 MB, 117 MB goal, 2 P
gc 8 @5.771s 0%: 0.017+0.74+0.080 ms clock, 0.035+0.29/0.30/0.72+0.16 ms cpu, 206->207->178 MB, 216 MB goal, 2 P
gc 9 @9.786s 0%: 0.006+0.55+0.017 ms clock, 0.013+0.18/0.34/0.54+0.034 ms cpu, 348->348->311 MB, 357 MB goal, 2 P
gc 10 @16.736s 0%: 0.026+1.0+0.031 ms clock, 0.053+0.33/0.62/0.97+0.063 ms cpu, 607->607->524 MB, 622 MB goal, 2 P
gc 11 @29.045s 0%: 0.039+1.3+0.034 ms clock, 0.078+0.52/0.46/1.2+0.069 ms cpu, 1022->1025->913 MB, 1048 MB goal, 2 P
gc 12 @50.471s 0%: 0.056+2.0+0.023 ms clock, 0.11+0.68/1.0/1.9+0.047 ms cpu, 1782->1783->1536 MB, 1827 MB goal, 2 P
scvg0: inuse: 1642, idle: 142, sys: 1784, released: 0, consumed: 1784 (MB)
# 寫入一段時間後,GC,堆記憶體減小,但是仍未向系統釋放記憶體
GC forced
gc 13 @170.610s 0%: 0.031+2.4+0.091 ms clock, 0.062+0/2.3/0+0.18 ms cpu, 1650->1650->381 MB, 3072 MB goal, 2 P
# 再次GC
GC forced
gc 14 @290.810s 0%: 0.020+1.7+0.039 ms clock, 0.041+0/1.6/0+0.078 ms cpu, 464->464->1 MB, 762 MB goal, 2 P
gc 15 @292.210s 0%: 0.036+0.98+0.14 ms clock, 0.073+0.21/0.51/0.91+0.28 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 16 @297.784s 0%: 0.029+0.64+0.043 ms clock, 0.058+0.20/0.17/0.62+0.086 ms cpu, 7->7->2 MB, 8 MB goal, 2 P
# 仍未釋放記憶體
scvg1: inuse: 3, idle: 1781, sys: 1784, released: 0, consumed: 1784 (MB)
gc 17 @309.790s 0%: 0.034+1.4+0.045 ms clock, 0.068+1.2/0/0+0.090 ms cpu, 5->5->3 MB, 6 MB goal, 2 P
gc 18 @315.795s 0%: 0.027+0.79+0.052 ms clock, 0.055+0.34/0.29/0.78+0.10 ms cpu, 5->6->2 MB, 7 MB goal, 2 P
gc 19 @321.799s 0%: 0.027+0.81+0.076 ms clock, 0.055+0.18/0.35/0.85+0.15 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 20 @324.802s 0%: 0.023+1.2+0.062 ms clock, 0.047+0.76/0.31/0.36+0.12 ms cpu, 4->6->2 MB, 6 MB goal, 2 P
gc 21 @324.804s 0%: 0.016+0.67+0.034 ms clock, 0.033+0.46/0.048/0.66+0.068 ms cpu, 4->5->5 MB, 5 MB goal, 2 P
gc 22 @330.808s 0%: 0.028+0.85+0.036 ms clock, 0.056+0.36/0.27/0.82+0.072 ms cpu, 6->7->2 MB, 10 MB goal, 2 P
gc 23 @333.810s 0%: 0.020+0.89+0.085 ms clock, 0.041+0.65/0/0.76+0.17 ms cpu, 4->5->4 MB, 5 MB goal, 2 P
gc 24 @336.812s 0%: 0.079+0.84+0.12 ms clock, 0.15+0/0.82/0.65+0.24 ms cpu, 5->5->1 MB, 8 MB goal, 2 P
gc 25 @339.814s 0%: 0.11+1.2+0.040 ms clock, 0.22+0.14/0.97/0+0.081 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 26 @348.817s 0%: 0.048+1.4+0.062 ms clock, 0.097+1.2/0/0+0.12 ms cpu, 6->7->3 MB, 7 MB goal, 2 P
gc 27 @348.819s 0%: 0.019+0.63+0.046 ms clock, 0.039+0.26/0.31/0.58+0.092 ms cpu, 4->4->4 MB, 6 MB goal, 2 P
gc 28 @357.825s 0%: 0.027+1.2+0.11 ms clock, 0.054+0.25/0.12/1.0+0.22 ms cpu, 8->9->3 MB, 9 MB goal, 2 P
gc 29 @363.829s 0%: 0.020+0.71+0.010 ms clock, 0.040+0.53/0/0.70+0.021 ms cpu, 5->6->3 MB, 6 MB goal, 2 P
gc 30 @363.830s 0%: 0.007+0.50+0.070 ms clock, 0.014+0.16/0.21/0.48+0.14 ms cpu, 4->4->4 MB, 6 MB goal, 2 P
gc 31 @366.832s 0%: 0.019+0.89+0.026 ms clock, 0.038+0.15/0.46/0.77+0.052 ms cpu, 7->8->5 MB, 9 MB goal, 2 P
gc 32 @372.837s 0%: 0.026+0.79+0.061 ms clock, 0.052+0.16/0.52/0.71+0.12 ms cpu, 8->8->1 MB, 10 MB goal, 2 P
gc 33 @378.841s 0%: 0.029+0.87+0.062 ms clock, 0.059+0.68/0.053/0.76+0.12 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
gc 34 @378.842s 0%: 0.075+0.73+0.077 ms clock, 0.15+0.37/0.15/0.64+0.15 ms cpu, 4->5->5 MB, 6 MB goal, 2 P
gc 35 @385.210s 0%: 0.079+0.90+0.11 ms clock, 0.15+0.35/0.47/0.79+0.23 ms cpu, 6->6->1 MB, 10 MB goal, 2 P
gc 36 @387.847s 0%: 0.021+0.67+0.046 ms clock, 0.042+0.53/0/0.64+0.092 ms cpu, 4->4->4 MB, 5 MB goal, 2 P
gc 37 @390.849s 0%: 0.10+0.68+0.043 ms clock, 0.21+0.23/0.32/0.66+0.087 ms cpu, 5->6->3 MB, 9 MB goal, 2 P
gc 38 @396.853s 0%: 0.071+0.95+0.031 ms clock, 0.14+0.50/0.70/0.40+0.062 ms cpu, 4->6->2 MB, 6 MB goal, 2 P
gc 39 @396.854s 0%: 0.011+1.2+0.033 ms clock, 0.022+0.53/0.58/0+0.067 ms cpu, 4->4->4 MB, 6 MB goal, 2 P
gc 40 @399.856s 0%: 0.017+0.89+0.024 ms clock, 0.035+0.29/0.28/0.86+0.048 ms cpu, 6->7->4 MB, 9 MB goal, 2 P
gc 41 @402.857s 0%: 0.038+0.57+0.050 ms clock, 0.077+0.22/0.12/0.51+0.10 ms cpu, 6->6->3 MB, 8 MB goal, 2 P
gc 42 @408.860s 0%: 0.091+0.86+0.10 ms clock, 0.18+0.63/0.005/0.77+0.21 ms cpu, 6->7->3 MB, 7 MB goal, 2 P
gc 43 @409.210s 0%: 0.028+1.0+0.093 ms clock, 0.057+0.23/0.70/0.88+0.18 ms cpu, 4->4->4 MB, 6 MB goal, 2 P
gc 44 @417.866s 0%: 0.019+0.69+0.018 ms clock, 0.039+0.50/0/0.67+0.037 ms cpu, 8->9->2 MB, 9 MB goal, 2 P
gc 45 @417.867s 0%: 0.018+0.68+0.079 ms clock, 0.037+0.37/0.049/0.66+0.15 ms cpu, 4->5->5 MB, 5 MB goal, 2 P
gc 46 @423.871s 0%: 0.077+0.75+0.059 ms clock, 0.15+0.24/0.21/0.72+0.11 ms cpu, 7->7->2 MB, 10 MB goal, 2 P
gc 47 @429.872s 0%: 0.065+0.54+0.091 ms clock, 0.13+0.17/0.32/0.50+0.18 ms cpu, 4->4->2 MB, 5 MB goal, 2 P
gc 48 @435.874s 0%: 0.017+0.68+0.11 ms clock, 0.035+0.13/0.43/0.60+0.22 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 49 @438.876s 0%: 0.012+0.54+0.050 ms clock, 0.024+0.093/0.39/0.51+0.10 ms cpu, 5->5->1 MB, 6 MB goal, 2 P
gc 50 @444.880s 0%: 0.027+0.79+0.065 ms clock, 0.055+0.18/0.50/0.71+0.13 ms cpu, 4->4->2 MB, 5 MB goal, 2 P
gc 51 @447.882s 0%: 0.023+0.70+0.052 ms clock, 0.047+0.37/0.53/0.29+0.10 ms cpu, 4->5->4 MB, 5 MB goal, 2 P
# 仍未釋放記憶體
scvg2: inuse: 5, idle: 1778, sys: 1784, released: 0, consumed: 1784 (MB)
gc 52 @450.884s 0%: 0.019+0.73+0.13 ms clock, 0.038+0.23/0.23/0.71+0.26 ms cpu, 6->6->2 MB, 8 MB goal, 2 P
gc 53 @453.887s 0%: 0.034+0.79+0.024 ms clock, 0.069+0.61/0/0.77+0.048 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
gc 54 @453.888s 0%: 0.13+0.79+0.093 ms clock, 0.26+0.15/0.77/0.31+0.18 ms cpu, 5->5->5 MB, 7 MB goal, 2 P
gc 55 @456.889s 0%: 0.051+0.76+0.10 ms clock, 0.10+0.20/0.18/0.74+0.21 ms cpu, 8->9->5 MB, 10 MB goal, 2 P
gc 56 @462.892s 0%: 0.019+0.55+0.12 ms clock, 0.038+0.24/0.12/0.51+0.24 ms cpu, 8->8->2 MB, 10 MB goal, 2 P
gc 57 @465.893s 0%: 0.015+0.54+0.052 ms clock, 0.031+0.11/0.33/0.49+0.10 ms cpu, 4->4->4 MB, 5 MB goal, 2 P
gc 58 @468.896s 0%: 0.059+0.68+0.050 ms clock, 0.11+0.44/0.054/0.65+0.10 ms cpu, 6->7->4 MB, 8 MB goal, 2 P
gc 59 @471.897s 0%: 0.019+0.71+0.15 ms clock, 0.038+0.29/0.24/0.63+0.30 ms cpu, 7->8->4 MB, 9 MB goal, 2 P
gc 60 @474.899s 0%: 0.026+0.83+0.046 ms clock, 0.052+0.37/0.27/0.79+0.093 ms cpu, 6->8->4 MB, 9 MB goal, 2 P
gc 61 @480.903s 0%: 0.12+0.90+0.021 ms clock, 0.25+0.31/0.68/0.56+0.043 ms cpu, 6->8->4 MB, 9 MB goal, 2 P
gc 62 @489.908s 0%: 0.026+0.85+0.097 ms clock, 0.052+0.42/0.30/0.77+0.19 ms cpu, 6->6->2 MB, 8 MB goal, 2 P
gc 63 @492.910s 0%: 0.030+0.75+0.10 ms clock, 0.061+0.57/0/0.69+0.20 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
gc 64 @495.913s 0%: 0.036+0.61+0.11 ms clock, 0.073+0.27/0/0.49+0.22 ms cpu, 4->8->4 MB, 7 MB goal, 2 P
gc 65 @498.915s 0%: 0.018+0.84+0.051 ms clock, 0.037+0.41/0.085/0.68+0.10 ms cpu, 5->9->5 MB, 9 MB goal, 2 P
gc 66 @501.917s 0%: 0.079+0.65+0.10 ms clock, 0.15+0.18/0.16/0.62+0.21 ms cpu, 6->8->4 MB, 10 MB goal, 2 P
gc 67 @504.918s 0%: 0.026+0.94+0.10 ms clock, 0.052+0.29/0.14/0.93+0.20 ms cpu, 5->7->4 MB, 9 MB goal, 2 P
gc 68 @507.920s 0%: 0.12+1.7+0.040 ms clock, 0.25+1.1/0.17/0+0.080 ms cpu, 5->7->4 MB, 8 MB goal, 2 P
gc 69 @516.926s 0%: 0.027+0.89+0.039 ms clock, 0.054+0.36/0.24/0.81+0.078 ms cpu, 5->7->3 MB, 9 MB goal, 2 P
gc 70 @519.929s 0%: 0.018+0.85+0.010 ms clock, 0.037+0.48/0.036/0.81+0.020 ms cpu, 4->6->4 MB, 6 MB goal, 2 P
gc 71 @522.930s 0%: 0.015+0.66+0.069 ms clock, 0.030+0.33/0.033/0.62+0.13 ms cpu, 5->7->4 MB, 8 MB goal, 2 P
gc 72 @525.933s 0%: 0.031+0.99+0.048 ms clock, 0.063+0.44/0.37/0.93+0.097 ms cpu, 5->5->2 MB, 8 MB goal, 2 P
gc 73 @528.934s 0%: 0.024+1.1+0.079 ms clock, 0.049+0.12/0.96/0+0.15 ms cpu, 4->4->2 MB, 6 MB goal, 2 P
gc 74 @531.936s 0%: 0.095+0.82+0.026 ms clock, 0.19+0.16/0.41/0.80+0.053 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
gc 75 @537.938s 0%: 0.15+1.0+0.038 ms clock, 0.30+0.60/0.006/1.0+0.076 ms cpu, 5->7->3 MB, 7 MB goal, 2 P
gc 76 @537.940s 0%: 0.14+1.2+0.041 ms clock, 0.29+0.41/0.77/0+0.083 ms cpu, 5->5->5 MB, 7 MB goal, 2 P
gc 77 @543.944s 0%: 0.094+1.0+0.15 ms clock, 0.094+0.54/0.010/0.90+0.15 ms cpu, 8->10->4 MB, 10 MB goal, 2 P
gc 78 @549.948s 0%: 0.026+0.87+0.029 ms clock, 0.053+0.55/0.010/0.82+0.058 ms cpu, 6->8->3 MB, 8 MB goal, 2 P
gc 79 @555.952s 0%: 0.13+0.89+0.055 ms clock, 0.26+0.50/0.028/0.79+0.11 ms cpu, 4->6->3 MB, 6 MB goal, 2 P
gc 80 @555.954s 0%: 0.017+0.67+0.13 ms clock, 0.035+0.14/0.34/0.65+0.26 ms cpu, 5->5->5 MB, 7 MB goal, 2 P
gc 81 @558.955s 0%: 0.013+0.78+0.044 ms clock, 0.026+0.27/0.29/0.74+0.089 ms cpu, 6->7->3 MB, 10 MB goal, 2 P
gc 82 @561.957s 0%: 0.13+1.5+0.012 ms clock, 0.26+1.1/0.17/0+0.025 ms cpu, 5->6->4 MB, 6 MB goal, 2 P
gc 83 @564.961s 0%: 0.029+0.80+0.12 ms clock, 0.058+0.18/0.56/0.65+0.25 ms cpu, 5->5->2 MB, 8 MB goal, 2 P
gc 84 @567.963s 0%: 0.030+0.78+0.067 ms clock, 0.061+0.16/0.35/0.89+0.13 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 85 @570.965s 0%: 0.028+0.78+0.015 ms clock, 0.057+0.48/0.53/0.16+0.030 ms cpu, 5->6->3 MB, 6 MB goal, 2 P
gc 86 @574.210s 0%: 0.11+0.84+0.059 ms clock, 0.23+0.14/0.60/0.77+0.11 ms cpu, 5->5->2 MB, 6 MB goal, 2 P
gc 87 @576.968s 0%: 0.057+0.68+0.089 ms clock, 0.11+0.49/0.018/0.62+0.17 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
gc 88 @576.969s 0%: 0.039+0.99+0.043 ms clock, 0.079+0.13/0.80/0+0.086 ms cpu, 5->5->5 MB, 7 MB goal, 2 P
gc 89 @591.974s 0%: 0.025+1.0+0.043 ms clock, 0.050+0.65/0/0.76+0.087 ms cpu, 8->10->3 MB, 10 MB goal, 2 P
gc 90 @594.976s 0%: 0.066+1.8+0.033 ms clock, 0.13+1.0/0/0+0.067 ms cpu, 4->6->3 MB, 6 MB goal, 2 P
gc 91 @594.979s 0%: 0.014+0.58+0.070 ms clock, 0.029+0.17/0.19/0.56+0.14 ms cpu, 4->4->4 MB, 6 MB goal, 2 P
# 釋放了!!!!!!!!!!!!!!!!!!
scvg3: 1767 MB released
scvg3: inuse: 5, idle: 1778, sys: 1784, released: 1767, consumed: 17 (MB)
gc 92 @603.984s 0%: 0.022+0.81+0.12 ms clock, 0.044+0.42/0.086/0.66+0.24 ms cpu, 6->9->4 MB, 9 MB goal, 2 P
gc 93 @606.986s 0%: 0.11+1.0+0.024 ms clock, 0.22+0.53/0.038/0.96+0.048 ms cpu, 5->8->5 MB, 9 MB goal, 2 P
gc 94 @609.988s 0%: 0.029+0.88+0.070 ms clock, 0.058+0.26/0.43/0.73+0.14 ms cpu, 6->6->2 MB, 10 MB goal, 2 P
gc 95 @612.989s 0%: 0.019+0.59+0.026 ms clock, 0.038+0.28/0.12/0.55+0.053 ms cpu, 4->5->3 MB, 6 MB goal, 2 P
gc 96 @615.991s 0%: 0.081+0.91+0.034 ms clock, 0.16+0.50/0.022/0.88+0.069 ms cpu, 4->7->4 MB, 7 MB goal, 2 P
gc 97 @618.994s 0%: 0.027+0.76+0.048 ms clock, 0.054+0/0.18/1.3+0.097 ms cpu, 5->5->2 MB, 9 MB goal, 2 P
gc 98 @627.999s 0%: 0.023+0.81+0.075 ms clock, 0.046+0.40/0.23/0.73+0.15 ms cpu, 4->4->2 MB, 5 MB goal, 2 P
gc 99 @634.003s 0%: 0.032+1.3+0.057 ms clock, 0.064+0.60/0.43/0.96+0.11 ms cpu, 4->4->3 MB, 5 MB goal, 2 P
gc 100 @640.006s 0%: 0.018+0.63+0.12 ms clock, 0.036+0.10/0.45/0.60+0.24 ms cpu, 5->5->1 MB, 7 MB goal, 2 P
gc 101 @649.007s 0%: 1.0+0.76+0.047 ms clock, 2.1+0.22/1.8/0+0.094 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
gc 102 @652.010s 0%: 0.017+0.64+0.068 ms clock, 0.035+0.11/0.46/0.63+0.13 ms cpu, 4->4->2 MB, 6 MB goal, 2 P
gc 103 @661.014s 0%: 0.10+0.78+0.071 ms clock, 0.21+0.61/0/0.76+0.14 ms cpu, 4->5->2 MB, 5 MB goal, 2 P
gc 104 @664.016s 0%: 0.027+0.94+0.10 ms clock, 0.055+0.62/0.001/0.81+0.20 ms cpu, 4->6->3 MB, 6 MB goal, 2 P
gc 105 @667.019s 0%: 0.024+0.76+0.060 ms clock, 0.048+0.15/0.52/0.69+0.12 ms cpu, 4->4->2 MB, 7 MB goal, 2 P
gc 106 @673.022s 0%: 0.023+0.91+0.064 ms clock, 0.047+0.50/0.056/0.83+0.12 ms cpu, 4->5->3 MB, 6 MB goal, 2 P
gc 107 @676.025s 0%: 0.020+1.5+0.038 ms clock, 0.040+1.0/0.17/0+0.077 ms cpu, 4->5->3 MB, 6 MB goal, 2 P
gc 108 @679.028s 0%: 0.031+1.6+0.047 ms clock, 0.063+0.98/0.26/0+0.095 ms cpu, 5->7->4 MB, 7 MB goal, 2 P
gc 109 @682.031s 0%: 0.018+0.93+0.032 ms clock, 0.036+0.40/0.19/0.78+0.064 ms cpu, 5->6->3 MB, 8 MB goal, 2 P
gc 110 @685.033s 0%: 0.086+0.84+0.12 ms clock, 0.17+0.46/0.11/0.79+0.25 ms cpu, 4->6->3 MB, 7 MB goal, 2 P
gc 111 @688.036s 0%: 0.051+1.7+0.040 ms clock, 0.10+1.1/0.18/0+0.080 ms cpu, 4->6->4 MB, 7 MB goal, 2 P
gc 112 @691.038s 0%: 0.10+1.1+0.044 ms clock, 0.21+0.45/0.20/1.1+0.089 ms cpu, 5->7->4 MB, 8 MB goal, 2 P
gc 113 @694.040s 0%: 0.013+0.60+0.12 ms clock, 0.026+0.086/0.47/0.54+0.25 ms cpu, 5->5->2 MB, 9 MB goal, 2 P
gc 114 @700.043s 0%: 0.027+0.92+0.061 ms clock, 0.054+0.26/0.50/0.62+0.12 ms cpu, 4->5->3 MB, 5 MB goal, 2 P
複製程式碼

程式裡讀取記憶體狀態

runtime package

import "runtime"

var m runtime.MemStats
func(){
    runtime.ReadMemStats(&m)
    # m.Alloc 堆記憶體
}()
複製程式碼

使用標準庫expvar

可以參考這篇實戰,可以通過開http介面讀取程式狀態

調研中途看到的一些優化建議和方法

相關文章