display | more...

ASP Response Object : Flush Method

The Flush method sends all buffered output to the end user immediately. If buffering is not set to True1 then calling this method will result in a runtime error. Calling the Flush method will also disable HTTP KeepAlive2 for the current page.


  Response.Buffer = True
  Response.Write = "Hello World!"
In this example, "Hello World!" would be sent immediately following the call to the Flush method instead of being buffered until the entire script has been processed.

Tips & Hints

Send Updates To Your Users
Since HTTP performs better when sending a few large blocks of data instead of many small blocks3, buffering should almost always be turned on for your ASP pages.

However, some pages are so large that buffering and then sending the entire page will cause considerable delay for the end user. This delay may result in the end user hitting the refresh button (placing them at the end of the Request queue) or even leaving your site. To avoid these delays, you should periodically flush the server's buffer for very large pages. This allows the end user to see results sooner, and still takes advantage of HTTP's better performance when sending large blocks of data.

Example (in VBScript):

  Response.Buffer = True
  'hidden: code to open ADO connection to a
  'database and retrieve a recordset.

  dim count
  count = 0
  While NOT myRecordset.EOF
    'this assumes the database has a field called "Title"
    Response.Write myRecordset("Title") & "<br>"
    count = count + 1

    If count = 1000 Then
      'flush the buffer and reset count.
      count = 0
    End If
The code above prints a list of titles from a database. After every 1,000 titles, the buffer is flushed, sending the information processed so far to the end user. If there were 10,000 titles in the database, then by flushing the buffer after the first 1,000 instead of waiting for the entire script to finish processing we decrease the end user's perceived delay by a factor of 10 (10000/1000 = 10). At the same time, we are still sending data in blocks of 1000, taking advantage of HTTP's improved performance for large blocks of data.

Back to the ASP Response Object
Back to ASP Objects
1 In IIS 5.0, buffering is True by default.
2 When the HTML KeepAlive header is sent the client, the client and server try to maintain a connection instead of reconnecting on every subsequent request. This is sent by default in HTTP/1.1, and is also sent by ASP when buffering is True. It was implemented to facilitate a performance gain (up to 50% over HTTP w/o KeepAlive in best-case scenarios).
3 Because of HTTP's use of the slow start and Nagling algorithms to minimize network congestion.


All code is my own (and is my common law spouse in 14 states).

Log in or register to write something here or to contact authors.