gpt4 book ai didi

.net - sos.dll 返回的对象大小与内存中进程大小不匹配

转载 作者:行者123 更新时间:2023-12-02 00:41:48 26 4
gpt4 key购买 nike

我使用了以下 sos 命令来枚举正在运行的 asp 应用程序(托管在 windows xp 4 GB 机器上)中特定类型的所有实例。

.foreach (obj { !dumpheap -type ::my type:: -short ::start of address space:: ::end of address space:: }) { !objsize ${obj} }.

这枚举了 gc gen2 中给定类型的所有对象。

对象的平均大小似乎在 500 KB 左右,大约有 2000 个对象。仅此一项就增加了大约 1 GB 的内存,而我在任务管理器中的 asp 进程内存只显示了大约 700 MB。还有一点是我没有考虑我正在使用的其他加载对象。

此外,上述所有对象都是不会被垃圾回收的根对象。不确定这个命令是否错误,或者是否有任何其他解释 sos 返回的大小不匹配以及任务管理器中显示的内容?

提前致谢,
巴拉斯 K.

最佳答案

!objsize 计算实例的大小,包括其所有引用的对象,因此如果您有任何对象共享对其他对象的引用,则这些对象的大小将被计算多次。最常见的来源可能是字符串,因为文字字符串是驻留的,因此在使用相同文字文本的对象之间共享。但是,您也可能有引用相同对象的集合。在任何情况下,总和都是不正确的,除非被计数的对象根本不共享任何引用。

考虑这个例子

class SomeType {
private readonly string Text;

public SomeType(string text) {
Text = text;
}
}

和这段代码

var st1 = new SomeType("this is a long string that will be stored only once due to interning");
var st2 = new SomeType("this is a long string that will be stored only once due to interning");

在 WinDbg 中

0:006> !dumpheap -type Some
Address MT Size
00ceb44c 00b738a8 12
00ceb458 00b738a8 12

0:006> !objsize 00ceb44c
sizeof(00ceb44c) = 164 ( 0xa4) bytes (TestApp.SomeType)
0:006> !objsize 00ceb458
sizeof(00ceb458) = 164 ( 0xa4) bytes (TestApp.SomeType)

0:006> !DumpObj 00ceb44c
Name: TestApp.SomeType
MethodTable: 00b738a8
EEClass: 00b714bc
Size: 12(0xc) bytes
File: c:\dev2010\FSharpLib\TestApp\bin\Release\TestApp.exe
Fields:
MT Field Offset Type VT Attr Value Name
79b9d2b8 4000001 4 System.String 0 instance 00ceb390 Text
0:006> !DumpObj 00ceb458
Name: TestApp.SomeType
MethodTable: 00b738a8
EEClass: 00b714bc
Size: 12(0xc) bytes
File: c:\dev2010\FSharpLib\TestApp\bin\Release\TestApp.exe
Fields:
MT Field Offset Type VT Attr Value Name
79b9d2b8 4000001 4 System.String 0 instance 00ceb390 Text

正如您从 !dumpobj 的输出中看到的,它们共享相同的引用,因此如果您将上面 !objsize 报告的大小相加,则字符串被计算两次。

关于.net - sos.dll 返回的对象大小与内存中进程大小不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2316460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com