I am using HttpWebRequest object to upload files using SSL with client certificate, I have a valid certificate on my server, My application is having a memory leak issue and Microsoft has posted something related to the issue on the following link:
FIX: Memory Leak When SSL and Client Certificates Are Used With the HttpWebRequest Object
Is there any work around to avoid this memory leak specially that each request is consuming 8K leaked memory, this will cause my application to consume so much memory.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
memory leaks were detected using .NET Memory Profiler and it shows the HttpWebRequest object has array of bytes that leaks the memory, I am deposing both stream and request objects.
I have tried this case with SSL and without SSL the leaks have disappeared in non SSL Requests.
simply set the
AllowWriteStreamBufferingproperty of yourHttpWebRequestobject to false:Note : The Framework caches SSL sessions as they are created and attempts to reuse a cached session for a new request, if possible. When attempting to reuse an SSL session, the Framework uses the first element of ClientCertificates (if there is one), or tries to reuse an anonymous sessions if ClientCertificates is empty.
Other Note : For performance reasons, you shouldn’t add a client certificate to a HttpWebRequest unless you know the server will ask for it.
For a code example illustrating how to enumerate the certificates in the client certificate store, see the X509Certificate2Collection class.
Or try to use this :