成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

NET內存持續增長問題排查

開發 后端
在某個NET程序的測試過程中,發現該程序的內存持續增長,無法釋放,直到程序關閉后才能釋放。經排查,確定問題的根源是在調用WCF服務的實現代碼中,下面通過簡單代碼來重現問題發生的過程。

一、背景

      在某個NET程序的測試過程中,發現該程序的內存持續增長,無法釋放,直到程序關閉后才能釋放。經排查,確定問題的根源是在調用WCF服務的實現代碼中,下面通過簡單代碼來重現問題發生的過程。

      1、服務端代碼,只提供GetFile操作,返回相對較大的內容,便于快速看到內存持續增長的過程。

 

 

 

  1. class Program 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             using (ServiceHost host = new ServiceHost(typeof(FileImp))) 
  6.             { 
  7.                 host.AddServiceEndpoint(typeof(IFile), new WSHttpBinding(), "http://127.0.0.1:9999/FileService"); 
  8.                 if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null
  9.                 { 
  10.                     ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); 
  11.                     behavior.HttpGetEnabled = true
  12.                     behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/FileService/metadata"); 
  13.                     host.Description.Behaviors.Add(behavior); 
  14.                 } 
  15.                 host.Opened += delegate 
  16.                  { 
  17.                      Console.WriteLine("FileService已經啟動,按任意鍵終止服務!"); 
  18.                  }; 
  19.                 host.Open(); 
  20.                 Console.Read(); 
  21.             } 
  22.         } 
  23.     } 
  24.  
  25.     class FileImp : IFile 
  26.     { 
  27.         static byte[] _fileContent = new byte[1024 * 8]; 
  28.  
  29.         public byte[] GetFile(string fileName) 
  30.         { 
  31.             int loginID = OperationContext.Current.IncomingMessageHeaders.GetHeader<int>("LoginID", string.Empty); 
  32.             Console.WriteLine(string.Format("調用者ID:{0}", loginID)); 
  33.             return _fileContent; 
  34.         } 
  35.     } 

 

 

 

 

      2、客戶端代碼,循環調用GetFile操作,在調用前給消息頭添加一些登錄信息。另外為了避免垃圾回收機制執行的不確定性對內存增長的干擾,在每次調用完畢后,強制啟動垃圾回收機制,對所有代進行垃圾回收,確保增長的內存都是可到達,無法對其進行回收。

 

  1. class Program 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             int callCount = 0
  6.             int loginID = 0
  7.             while (true
  8.             { 
  9.                 using (ChannelFactory<IFile> channelFactory = 
  10.                     new ChannelFactory<IFile>(new WSHttpBinding(), "http://127.0.0.1:9999/FileService")) 
  11.                 { 
  12.                     IFile fileProxy = channelFactory.CreateChannel(); 
  13.                     using (fileProxy as IDisposable) 
  14.                     { 
  15.                         //OperationContext.Current = new OperationContext(fileProxy as IContextChannel); 
  16.                         OperationContextScope scope = new OperationContextScope(fileProxy as IContextChannel); 
  17.                         var loginIDHeadInfo = MessageHeader.CreateHeader("LoginID", string.Empty, ++loginID); 
  18.                         OperationContext.Current.OutgoingMessageHeaders.Add(loginIDHeadInfo); 
  19.                         byte[] fileContent = fileProxy.GetFile(string.Empty); 
  20.                     } 
  21.                 } 
  22.                 GC.Collect();//強制啟動垃圾回收 
  23.                 Console.WriteLine(string.Format("調用次數:{0}", ++callCount)); 
  24.             } 
  25.         } 
  26.     } 

 

 

二、分析排查

 

      要解決內存持續增長的問題,首先需要定位問題,才能做相應的修復。對于邏輯簡單的代碼,可以簡單直接通過排除法來定位問題代碼所在,對于錯綜復雜的代 碼,就需要耗費一定時間了。當然除了排除法,還可以借助內存檢測工具來快速定位問題代碼。對于.net平臺,微軟提供.net輔助工具CLR Profiler幫助我們的性能測試人員以及研發人員,找到內存沒有及時回收,占著內存不釋放的方法。監測客戶端程序運行的結果如下:

      從上圖可看到OperationContextScope對象占用了98%的內存,當前OperationContextScope對象持有256個 OperationContextScope對象的引用,這些OperationContextScope對象總共持有258個 OperationContext的引用,每個OperationContext對象持有客戶端代理的相關對象引用,導致每個客戶端代理產生的內存在使用 完畢后都無法得到釋放。

三、問題解決

      OperationContextScope類主要作用是創建一個塊,其中 OperationContext 對象在范圍之內。也就是說創建一個基于OperationContext 的上下文范圍,在這范圍內共享一個相同的OperationContext對 象。這種上下文的特性是支持嵌套的,即一個大的上下文范圍內可以有若干個小的上下文范圍,而且不會造成相互不干擾。所以如果沒顯式調用該對象的 Dispose方法結束當前上下文恢復前一上下文,再利用OperationContextScope類創建新的上下文,就會一直嵌套下去。所以在這里應 該要顯式調用Dispose方法結束當前OperationContextScope上下文范圍,這樣可以解決內存持續增長的問題了。

 

 

 

  1. class Program 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             int callCount = 0
  6.             int loginID = 0
  7.             while (true
  8.             { 
  9.                 using (ChannelFactory<IFile> channelFactory = 
  10.                     new ChannelFactory<IFile>(new WSHttpBinding(), "http://127.0.0.1:9999/FileService")) 
  11.                 { 
  12.                     IFile fileProxy = channelFactory.CreateChannel(); 
  13.                     using (fileProxy as IDisposable) 
  14.                     { 
  15.                         //OperationContext.Current = new OperationContext(fileProxy as IContextChannel); 
  16.                         using (OperationContextScope scope = new OperationContextScope(fileProxy as IContextChannel)) 
  17.                         { 
  18.                             var loginIDHeadInfo = MessageHeader.CreateHeader("LoginID", string.Empty, ++loginID); 
  19.                             OperationContext.Current.OutgoingMessageHeaders.Add(loginIDHeadInfo); 
  20.                         } 
  21.                         byte[] fileContent = fileProxy.GetFile(string.Empty); 
  22.                     } 
  23.                 } 
  24.                 GC.Collect();//強制啟動垃圾回收 
  25.                 Console.WriteLine(string.Format("調用次數:{0}", ++callCount)); 
  26.             } 
  27.         } 
  28.     } 

 

 

 四、問題根源

      OperationContextScope為什么能持有大量的OperationContext引用?從CLR Profiler工具獲取的結果中可以看到OperationContextScope對象通過其內部OperationContextScope對象來 持有大量OperationContext對象引用,可以推斷該類應該有一個OperationContextScope類型的字段。下面看一下OperationContextScope類的源碼。

 

 

  1. public sealed class OperationContextScope : IDisposable 
  2.     { 
  3.         [ThreadStatic] 
  4.         static OperationContextScope currentScope; 
  5.   
  6.         OperationContext currentContext; 
  7.         bool disposed; 
  8.         readonly OperationContext originalContext = OperationContext.Current; 
  9.         readonly OperationContextScope originalScope = OperationContextScope.currentScope; 
  10.         readonly Thread thread = Thread.CurrentThread; 
  11.   
  12.         public OperationContextScope(IContextChannel channel) 
  13.         { 
  14.             this.PushContext(new OperationContext(channel)); 
  15.         } 
  16.   
  17.         public OperationContextScope(OperationContext context) 
  18.         { 
  19.             this.PushContext(context); 
  20.         } 
  21.   
  22.         public void Dispose() 
  23.         { 
  24.             if (!this.disposed) 
  25.             { 
  26.                 this.disposed = true
  27.                 this.PopContext(); 
  28.             } 
  29.         } 
  30.   
  31.         void PushContext(OperationContext context) 
  32.         { 
  33.             this.currentContext = context; 
  34.             OperationContextScope.currentScope = this
  35.             OperationContext.Current = this.currentContext; 
  36.         } 
  37.   
  38.         void PopContext() 
  39.         { 
  40.             if (this.thread != Thread.CurrentThread) 
  41.                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInvalidContextScopeThread0))); 
  42.   
  43.             if (OperationContextScope.currentScope != this
  44.                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxInterleavedContextScopes0))); 
  45.   
  46.             if (OperationContext.Current != this.currentContext) 
  47.                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.SFxContextModifiedInsideScope0))); 
  48.   
  49.             OperationContextScope.currentScope = this.originalScope; 
  50.             OperationContext.Current = this.originalContext; 
  51.   
  52.             if (this.currentContext != null
  53.                 this.currentContext.SetClientReply(nullfalse); 
  54.         } 
  55.     } 

 

 

      當前的上下文對象由線程***的靜態字段currentScope持有,其實例字段originalScope保持前一上下文對象的引用,如果使用完畢后不 結束當前上下文范圍,就會一直嵌套下去,導致所有OperationContext對象都保持可到達,垃圾回收機制無法進行回收,從而使得內存持續增長, 直到內存溢出。

     

五、總結

      類似OperationContextScope,TranscationScope以XXXScope結尾的類都可以看作Context+ContextScope的設計方式(參考Artech大神的博文:Context+ContextScope——這是否可以看作一種設計模式?),用于在同一范圍內共享同一事物或對象。在使用這類上下文對象的時候,確保使用using關鍵字來使得上下文范圍邊界可控。

 

 

責任編輯:王雪燕 來源: 博客園
相關推薦

2022-01-13 15:11:12

區塊鏈數字化轉型技術

2012-02-01 08:40:07

Opera瀏覽器Opera Mini

2023-09-14 10:39:05

Google開發者

2013-08-01 09:14:54

10GbE40GbESDN

2021-02-04 10:35:25

聯想新基建數字化轉型

2021-02-25 09:44:22

低代碼GartnerForrester

2021-12-13 10:55:44

Mozilla財報收入增長

2010-01-27 16:08:05

SAS零售行業

2010-03-01 15:34:19

IP呼叫中心

2023-03-10 14:41:12

物聯網IoT

2019-01-18 15:09:21

大數據IDC金融

2013-08-20 10:01:12

橫向擴展架構IT趨勢數據中心

2018-09-25 09:03:10

Windows 10微軟

2023-01-09 14:07:49

戴爾

2009-09-24 18:17:37

互聯網域名域名系統威瑞信

2015-02-01 15:18:51

微軟Azure云服務

2018-04-26 22:36:05

物聯網物聯網平臺支出

2017-04-26 12:03:32

短信

2019-04-15 11:00:51

人工智能AI

2020-12-01 10:43:02

云計算IT公有云
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 中文字幕在线一区二区三区 | 日韩中文字幕在线不卡 | 国产一区二区免费 | 亚洲欧美日韩中文在线 | 日韩久久久久久久久久久 | 亚洲一区二区中文字幕 | 久久久这里只有17精品 | 日本三级全黄三级三级三级口周 | 一区二区三区播放 | 国产精品久久久久久婷婷天堂 | 亚洲网视频 | 免费在线黄 | 日本久久黄色 | 欧美日韩精选 | 日韩在线一区视频 | 国产成人99 | 亚洲一区二区三区在线视频 | 日韩在线观看 | 狠狠躁躁夜夜躁波多野结依 | 亚洲精彩免费视频 | 国产欧美精品一区二区三区 | 国产99免费视频 | 久久黄网| 久久久精彩视频 | 在线精品一区二区三区 | 国产美女在线看 | 在线观看视频中文字幕 | 欧美综合视频在线 | 欧美一区二区三区在线观看 | 亚洲欧美日韩一区 | 在线观看中文字幕 | 久草在线在线精品观看 | 性欧美精品一区二区三区在线播放 | 日韩中文字幕在线视频观看 | 五月花丁香婷婷 | 久久久av | 日韩中文电影 | 国产成人jvid在线播放 | 99亚洲| 日韩在线视频一区 | 欧美日韩精品一区二区三区四区 |