調用.NET Serviced Component引發的效能問題及其解決

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

 

在企業使用者環境裡,.NET Serviced Component使用廣泛。它比較好的把傳統COM+封裝和.NET應用邏輯銜接了起來,在伺服器端應用起到重要作用。.NET Serviced Component 的使用需要注意到很多方面,特別是要做到對象資源合理應用(pooling)和及時釋放(Dispose)。現有文章就這方面有很多具體討論。

在這裡,我要分析的是一種以前處理過的特定情況下.NET Serviced Component的引發的High CPU的問題。不只一個客戶遇到此類問題,覺得有必要和大家分享一下。

如果程式出現High CPU,這個應用程式線程一定出現了繁忙的運算,比如重複的計算或者長時間的迴圈,而不是等待資料庫,鎖,或其它資源。如果這個程式是.NET程式,頻繁Garbage Collection導致High CPU就必須要重點考慮。因為每次GC都需要檢查.NET 對象樹和調整.NET堆, 是繁重的操作。我們可以通過捕捉效能日誌(perfmon),查看.NET CLR Memory 對象下的%Time in GC 和 Induced GC 這兩個效能計數器指標,來判斷High CPU是不是和GC有關。

在這個例子裡面,通過查看效能監控器,可以看到Induced GC增長非常的快速,表明是這次High CPU的直接原因:

 

 

通常情況下,Induced GC 出現是客戶代碼裡面直接調用了GC.Collect 。然而,在查看客戶代碼後,發現根本沒有GC.Collect出現。

我們注意到這個程式裡面使用了.NET Enterprise Serviced Components, 開始便建議客戶使用Dispose方法釋放這些Components, 而不是用DisposeObject:

http://msdn.microsoft.com/en-us/library/system.enterpriseservices.servicedcomponent.disposeobject.aspx

並且嘗試啟用對象池,, 發現改善並不明顯:

http://www.codeproject.com/Articles/579/COM-Object-Pooling

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684273(v=vs.85).aspx

於是在這個程式High CPU的時候抓取了Memory Dump。

註:抓取Memory Dump的方法很多,可以用WinDBG, procdump或者DebugDiag

通過仔細分析Memory Dump,發現Induced GC邏輯出現在以下Call Stack裡面:

非托管 Call Stack:

Child-SP          RetAddr           Call Site00000000`0f59bbe8 00000000`7704ce00 ntdll!ZwWaitForSingleObject+0xa00000000`0f59bbf0 000007fe`f52fa74a kernel32!WaitForSingleObjectEx+0x9c00000000`0f59bcb0 000007fe`f52fa83b mscorwks!CLREventWaitHelper+0x4200000000`0f59bd10 000007fe`f53cee38 mscorwks!CLREvent::WaitEx+0x6300000000`0f59bdc0 000007fe`f53c1cb7 mscorwks!SVR::gc_heap::wait_for_gc_done+0x8800000000`0f59be00 000007fe`f542913b mscorwks!SVR::GCHeap::GarbageCollectGeneration+0x14700000000`0f59be90 000007fe`f5412ac3 mscorwks!SVR::GCHeap::GarbageCollect+0x5b00000000`0f59bee0 000007fe`f57301a5 mscorwks!GCInterface::AddMemoryPressure+0x13b00000000`0f59bf70 000007fe`f57b9beb mscorwks!RCW::AddMemoryPressure+0x1500000000`0f59bfb0 000007fe`f57dbbf4 mscorwks!RCW::CreateRCW+0x17b00000000`0f59c030 000007fe`f57dbdea mscorwks!COMInterfaceMarshaler::CreateObjectRef+0x7400000000`0f59c130 000007fe`f58843f4 mscorwks!COMInterfaceMarshaler::WrapWithComObject+0x3a00000000`0f59c1a0 000007fe`f0268995 mscorwks!MarshalNative::WrapIUnknownWithComObject+0x13400000000`0f59c3c0 000007fe`f0270933 System_EnterpriseServices_ni!System.EnterpriseServices.RemoteServicedComponentProxy..ctor(System.Type, IntPtr, Boolean)+0xd500000000`0f59c420 000007fe`f0270213 System_EnterpriseServices_ni!System.EnterpriseServices.FastRSCPObjRef.GetRealObject(System.Runtime.Serialization.StreamingContext)+0x3300000000`0f59c470 000007fe`f37254db System_EnterpriseServices_ni!System.EnterpriseServices.ServicedComponentProxyAttribute.CreateProxy(System.Runtime.Remoting.ObjRef, System.Type, System.Object, System.Runtime.Remoting.Contexts.Context)+0x13300000000`0f59c500 000007fe`f37253ff mscorlib_ni!System.Runtime.Remoting.RemotingServices.SetOrCreateProxy(System.Runtime.Remoting.Identity, System.Type, System.Object)+0x9b00000000`0f59c560 000007fe`f372466f mscorlib_ni!System.Runtime.Remoting.RemotingServices.GetOrCreateProxy(System.Runtime.Remoting.Identity, System.Object, Boolean)+0xbf00000000`0f59c5c0 000007fe`f027050a mscorlib_ni!System.Runtime.Remoting.RemotingServices.InternalUnmarshal(System.Runtime.Remoting.ObjRef, System.Object, Boolean)+0x12f00000000`0f59c650 000007fe`f40e3edf System_EnterpriseServices_ni!System.EnterpriseServices.ServicedComponentProxyAttribute.CreateInstance(System.Type)+0x2ba00000000`0f59c7c0 000007fe`f5416e61 mscorlib_ni!System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(System.Type, System.Object[], Boolean)+0x9ba81f

 

對應的.NET 託管 Call Stack:

Child-SP         RetAddr          Call Site000000000f59c3c0 000007fef0270933 System.EnterpriseServices.RemoteServicedComponentProxy..ctor(System.Type, IntPtr, Boolean)000000000f59c420 000007fef0270213 System.EnterpriseServices.FastRSCPObjRef.GetRealObject(System.Runtime.Serialization.StreamingContext)000000000f59c470 000007fef37254db System.EnterpriseServices.ServicedComponentProxyAttribute.CreateProxy(System.Runtime.Remoting.ObjRef, System.Type, System.Object, System.Runtime.Remoting.Contexts.Context)000000000f59c500 000007fef37253ff System.Runtime.Remoting.RemotingServices.SetOrCreateProxy(System.Runtime.Remoting.Identity, System.Type, System.Object)000000000f59c560 000007fef372466f System.Runtime.Remoting.RemotingServices.GetOrCreateProxy(System.Runtime.Remoting.Identity, System.Object, Boolean)000000000f59c5c0 000007fef027050a System.Runtime.Remoting.RemotingServices.InternalUnmarshal(System.Runtime.Remoting.ObjRef, System.Object, Boolean)000000000f59c650 000007fef40e3edf System.EnterpriseServices.ServicedComponentProxyAttribute.CreateInstance(System.Type)

通過上面的調用棧資訊,可以得知當serviced componnet被建立的時候,.NET RCW 邏輯會在mscorwks!RCW::CreateRCW  裡調用GC.AddMemoryPressure 對可能的額外記憶體需要做提前預估. 當記憶體在不停擴充時, AddMemoryPressure會增加 Induced GC 的計數(如我們在效能監控器裡發現的), 並且調用GarbageCollect。

為瞭解決這種問題,我們需要在應用程式裡減少建立Serviced Component的次數。光在COM+裡面修改成Object Pooling是不夠的,因為這種Induced GC是在每次應用程式調用RCW的時候都會發生。通過研究代碼和測試,如在每次調用建立Serviced Component之前使用 RemoveMemoryPressure(4004) 用來抵消RCW裡的相同bytes的AddMemoryPressure(4004)可以臨時解決這個問題.

最終解決方案是我們在應用程式本身建立了自己的對象池(建立一組全域對象,簡單管理它們的存在周期),以徹底減少CreateInsance和CreateRCW的調用。這樣處理以後,即使在大壓力情況下, 程式運行也是良好的。

調用.NET Serviced Component引發的效能問題及其解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.