ElektroStudios
ElektroStudios

Reputation: 20474

Which is the official buffer size that Microsoft uses to copy files?

I would like to know which is the proper buffer size value to use in a .Net splitter/merger application that I'm developing, because to improve some FileStream I will set the same buffer size as Microsoft internally uses for copy operations in Windows.

I know this information exists and it is public because I seen this info in the past somewhere over Internet (maybe on MSDN) but I lost any references, if I remember good Microsoft uses 8 or 16 kb for buffer size, in any case is less than 1 MB for sure.

I'm looking for an official reference where Microsoft allegates the value they uses, or else some kind of technically demonstrable answer (maybe using Reflection?), please avoid answers like "I think they use X kb..." because is not demonstrable.

Upvotes: 0

Views: 509

Answers (1)

Alex
Alex

Reputation: 13234

It is easy to check the default buffer sizes used by specific stream implementations, by checking the implementation in the reference source.

The default buffer size for e.g. a FileStream is 4096 bytes. The default buffer size for Stream.Copy implementations is 80 Kb.

A good practice when choosing a buffer size yourself is:

  • Make it a multiple of the OS page size (generally 4096 bytes)
  • If you have files / streams of a substantial size, make the buffer larger than a single OS page.
  • Do performance tests to find a good size if this is a (potential) performance bottleneck.
  • What is optimal on System A, may not be on System B with a different hardware/software configuration.

Upvotes: 4

Related Questions