I am trying to upload bytes of a large file (about 30 MB) from HTTPWebRequest to some server. The problem is that since the size of the bytes is greater than 85000 , it is stored in LargeObjectHeap (LOH). The problem is that my code creates at least 5 instances of the same object in the LOH, which are then not deleted from memory even after closing the response stream. The following is a snippet of code that causes this problem. Prior to this block of code, there was only one file instance in LOH.
using (IO.Stream requestStream = webReqest.GetRequestStream()) { List<byte> uploadData = new List<byte>(); uploadData.AddRange(Encoding.UTF8.GetBytes(stringContainingHeaderInfo)); uploadData.AddRange(bytesOfTheLargeFile); byte[] fileFullData = uploadData.ToArray(); requestStream.Write(fileFullData, 0, fileFullData.Length); requestStream.Close(); uploadData.Clear(); uploadData = null; fileFullData = null; fileEntityBytes = null; using (WebResponse webResponse = webRequest.GetResponse()) {
Is there a way to further optimize this code block so that fewer copies are created on the heap.
garbage-collection c # large-object-heap
Piyush
source share