Is .net Response.OutputStream.Write asynchronous?

Posted on 2009-04-08
Last Modified: 2013-11-08
On Micorsofts website ( there's an article showing how to download very large files from a website.

Works great but...

In their sample code (see code section) they allocate a 10,000 byte buffer to chunk the byte file stream to the output stream. Fine. But in the loop they re-allocate a new 10,000 byte buffer for each chunk. Seems like a waste of memory.

I can only image that response.outputstream.write is running in a separate thread and may still be using the original buffer. I can't find any info on threading for this method.

Any thoughts on why/if the reallocation is necessary?
System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:

byte[] buffer = new Byte[10000];

// Length of the file:

int length;

// Total bytes to read:

long dataToRead;

// Identify the file to download including its path.

string filepath  = "DownloadFileName";

// Identify the file name.

string  filename  = System.IO.Path.GetFileName(filepath);



	// Open the file.

	iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 


	// Total bytes to read:

	dataToRead = iStream.Length;

	Response.ContentType = "application/octet-stream";

	Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

	// Read the bytes.

	while (dataToRead > 0)


		// Verify that the client is connected.

		if (Response.IsClientConnected) 


			// Read the data in buffer.

			length = iStream.Read(buffer, 0, 10000);

			// Write the data to the current output stream.

			Response.OutputStream.Write(buffer, 0, length);

			// Flush the data to the HTML output.


			buffer= new Byte[10000];

			dataToRead = dataToRead - length;




			//prevent infinite loop if user disconnects

			dataToRead = -1;




catch (Exception ex) 


	// Trap the error, if any.

	Response.Write("Error : " + ex.Message);




	if (iStream != null) 


		//Close the file.





Open in new window

Question by:mduffin06

Accepted Solution

burningmace earned 500 total points
ID: 24100186
It isn't necessary at all - the code would simply overwrite the old data with the new. You can comment line 46 out with no problems at all. I think the programmer did this just to make sure that the old data would not cause complications. However, it wouldn't be a waste of memory as the garbage collector would recognise that line 46 causes the old buffer asset to become orphaned and would unallocate it. I'm not sure how GC scheduling is handled in .NET, but the use of New may signal GC automatically to check if an old asset is being replaced. If not, then this method probably wastes around 500K of memory at peak before GC notices and clear it. Not really a huge issue with most systems having 1GB of memory or more these days. But yes, it is bad practice to do things like that.

Author Closing Comment

ID: 31568143
Per burningmace advice, removed new allocation. Seems to work fine. No problems to date.

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now