Tuesday, September 28, 2010

Response.Flush and Response.Buffer Property

The Flush method sends buffered output immediately. This method causes a run-time error if Response.Buffer has not been set to TRUE.

The Buffer property specifies whether to buffer the output or not. When the output is buffered, the server will hold back the response to the browser until all of the server scripts have been processed, or until the script calls the Flush or End method.


Note: If this property is set, it should be before the tag in the .asp file

in this example, there will be no output sent to the browser before the loop is finished. If buffer was set to False, then it would write a line to the browser every time it went through the loop.

<%Response.Buffer=true%>

<% for i=1 to 100 response.write(i & "")
next

%>

IIS5 enables buffering by default, therefore all the output from our ASP is actually sent to the browser only when the page completes its processing. In many cases this approach improves the overall processing speed, and indirectly makes for a more scalable site. However, buffering has two minor defects: (1) the end user might perceive a loss in speed, because she won't see any output until the page is complete, and (2) buffered output has to be maintained on the server, therefore your ASP application is going to take more memory than it would do if buffering were disabled. On server machines with an inadequate amount of memory, large HTML pages can reduce the overall performance.

Fortunately, we can solve both problems with judicious use of the Response.Flush method, that lets we flush the output buffer periodically when sending a large amount of data back to the client. For example, if we are sending back to the client data from thousands of records, we might flush the buffer every 100 records, so that the end user sees some results earlier and we don't tax the server's memory.

2 comments:

  1. What is the difference between Response.Flush and Response.End

    ReplyDelete
  2. The Response.End method can perform the Web server to stop processing and return the current output. The remaining process will stopped.

    While the Flush method sends buffered output immediately and unlike response.End it not stop the processing. But we have to set Response.Buffer Property to TRUE.

    ReplyDelete