Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the max length of a buffer Stream.Read and Stream.Write can handle?

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,

Here's the code (not mine, vendor's, FYI):
Code:
private void toStm(Stream input, Stream output)
{  byte[] buffer = new byte[16 * 1024];
   int read;

   while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
      {  output.Write(buffer, 0, read); }

   output.Seek(0, System.IO.SeekOrigin.Begin);
}

As you can see, the buffer there is 16 Kb.
I checked the possible max length of that buffer, Stream.Length, and there's no definite number (However, judging by the data type - Int64, and assuming it's 32 bit byte, after obvious calculations (2^64 / 32 - 1) we arrive to the number ~576 PiB ("petabyte" or "pebibyte", ... which denies any comprehension, let alone credibility.
Hence the question in subject.

AHWBGA!

Regards,

Ilya
 
There is no max as such: it is whatever your system can take. Say you have a 4Gb system. Half of it is used for the OS and services. That leaves you 2Gb. Say all the other code and heap allocation takes up 1.5Gb. Effectively, you have 0.5Gb to play with. If you go over that, it may or may not work. The OS may swap stuff into the swap space and give you more memory to play with. This varies from system to system. If you run as a 32 bit program on a 64-bit system, you can't go over (4Gb - other code - stack).

Basically, it isn't worth worrying about until your program crashes with an out of memory error. Even something as big as Visual Studio doesn't use very much memory.
 
Got it, thank you!

Any idea what might be the compelling reason for that vendor's programmer to have buffer sized only to 16 KB?

Regards,

Ilya
 
No reason whatsoever - 16 is a nice round binary number. Psychologically, any factor of two (eg 8, 16, 128) or factor of two minus one (eg 15, 31, 63) makes the reader think the writer knows what they are doing. They could have chosen 10 or 99: it wouldn't have made any difference.
 
So I thought.
Some programming systems have/had this 16 KB limit on the length of the String memvar (VFP for sure, probably dBase as well, maybe (but not sure about) VB6...)
Thank you, xwb!
Case closed.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top