본문 바로가기
C#

[C#] HttpWebRequest/HttpWebResponse Memory leak 현상 발생 관련 사항

by Hwoarang757 2022. 8. 12.

출처 : c# - WebRequest/WebResponse Memory leak - Stack Overflow

 

WebRequest/WebResponse Memory leak

I have an .Net Framework #4.0 application that makes a large number of web requests using the WebRequest/WebResponse classes , as i see it has memory leak (or maybe i am doing something wrong) I W...

stackoverflow.com

.NET Framework의 HttpWebReqeust , HttpWebResponse Memory가 누수된다는 현상이 보고되고 있습니다.

4.5 이상의 Version을 사용하시게 되면 , .NET Framework에서 제공하는 HttpClient 로 대체하는 것을 권장 드리고 싶습니다.

HttpWebRequest , HttpWebResponse 사용 시에 최대한 Memory 누수를 줄일 수 있는 방안을 찾고 있습니다....

 

//AllowWriteStreamBuffering 를 false 로 처리 
hwreq.AllowWriteStreamBuffering = false;


//httpWebResponse using 및 GetResponse Close 처리 진행 , Stream Close 처리 진행 
            using (HttpWebResponse hwrres = (HttpWebResponse)hwreq.GetResponse())
                    {
                        if (hwrres.StatusCode == HttpStatusCode.InternalServerError)
                        {
                        }

                        if (hwrres.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream stream = hwrres.GetResponseStream())
                            {
                                using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default, true))
                                {
                                    resdata = sr.ReadToEnd();
                                }
                            }
                            hwrres.GetResponseStream().Close();
                            hwrres.GetResponseStream().Dispose();
                        }
                    }



private static string HttpRequest(string url, byte[] bytePost )
        {
            lock (lockObj)
            {
                int resultNum = -1;
                string resdata = "";

                try
                {
                    hwreq = (HttpWebRequest)WebRequest.Create(url);
                    //
                    hwreq.AllowWriteStreamBuffering = false;

                    hwreq.KeepAlive = true;
                    hwreq.Accept = "*/*";
                    hwreq.AllowAutoRedirect = true;
                    hwreq.UserAgent = "http_requester/0.1";
                    hwreq.Timeout = 15 * 1000;
                    hwreq.Method = "POST";

                    
                    hwreq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                    hwreq.ContentLength = bytePost.Length;

                    
                    using (Stream requestStream = hwreq.GetRequestStream()) 
                        requestStream.Write(bytePost, 0, bytePost.Length);

                    
                    using (HttpWebResponse hwrres = (HttpWebResponse)hwreq.GetResponse())
                    {
                        if (hwrres.StatusCode == HttpStatusCode.InternalServerError)
                        {
                        }

                        if (hwrres.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream stream = hwrres.GetResponseStream())
                            {
                                using (StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default, true))
                                {
                                    resdata = sr.ReadToEnd();
                                }
                            }
                            hwrres.GetResponseStream().Close();
                            hwrres.GetResponseStream().Dispose();
                        }
                    }
                }
                catch (WebException ex)
                {
                    resdata = "{\"result\":\"-1\",\"msg\":\"" + ex.Message + "\"}";
                } 
                finally
                {
   
                }


                return resdata;
            }
        }