Piyush
Piyush

Reputation: 826

Reduce the multiple copies of the same object in the Large Object Heap

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.

Upvotes: 7

Views: 236

Answers (1)

Kajal Sinha
Kajal Sinha

Reputation: 1575

Microsoft has recently introduced LargeObjectHeapCompactionMode for GC in .NET 4.5.1 Please use the following link which might help you: http://msdn.microsoft.com/en-us/library/system.runtime.gcsettings.largeobjectheapcompactionmode(v=vs.110).aspx

Upvotes: 1

Related Questions