【问题标题】:Memory leak when using HttpWebRequest object in ASP.NET site在 ASP.NET 站点中使用 HttpWebRequest 对象时发生内存泄漏
【发布时间】:2013-06-02 17:47:27
【问题描述】:

这是我的课:

public class HttpRequestHelper
    {
        public static string GetRequestText(string url, string method, string referer, string postData, int timeout = 50000, string userAgent = null, string proxy = null)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            request.Method = method;
            request.Timeout = timeout;
            if (!string.IsNullOrEmpty(proxy))
                request.Proxy = new System.Net.WebProxy(proxy);
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            if (!userAgent.IsNullOrEmptyText())
            {
                request.UserAgent = userAgent;
            }
            if (!string.IsNullOrEmpty(referer))
            {
                request.Referer = referer;
            }
            if (method == "POST")
            {
                if (!string.IsNullOrEmpty(postData))
                {
                    request.ContentLength = postData.Length;
                    using (Stream writeStream = request.GetRequestStream())
                    {
                        UTF8Encoding encoding = new UTF8Encoding();
                        byte[] bytes = encoding.GetBytes(postData);
                        writeStream.Write(bytes, 0, bytes.Length);
                    }
                }
                else request.ContentLength = 0;
            }
            string result = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        result = readStream.ReadToEnd();
                    }
                }
            }
            return result;
        }
    }

我的网站通常占用大约 170mb 内存(数据库中大约有 40k 条记录)。但是当我调用GetRequestText 时,内存最高可达 1Gb。我不知道为什么?对此有何想法或解决方案?

提前感谢!

【问题讨论】:

  • 那么postData 有多大,响应有多大?
  • 您好乔恩,感谢您的关注。无更新数据。我使用 GET 方法。响应是纯文本(html源代码)。
  • 在这种情况下,您发布的大部分代码都是无关紧要的。如果您只包含 relevant 代码会很有帮助。好的,那么反应有多大?
  • 我收到了来自 whatismyipaddress.com 的 GET 请求,格式为:whatismyipaddress.com/ip/[ipaddress]
  • 好吧,我们仍然不知道导致问题的响应有多大 - 或者你这样做了多少次......如果你正在调用这个方法非常 i> 频繁(每秒数千次),那么也许这就是问题所在。内存增长的速度有多快?

标签: c# asp.net memory


【解决方案1】:

这可能是 .NET 框架试图为您智能地管理内存...在任务管理器中报告的内存实际上不是您的应用程序使用的内存,而是分配给您的应用程序使用的内存。尝试在此方法的末尾添加 GC.Collect() 调用 - 如果它使您的内存恢复到相对正常的大小,那么您无需担心。如果没有,那么您实际上确实存在可能的内存泄漏,其形式为未释放资源的对象或其他形式。

【讨论】:

  • 另外,当您完成测试时,不要将 GC.Collect() 方法留在原处 - 它会大大降低您的应用程序的速度。
  • 不,我的意思是应用程序会变慢。我永远不会使用 GC.Collect()。我不同意这个解决方案。我的意思是我以前试过。但是应用太慢了。
  • 我请你试试看它对你的内存使用有什么影响,从而确认或否认你担心的内存泄漏。
猜你喜欢
  • 1970-01-01
  • 2011-02-12
  • 2018-04-01
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多