Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hope this is okay. It's my first answer on StackOverflow.</p> <p>Yes absolutely if your chunksize is over 85000 bytes then the array will get allocated on the large object heap. You will probably not run out of memory very quickly as you are allocating and deallocating contiguous areas of memory that are all the same size so when memory fills up the runtime can fit a new chunk into an old, reclaimed memory area.</p> <p>I would be a little worried about the Array.Resize call as that will create another array (see <a href="http://msdn.microsoft.com/en-us/library/1ffy6686(VS.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/1ffy6686(VS.80).aspx</a>). This is an unecessary step if actualLength==Chunksize as it will be for all but the last chunk. So I would as a minimum suggest:</p> <pre><code>if (actualLength != chunkSize) Array.Resize(ref buffer, actualLength); </code></pre> <p>This should remove a lot of allocations. If the actualSize is not the same as the chunkSize but is still > 85000 then the new array will also be allocated on the Large object heap potentially causing it to fragment and possibly causing apparent memory leaks. It would I believe still take a long time to actually run out of memory as the leak would be quite slow.</p> <p>I think a better implementation would be to use some kind of Buffer Pool to provide the arrays. You could roll your own (it would be too complicated) but WCF does provide one for you. I have rewritten your code slightly to take advatage of that:</p> <pre><code>BufferManager bm = BufferManager.CreateBufferManager(chunkSize * 10, chunkSize); for (int i = resumeChunk; i &lt; chunks; i++) { byte[] buffer = bm.TakeBuffer(chunkSize); try { fileStream.Position = i * chunkSize; int actualLength = fileStream.Read(buffer, 0, (int)chunkSize); if (actualLength == 0) break; //Array.Resize(ref buffer, actualLength); using (MemoryStream stream = new MemoryStream(buffer)) { UploadFile(stream, actualLength); } } finally { bm.ReturnBuffer(buffer); } } </code></pre> <p>this assumes that the implementation of UploadFile Can be rewritten to take an int for the no. of bytes to write.</p> <p>I hope this helps</p> <p>joe</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload