Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 324
Piyush Avatar asked May 16 '14 05:05

Piyush


1 Answers

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

like image 93
Kajal Sinha Avatar answered Sep 27 '22 23:09

Kajal Sinha