2010-02-23 45 views
3

我已經使用以下sos命令來枚舉正在運行的asp應用程序(託管在Windows xp 4 GB計算機上)中的特定類型的所有實例。由sos.dll和內存中進程大小返回的對象大小不匹配

.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返回的大小不匹配以及task manager中顯示的內容?

由於提前,
巴拉斯K.

+0

你在任務管理器中查看什麼計數器? – 2010-02-23 07:53:50

+0

Btw'!dh'顯示指定圖像的標題。我假設你的意思是'!dumpheap'。 – 2010-02-23 08:08:07

+0

是的。它是!dumpheap。我在任務管理器中查看了內存使用情況和內存使用情況峯值。 – 2010-02-23 08:18:00

回答

3

!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報告的大小求和,字符串就是c安裝了兩次。

+0

謝謝Rasmussen,我的每個對象都包含一個獨特的xml文檔,它是從本質上獨特的窗體(字符串)的定義構造而來的。再次感謝偉大的洞察力。我會再次驗證字符串是否確實被攔截,並在這種情況下將此問題標記爲已關閉。 – 2010-02-23 09:40:36