结论

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接口读取程序状态

调研中途看到的一些优化建议和方法

  • 对象池和日志优化
  • string的拼接性能
  • CompilerOptimizations

转载于:https://juejin.im/post/5a70ec9af265da3e4d73014d

golang 释放内存机制的探索相关推荐

  1. c++ 无法读取内存_Linux内存机制以及手动释放swap和buffer和cache

    点击上方 "Linux中文社区" 关注,星标或者置顶11点30分准时推送,第一时间送达 作者:Darren_Wen | 责编:中文妹 来源:blog.51cto.com/wenda ...

  2. android 清理手机开启的所有程序,安卓内存机制详解 别清理释放RAM损害你的手机...

    许多人喜欢在手机里面安装一个一键清理或者是XX优化大师.不知道大家有没有觉得,在一键清理后,手机打开应用的速度反而没有清理前快.这是为什么呢?今天我就给大家讲一下安卓的内存机制. 当一个程序放到后台时 ...

  3. android 8不能清理内存,安卓手机内存机制揭秘 清理释放RAM对提速无效

    相信很多安卓手机用户都经常使用一键清理或优化大师来对手机RAM进行清理,但是你有没有发现这样手机打开应用的速度反而更慢了.这里就要提到安卓系统的内存运行机制了,下面就来为大家分析一下. 当一个程序放到 ...

  4. 图示Golang垃圾回收机制

    垃圾回收概念 程序创建对象等引用类型实体时会在虚拟内存中分配给它们一块内存空间,如果该内存空间不再被任何引用变量引用时就成为需要被回收的垃圾.操作系统会记录一个进程运行时的所占用的内存.CPU和寄存器 ...

  5. linux手动释放内存的方法

    Linux手动释放缓存的方法 Linux释放内存的命令: sync echo 1 > /proc/sys/vm/drop_caches drop_caches的值可以是0-3之间的数字,代表不同 ...

  6. 如何为linux释放内存和缓存

    如何为linux释放内存和缓存_华陌飞尘_新浪博客 如何为linux释放内存和缓存     (2011-10-20 10:49:01)        标签:     linux     swap    ...

  7. C++ 之new和delete释放内存

    C++ -释放内存(new和delete) C++动态分配和释放内存 @c.biancheng.net/view/206.html 在C语言中,动态分配内存用 malloc() 函数,释放内存用 fr ...

  8. c/c++内存机制(一)(原)

    一:C语言中的内存机制 在C语言中,内存主要分为如下5个存储区: (1)栈(Stack):位于函数内的局部变量(包括函数实参),由编译器负责分配释放,函数结束,栈变量失效. (2)堆(Heap):由程 ...

  9. Linux下如何释放内存

    在Linux系统下,我们一般不需要去释放内存,因为系统已经将内存管理的很好.但是凡事也有例外,有的时候内存会被缓存占用掉,导致系统使用SWAP空间影响性能,此时就需要执行释放内存(清理缓存)的操作了. ...

最新文章

  1. linux启动x不启动桌面,redhat开机不启动桌面登录程序
  2. 探秘Hadoop生态12:分布式日志收集系统Flume
  3. 干货|如何在无回显时渗透
  4. android 富文本框架_当微擎框架遇上uniapp,以一当十同时开发十个平台项目
  5. 打算年后跳槽的注意了... 这个岗位,人才缺口30万 薪资水涨船高
  6. GX Works2使用问题记录
  7. python编程语言在线编译手机_groovy在线运行,groovy在线编译,支持手机在线编程写代码 - Groovy教程...
  8. 固态硬盘win10升级之后出现硬盘掉速现象?已解决
  9. 电子元器件识别(图解)
  10. VirtualBox+CentOS6.5安装增强功能包 - Building the main Guest Additions module [失败]
  11. 用 Amazon Web Services 进行云计算,第 3 部分: 用 EC2 根据需要提供服务器
  12. 9. 【gRPC系列学习】连接失败处理:backoff协议
  13. Android中日志打印 Log的使用
  14. 2017 多校训练第二场 HDU 6047 Maximum Sequence(贪心+优先队列)
  15. msn space 决定搬家了
  16. java Word 转 PDF格式
  17. ADI Blackfin DSP处理器-BF533的开发详解7:SPI接口的驱动和应用(含源代码)
  18. c语言程序设计班车管理系统,【2017年整理】班车信息管理系统.doc
  19. CiteSpace的入门学习,分析知网文献与web of science
  20. 中国联通定位平台及其应用

热门文章

  1. 单臂路由配置实验同一交换机上vlan间ping不通_【干货】什么是单臂路由?如何配置?...
  2. php redis 源码分析,从源码中分析关于phpredis中的连接池可持有数目
  3. python threading_【python标准库学习】thread,threading(一)多线程的介绍和使用
  4. python全局名称空间_python之名称空间知识点整理
  5. matlab子函数调用变量,matlab中,怎么样用function自定义函数调用另一个函数名为输入?...
  6. python random函数sample_Python random.seed() random.sample()函数使用
  7. python怎么创建列表_用Python将一个列表分割成小列表的实例讲解 Python 如何创建一个带小数的列表...
  8. 学计算机申请书100字,加入学生会申请书100字范文
  9. C语言中如何求一天是星期几,计算任何一天是星期几的C语言源代码.
  10. @echo off是什么意思_为什么执行自己的程序要在前面加./