Web API导致内存使用率达到99%,没有下拉

时间:2017-06-08 06:02:06

标签: c# .net asp.net-mvc api azure

我正在使用MVC 4&创建了一个独立实用程序应用程序,每隔30分钟就会将数据发送到云数据库。

我可以在云服务器上注意到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();
            }
         }

1 个答案:

答案 0 :(得分:1)

  

var rawJson = new StreamReader(d1).ReadToEnd();

根据您的API方法,我发现您没有发布StreamReader资源。我想这就是你的CPU内存为99%的原因。

当您调用Dispose时,StreamReader,StreamWriter,BinaryReader和BinaryWriter都会关闭/处置它们的基础流。如果读者/作者只是垃圾收集,他们不会处理流 - 你应该总是处理读者/作者,最好是使用using语句。

所以我建议您可以更改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
                }