I am trying to upload the bytes of a large file (around 30MB) with the HTTPWebRequest to some server. The problem is that since the size of bytes is more than 85000, it is getting stored in the LargeObjectHeap(LOH). The problem is that my code is creating at-least 5 instances of the same object in the LOH, which then didn't get removed from the memory even after closing the response stream. Below is the code snippet which is causing this issue. Before this code block there was only one instance of the file in the 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())
{
//Do Something with the response
}
}
Is there a way to further optimize this code block so that less number of copy gets created in the heap.
byte[]
? – GorauploadData.Clear(); uploadData = null; fileFullData = null; fileEntityBytes = null;
– ChemosynthesisbytesOfTheLargeFile
. – Synthesize