低价网站建设顺德,国外网站推荐,网站的申请,南京机械加工网一#xff1a;背景
1. 讲故事
前些天有位朋友找到我#xff0c;说他生产上的程序有内存暴涨情况#xff0c;让我帮忙看下怎么回事#xff0c;最简单粗暴的方法就是让朋友在内存暴涨的时候抓一个dump下来#xff0c;看一看大概就知道咋回事了。
二#xff1a;Windbg 分…一背景
1. 讲故事
前些天有位朋友找到我说他生产上的程序有内存暴涨情况让我帮忙看下怎么回事最简单粗暴的方法就是让朋友在内存暴涨的时候抓一个dump下来看一看大概就知道咋回事了。
二Windbg 分析
1. 到底是谁吃了内存
这个问题说的再多也不为过一定要看清楚这个程序是如何个性化发展的可以使用 !address -summary 命令。 0:000 !address -summary--- Usage Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal
Free 255 7dfb064e1000 ( 125.981 TB) 98.42%
unknown 529 204d53ac000 ( 2.019 TB) 99.97% 1.58%
Heap 889 0170f0000 ( 368.938 MB) 0.02% 0.00%
Image 1214 007a9a000 ( 122.602 MB) 0.01% 0.00%
Stack 192 005980000 ( 89.500 MB) 0.00% 0.00%
Other 10 0001d8000 ( 1.844 MB) 0.00% 0.00%
TEB 64 000080000 ( 512.000 kB) 0.00% 0.00%
PEB 1 000001000 ( 4.000 kB) 0.00% 0.00%--- State Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal
MEM_FREE 255 7dfb064e1000 ( 125.981 TB) 98.42%
MEM_RESERVE 709 20443eab000 ( 2.017 TB) 99.86% 1.58%
MEM_COMMIT 2190 0b5c64000 ( 2.840 GB) 0.14% 0.00%
从卦象看进程内存也才 2.84G严格来说也不算多可能朋友抓的有点心急从上面的 unknown 指标看大概率是托管堆的暴涨继续使用 !eeheap -gc 观察下托管堆。 0:000 !eeheap -gc
Number of GC Heaps: 4
----------------------------------------
Heap 0 (000001d0adf50a20)
generation 0 starts at 1d0b3fad350
generation 1 starts at 1d0b3f9be88
generation 2 starts at 1d0ae5d1000
ephemeral segment allocation context: none
Small object heapsegment begin allocated committed allocated size committed size 01d0ae5d0000 01d0ae5d1000 01d0b4046258 01d0b48ac000 0x5a75258 (94851672) 0x62dc000 (103661568)
Large object heap starts at 1d4ae5d1000segment begin allocated committed allocated size committed size 01d4ae5d0000 01d4ae5d1000 01d4b6d0c4e8 01d4b6d2d000 0x873b4e8 (141800680) 0x875d000 (141938688)
Pinned object heap starts at 1d4ee5d1000segment begin allocated committed allocated size committed size 01d4ee5d0000 01d4ee5d1000 01d4ee5e4f08 01d4ee5f2000 0x13f08 (81672) 0x22000 (139264)
------------------------------
...
Heap 3 (000001d0ae4fd000)
generation 0 starts at 1d3b26929e0
generation 1 starts at 1d3b2687ad8
generation 2 starts at 1d3ae5d1000
ephemeral segment allocation context: none
Small object heapsegment begin allocated committed allocated size committed size 01d3ae5d0000 01d3ae5d1000 01d4179a5980 01d418021000 0x693d4980 (1765624192) 0x69a51000 (1772425216)
Large object heap starts at 1d4de5d1000segment begin allocated committed allocated size committed size 01d4de5d0000 01d4de5d1000 01d4df8836d8 01d4df884000 0x12b26d8 (19605208) 0x12b4000 (19611648)
Pinned object heap starts at 1d51e5d1000segment begin allocated committed allocated size committed size 01d51e5d0000 01d51e5d1000 01d51e5dd7e0 01d51e5e2000 0xc7e0 (51168) 0x12000 (73728)
------------------------------
GC Allocated Heap Size: Size: 0x8a6b9060 (2322305120) bytes.
GC Committed Heap Size: Size: 0x8c6b1000 (2355826688) bytes.
从GC堆看果然是托管层的问题继续使用 !dumpheap -stat 观察下托管堆的现状看看哪一位是罪魁祸首。 0:000 !dumpheap -stat
Statistics:MT Count TotalSize Class Name
...
7fff32e81db8 43 68,801,032 SmartMeter.Mem.TerminalInfo[]
7fff329f7470 200,000 110,400,000 SmartMeter.Model.MeterInfo_Model
7fff3227d708 2,285,392 116,193,998 System.String
01d0ae46b350 543 1,857,281,320 Free
Total 3,947,969 objects, 2,314,533,332 bytesFragmented blocks larger than 0.5 MB:Address Size Followed By01d0ae935870 723,384 01d0ae9e6228 System.SByte[]01d1b41d3cd0 23,081,616 01d1b57d6f60 System.Byte[]01d3b274eb40 1,696,943,656 01d4179a3968 System.Byte[]
这卦不看不知道一看吓一跳这2.3G的内存居然被一个 1.69G 的Free给侵吞了不信的话可以用 !do 验证下。 0:000 !do 01d3b274eb40
Free Object
Size: 1696943656(0x65254e28) bytes
2. 为什么会有这么大的Free
这是一个值得思考的问题也决定着我们下一步分析的方向接下来就是看下这个 free 的落脚点以及周围对象的分布情况可以使用 !gcwhere 观察。 0:000 !gcwhere 01d3b274eb40
Address Heap Segment Generation Allocated Committed Reserved
01d3b274eb40 3 01d3ae5d0000 0 1d3ae5d1000-1d4179a5980 1d3ae5d0000-1d418021000 1d418021000-1d4ae5d00000:000 !dumpheap -segment 1d3ae5d0000...01d3b274e948 7fff32468658 96 01d3b274e9a8 7fff3227d708 28 01d3b274e9c8 7fff3227d708 28 01d3b274e9e8 7fff32d0c8d8 80 01d3b274ea38 7fff3227d708 96 01d3b274ea98 7fff32d0aa38 40 01d3b274eac0 01d0ae46b350 128 Free01d3b274eb40 01d0ae46b350 1,696,943,656 Free01d4179a3968 7fff323e1638 8,216
从卦象看挺遗憾的如果 Free 落在segment的最后一个位置那么 segment 就会 uncommitted 进而内存就下去了可偏偏最后一个位置是 8216byte 的对象占据着阻止了内存的回收有经验的朋友可能知道这个对象非富即贵大概率是被 pinned 了可以用 !gcroot 观察下。 0:000 !gcroot 01d4179a3968
HandleTable:000001d0ae3927f8 (async pinned handle)- 01d3b26706f0 System.Threading.OverlappedData - 01d4179a3968 System.Byte[] Found 1 unique roots.0:000 !dumpobj /d 1d4179a3968
Name: System.Byte[]
MethodTable: 00007fff323e1638
EEClass: 00007fff323e15b8
Tracked Type: false
Size: 8216(0x2018) bytes
Array: Rank 1, Number of elements 8192, Type Byte (Print Array)
Content: ............L.o.g.\.2.0.2.3.0...
Fields:
None
从上面的 async pinned handle 来看是一个文件监控的回调函数到这里就可以从表象解释是这个 8216 的对象导致的内存无法回收。
3. 真的要 8216 来担责吗
如果你真的要让 8216 来担责那真的只看到了表象内存的突然暴涨回不去只是恰好遇到了 8216 的阻止但它不是本质原因真正要考虑的是为什么GC回收后会产生这么大一个单独 Free其实隐喻了当前程序出现过短时的 大对象分配对就是这个词。
接下来的问题是如何找到这个 大对象分配 呢 最好的方法就是用 perfview 的 .NET SampAlloc 去洞察如果非要用 WinDbg 的话那就只能看看 Free 生前是什么或许能寻找到答案可以借助 .writemem 命令观察。 0:000 !do 01d3b274eb40
Free Object
Size: 1696943656(0x65254e28) bytes0:000 .writemem D:\testdump\1.txt 01d3b274eb40 L?0x65254e28
Writing 65254e28 bytes................ 从卦中数据看有大量的计费信息看样子又是从数据库中短时的捞取了大批量数据在托管堆上折腾导致的知道了本质原因解决办法就比较简单了通常有两种做法。 修改 GC 模式改成 Workstation。 大批量数据 改成 小步快跑
三总结
这起内存暴涨事故表象上是 8216 的阻挡导致了内存无法被uncommitted所致本质上还是归于托管堆的 内存黑洞 现象。 文章转载自: http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.nptls.cn.gov.cn.nptls.cn http://www.morning.yhsrp.cn.gov.cn.yhsrp.cn http://www.morning.zwwhq.cn.gov.cn.zwwhq.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.hlnys.cn.gov.cn.hlnys.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.glbnc.cn.gov.cn.glbnc.cn http://www.morning.qkdcb.cn.gov.cn.qkdcb.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.pbtrx.cn.gov.cn.pbtrx.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.qcymf.cn.gov.cn.qcymf.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.lynb.cn.gov.cn.lynb.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.tpmnq.cn.gov.cn.tpmnq.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.dwwlg.cn.gov.cn.dwwlg.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.mdjzydr.com.gov.cn.mdjzydr.com http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.fwkq.cn.gov.cn.fwkq.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.iznek.com.gov.cn.iznek.com http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.jwwfk.cn.gov.cn.jwwfk.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.sqgsx.cn.gov.cn.sqgsx.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.grcfn.cn.gov.cn.grcfn.cn http://www.morning.dtgjt.cn.gov.cn.dtgjt.cn http://www.morning.bzfwn.cn.gov.cn.bzfwn.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn http://www.morning.qbccg.cn.gov.cn.qbccg.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.snbrs.cn.gov.cn.snbrs.cn http://www.morning.pabxcp.com.gov.cn.pabxcp.com http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn