response - ASP.net memory usage during download -


On the ASP.NET site, at the place of my work, the following part of the code is responsible for handling file downloads (Note:

  Private Zero DownloadFile (stream stream) {int bytesRead; Int chuck size = 1048576; // 1MB byte [] readBuffer = new byte [chunk size]; While ((bytesRead = stream.Read (readBuffer, 0, readBuffer.Length)) = 0) {If (! Response.IsClientConnected) break; Byte [] part = new byte [bytes] read; Array.Copy (readBuffer, 0, part, 0, bytesRead); Response.BinaryWrite (part); Response.Flush (); } Stream. Stop it (); }  

Our users often download multi-hundred MB files, which can make server memory much faster. My impression is that this reaction is due to buffering. Does this make sense?

I have read about the 'buffer' property of the reaction object now, if I set it to false, then it prevents the reaction. Burnerwit () By buffering data in call memory? In general, what is a good way to limit memory usage in this situation? Maybe I should stream into a temporary file with a zip, then reply to Response.TransmitFile ()?

Edit: In addition to the potential solutions, I am very interested in explaining the issue of memory usage, why the above code consumes more than 1 MB, however Response The flush is called on every loop walking? Is this the only unnecessary heap allocation that happens when each loop is running (and does not yet have GCD), or is there anything else at work?

Here are some code that I'm working for. It uses 8000 byte buffers to send files to chunks. There was a significant reduction in memory allocated from some informal testing on large files.

  int buffersize = 8000; File stream stream = new file stream (filename, filemodel. Open, fileaccess read); Try {long fileSize = stream.Length; Long data lift tiered = file size; Int chuck langeline; Buffer = new byte [buffer size]; While (Data lift read> 0) {if (! Response.IsClientConnected) {break; } ChunkLength = stream.Read (buffer, 0, buffer size); Response.OutputStream.Write (buffer, 0, chuck lang); Response.Flush (); DataLeftToRead - = Choncolamba; }} Finally {if (stream! = Null) {stream.Close (); }  

A syntax error and edit to determine a missing value


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -