Basic Windbg - 1. SOSBasics(总结)
我们都知道,对于字符串相加,建议使用StringBuilder,而不是普通的string concat,为什么呢?我们通过dump简单看一下。
先看这个代码:
Code
对于hello字符串,我们加10000次,然后得到时间差。对于world字符串,我们也加10000次,然后得到时间差。按照前面讲的抓dump的方法,我们得到dump,然后看!dumpheap -stat的结果:
0:000> !dumpheap -stat
total 3686 objects
Statistics:
MT Count TotalSize Class Name
791334a8 1 12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]]
79119954 1 12 System.Security.Permissions.ReflectionPermission
79119834 1 12 System.Security.Permissions.FileDialogPermission
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
79104368 266 6384 System.Collections.ArrayList
7912d8f8 299 31408 System.Object[]
004ad640 31 1060716 Free
790fd8c4 2399 2423452 System.String
Total 3686 objects
然后我们看字符串的东西:!dumpheap -mt 790fd8c4 -strings(strings这个参数的意思是,只输出字符串,其他的都省略掉),结果如下:
0:000> !dumpheap -mt 790fd8c4 -strings
total 2399 objects
Statistics:
Count TotalSize String Value
1 20 "}"
1 20 "{"
1 20 "x"
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
2 5600 "<PermissionSet class="System.Security.NamedPermissionSet"versio"
1 9280 "<NamedPermissionSets><PermissionSet class="System.Security.Name"
10 262092 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20 1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2399 objects
值得我们注意的是,world字符串(一大堆),一共有10个,大小是262092,hello字符串一共有20个,大小是1998520。两者为啥相差这么大?书上说,string concat会不停的产生临时对象,这些对象都会占用内存的,so,会比较慢,也比较吃内存。而StringBuilder是预先分配好了一块内存来用,so,会快一些。从上面的结果来看,貌似是这样的。
好,那我们修改一下代码吧!
Code
我们对StringBuilder的参数做点手脚,让它一次性的初始化64K大小,重新抓dump,然后重新用!dumpheap -mt和!dumpheap -stat来看。
1 131096 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20 1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2387 objects
这个结果比较有意思吧!如果我们让stringBuilder的buffer足够大,那么它可以避免频繁分配内存。这个例子来看,字符串只分配了两次而已(上面还有一个5个字节的,我没有列出)
第一部分暂时到此,写的大脑缺氧,过两天再写。。。
先看这个代码:

对于hello字符串,我们加10000次,然后得到时间差。对于world字符串,我们也加10000次,然后得到时间差。按照前面讲的抓dump的方法,我们得到dump,然后看!dumpheap -stat的结果:
0:000> !dumpheap -stat
total 3686 objects
Statistics:
MT Count TotalSize Class Name
791334a8 1 12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]]
79119954 1 12 System.Security.Permissions.ReflectionPermission
79119834 1 12 System.Security.Permissions.FileDialogPermission
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
79104368 266 6384 System.Collections.ArrayList
7912d8f8 299 31408 System.Object[]
004ad640 31 1060716 Free
790fd8c4 2399 2423452 System.String
Total 3686 objects
然后我们看字符串的东西:!dumpheap -mt 790fd8c4 -strings(strings这个参数的意思是,只输出字符串,其他的都省略掉),结果如下:
0:000> !dumpheap -mt 790fd8c4 -strings
total 2399 objects
Statistics:
Count TotalSize String Value
1 20 "}"
1 20 "{"
1 20 "x"
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
2 5600 "<PermissionSet class="System.Security.NamedPermissionSet"versio"
1 9280 "<NamedPermissionSets><PermissionSet class="System.Security.Name"
10 262092 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20 1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2399 objects
值得我们注意的是,world字符串(一大堆),一共有10个,大小是262092,hello字符串一共有20个,大小是1998520。两者为啥相差这么大?书上说,string concat会不停的产生临时对象,这些对象都会占用内存的,so,会比较慢,也比较吃内存。而StringBuilder是预先分配好了一块内存来用,so,会快一些。从上面的结果来看,貌似是这样的。
好,那我们修改一下代码吧!

1 131096 "worldworldworldworldworldworldworldworldworldworldworldworldwor"
20 1998520 "hellohellohellohellohellohellohellohellohellohellohellohellohel"
Total 2387 objects
这个结果比较有意思吧!如果我们让stringBuilder的buffer足够大,那么它可以避免频繁分配内存。这个例子来看,字符串只分配了两次而已(上面还有一个5个字节的,我没有列出)
第一部分暂时到此,写的大脑缺氧,过两天再写。。。
分类:
Basic Debug
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?