Limit on the Number of Bytes Written Asynchronously

Last reviewed: November 2, 1995
Article ID: Q98893
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT version 3.1
    

SUMMARY

There is a limit to the number of bytes that can be written with WriteFile() using asynchronous I/O (FILE_FLAG_OVERLAPPED specified). This limit depends on the size of your system.

Asynchronous (overlapped) I/O consumes system resources for a long time. For example, the memory used is locked in the process working set until the I/O completes. To limit the amount of system resources used asynchronously by an application, the system charges asynchronous I/O to the working set of the process requesting the I/O.

While the working set size is dynamically raised and lowered based on the load, there are minimum and maximum values. These values are based on system size: consider up to 12 megabytes (MB) a small system, between 12 MB and 19 MB a medium system, and greater than 19 MB a large system. Each process is guaranteed a minimum working set for performance reasons; about 120K for small systems, 160K for large systems, and 245K for large systems.

When system resources are heavily taxed, a process is confined to its maximum working set. Asynchronous I/Os may never cause you to exceed your maximum working set, because once you are allowed to initiate an asynchronous I/O, the page cannot be taken away if memory becomes tight. The maximum working set sizes are about 300K for a small system, 716K for a medium system, and 1.5 MB for a large system.

MORE INFORMATION

The following code can be used to experiment with the maximum number of bytes that can be written using asynchronous I/O. Simply change the line to vary the number of bytes that the code attempts to write:

   #define NBR_BYTE 700000

Sample Code

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>

#include <windows.h>

#define NBR_BYTE 700000

int main(void)
{
   char        *c;
   HANDLE      hFile;
   DWORD       byteWrite;
   OVERLAPPED  overLap;
   DWORD       err;
   BOOL        result;

   c = malloc( NBR_BYTE );
   assert( c != NULL );

   overLap.hEvent = CreateEvent( NULL, FALSE, FALSE, "event1" );
   assert( overLap.hEvent );

   hFile = CreateFile( "test", GENERIC_WRITE, 0,  NULL, OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED |
                       FILE_FLAG_WRITE_THROUGH,
                       NULL);

   if( hFile == INVALID_HANDLE_VALUE )
   {
      free( c );
      printf( "error opening file\n" );
      exit( 0 );
   }

   overLap.Offset     = 0;
   overLap.OffsetHigh = 0;
   result = WriteFile( hFile, c, NBR_BYTE, &byteWrite, &overLap );
   if( result == FALSE )
   {
      err = GetLastError( );
      if( err != ERROR_IO_PENDING )
      {
         free( c );
         printf( "Error: %d\n", GetLastError() );
         exit( 0 );
      }
   }

   free( c );

   return 0;
}


Additional reference words: 3.10 asynch
KBCategory: kbprg
KBSubcategory: BseFileio


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.