具有大量句柄的.Net应用程序的高内存使用量

时间:2016-03-15 09:50:49

标签: .net windows-services windbg postmortem-debugging

我正在使用带有进程转储的WinDbg对高内存压力.net应用程序进行事后分析,此过程是Windows服务。

我有一种感觉,这14GB的这个进程内存消耗大部分来自中止的线程,因此有很多孤儿信号量/事件/突变等。但是我无法将所有这些放在一起并将它们加起来像多少单个信号量/ events / mutant所需的内存以及哪种WinDbg命令会对这种情况有所帮助?

以下是WinDbg输出:

!手柄

**Type                      Count**

None                        90
Event                       5550
Section                     41
File                        1166
Directory                   3
Mutant                      160
Semaphore                   4581
Key                         78
Token                       2
Thread                      553
IoCompletion                6
Timer                       1
TpWorkerFactory             3
ALPC Port                   9
WaitCompletionPacket        33

!address -summary

--- Usage Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal

<unknown>                               471        3`86ea2000 (  14.108 Gb)  92.40%    0.01%

!threads (列出的大量线程都有ThreadAbortException异常)

Lock  
   ID OSID ThreadOBJ           State GC Mode     GC Alloc Context                  Domain           Count Apt Exception

  12    3 33f0 00000017e3c23200 1282b221 Preemptive  0000000000000000:0000000000000000 00000017e3bb3930 1     MTA System.Threading.ThreadAbortException 000000181de5d668

1 个答案:

答案 0 :(得分:1)

手柄及其包装纸可能非常小。使用dt -r了解其大小:

0:025> dt -r ntdll!_KSEMAPHORE
   +0x000 Header           : _DISPATCHER_HEADER
      +0x000 Type             : UChar
      +0x001 TimerControlFlags : UChar
      +0x001 Absolute         : Pos 0, 1 Bit
      +0x001 Coalescable      : Pos 1, 1 Bit
      +0x001 KeepShifting     : Pos 2, 1 Bit
      +0x001 EncodedTolerableDelay : Pos 3, 5 Bits
      +0x001 Abandoned        : UChar
      +0x001 Signalling       : UChar
      +0x002 ThreadControlFlags : UChar
      +0x002 CpuThrottled     : Pos 0, 1 Bit
      +0x002 CycleProfiling   : Pos 1, 1 Bit
      +0x002 CounterProfiling : Pos 2, 1 Bit
      +0x002 Reserved         : Pos 3, 5 Bits
      +0x002 Hand             : UChar
      +0x002 Size             : UChar
      +0x003 TimerMiscFlags   : UChar
      +0x003 Index            : Pos 0, 6 Bits
      +0x003 Inserted         : Pos 6, 1 Bit
      +0x003 Expired          : Pos 7, 1 Bit
      +0x003 DebugActive      : UChar
      +0x003 ActiveDR7        : Pos 0, 1 Bit
      +0x003 Instrumented     : Pos 1, 1 Bit
      +0x003 Reserved2        : Pos 2, 4 Bits
      +0x003 UmsScheduled     : Pos 6, 1 Bit
      +0x003 UmsPrimary       : Pos 7, 1 Bit
      +0x003 DpcActive        : UChar
      +0x000 Lock             : Int4B
      +0x004 SignalState      : Int4B
      +0x008 WaitListHead     : _LIST_ENTRY
         +0x000 Flink            : Ptr64 _LIST_ENTRY
         +0x008 Blink            : Ptr64 _LIST_ENTRY
   +0x018 Limit            : Int4B

因此Semaphore的大小为0x18 + 4字节。请注意,如果使用Ptr部分,则尺寸可能会增加,例如如果你有一个非常大的等待名单。

我建议您使用!dumpheap -stat。这将输出一个.NET对象列表,按其总大小排序。

通常会有byte[]object[](或类似)和String占用大部分内存。

0:025> !dumpheap -mt 000007feef0aea80
         Address               MT     Size
[...]
000007feef0bda88     4915       329862 System.String
000007feef0be100     1419       382288 System.Object[]
000007feef05f748        2       786520 System.UInt32[]
Total 65575 objects

对于单个对象,您可以使用!objsize <address>。我的例子中的System.Threading.Thread有16 kB。所以即使你有1000个线程,这也只能弥补16 MB的内存。 System.Threading.ManualResetEvent的一个示例在我的应用程序中仅占80个字节。

0:025> !objsize 0000000011878358 
sizeof(0000000011878358) = 16736 (0x4160) bytes (System.Threading.Thread)

0:025> !objsize 00000000118af810 
sizeof(00000000118af810) = 80 (0x50) bytes (System.Threading.ManualResetEvent)