2017-06-08 23 views
0

我正在使用MVC 4 &創建了一個獨立實用程序應用程序,該應用程序每30分鐘向雲數據庫發送一次數據。Web API導致內存使用率99%,沒有下降

我可以注意到雲服務器CPU內存使用率高達99%&我必須在發生這種情況時重新啓動IIS。當ti達到90%以上時,內存沒有下降。

有什麼我可以在我的代碼中更改?或者如何處理這個?

以下是代碼: - API調用方法: -

List<IEnumerable<Logs>> listOfLists = new List<IEnumerable<Logs>>(); 
          for (int i = 0; i < lst.Count(); i += 50) 
          { 
           listOfLists.Add(lst.Skip(i).Take(50)); 
          } 
          int count = 0; 
          foreach (var data in listOfLists) 
          { 
           count = count + data.Count(); 
           string url = UrlLink + "/api/SaveEmployee?Schema=" + Schema; 
           HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
           request.Timeout = 20000000; request.Proxy = null; request.KeepAlive = true; 
           request.KeepAlive = true; 
           request.ContentType = "application/json; charset=utf-8"; 
           request.Accept = "application/json, text/javascript, *"; request.Proxy = null; 
           request.Method = "Post"; 
           using (var streamWriter = new StreamWriter(request.GetRequestStream())) 
           { 
            streamWriter.Write(JsonConvert.SerializeObject(data)); 
            streamWriter.Flush(); 
           } 
           try 
           { 
            using (WebResponse response = request.GetResponse()) 
            { 
             Stream stream = response.GetResponseStream(); 
             var rawJson = new StreamReader(stream).ReadToEnd(); 
             lst = (JsonConvert.DeserializeObject<List<Logs>>(rawJson)); 

               using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) 
               using (StreamWriter sw = new StreamWriter(fs)) 
               {              
                sw.WriteLine(data.Count()); 
               } 
            } 
           } 
           catch (Exception ee) 
           { 

           } 
           finally 
           { 
            request.Abort(); 
           } 
} 

API方法: -

[AcceptVerbs("GET", "POST")] 
     public void SaveEmployee(string Schema) 
     { 
      using (var ctx = new ApplicationDbContext(Schema)) 
      { 
       ctx.Configuration.AutoDetectChangesEnabled = false; 
       var d1 = Request.Content.ReadAsStreamAsync().Result; 
       var rawJson = new StreamReader(d1).ReadToEnd(); 
      } 
     } 
+0

這是蔚藍的Web應用程序或獨立的VM?你嘗試增加實例的數量嗎? ? – Aravind

回答

1

VAR rawJson =新的StreamReader(D1).ReadToEnd();

根據您的API方法,我發現您不會釋放StreamReader資源。我想這就是你的CPU內存爲99%的原因。

StreamReader,StreamWriter,BinaryReader和BinaryWriter在您對其調用Dispose時全部關閉/處置其基礎流。如果讀者/作者只是垃圾收集,他們不會處理流 - 你應該總是處理讀寫器,最好是使用聲明。

因此,我建議你可以改變,如下的WebAPI方法:

 ctx.Configuration.AutoDetectChangesEnabled = false; 
     var d1 = Request.Content.ReadAsStreamAsync().Result; 
     using (StreamReader sr = new StreamReader(d1)) 
       { 
        string rawJson = sr.ReadToEnd(); 
        //....Then you could return the response 
       } 
+0

謝謝。我將更改代碼並嘗試此操作。 – Anup

+0

我實現了代碼,但內存仍高達97%。 – Anup

+0

你能告訴我你的web api託管在azure web應用程序或azure VM嗎? –