C# 在ASP.NET站点中使用HttpWebRequest对象时内存泄漏

C# 在ASP.NET站点中使用HttpWebRequest对象时内存泄漏,c#,asp.net,memory,C#,Asp.net,Memory,这是我的班级: public class HttpRequestHelper { public static string GetRequestText(string url, string method, string referer, string postData, int timeout = 50000, string userAgent = null, string proxy = null) { HttpWebReque

这是我的班级:

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。我不知道为什么?对此有什么想法或解决方案吗


提前谢谢

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

那么
postData
有多大,响应有多大?嗨,Jon,感谢您的关注。没有post数据。我正在使用GET方法。回复是纯文本(html源代码)。在这种情况下,您发布的代码中有很大一部分是不相关的。如果您只包含相关代码,这将非常有用。好的,那么响应有多大?我从whatismyipaddress.com收到了一个请求,请求的格式是:我们仍然不知道引起问题的响应有多大,或者您这样做了多少次。。。如果您非常频繁地调用此方法(每秒数千次),那么这可能就是问题所在。内存增长的速度有多快?在完成测试时,也不要将GC.Collect()方法留在原地-它会大大降低应用程序的速度。不,我的意思是应用程序会降低速度。我永远不会使用GC.Collect()。我不同意这种解决办法。我是说我以前试过。但是这个应用程序太慢了。我要求你试试看它对你的内存使用有什么影响,从而确认或否认你担心的内存泄漏。