Reduce multiple copies of the same object in a bunch of large objects

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()) { //Do Something with the response } } 

Is there a way to further optimize this code block so that fewer copies are created on the heap.

+7
garbage-collection c # large-object-heap
source share
1 answer

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

+1
source share

All Articles