I understand it is required. I was listing the large objects as an
"Helge Jensen" <helge.jensen@slog.dk> wrote in message
news:eMtqaWQaGHA.3444@TK2MSFTNGP05.phx.gbl...
>
>
> Greg Young [MVP] wrote:
>> One thing that has yet to be mentioned as a reason why its better to use
>> the
>> looping methodology is large objects ...
>
> The looping strategy is required for semantic reasons. A stream may
> return less bytes than requested, even if the stream is not closed yet.
>
> invoking s.Read(buf, 0, bug.Length) may return at any time there is any
> data available, or the stream is closed.
>
> In the real world not-looping will often break with for example
> network-streams, where data arrive "slowly".
>
>> if you use the simple read/write methodology you end up with a byte array
>> the size of the data being created. If this byte size is over a certain
>> limit (I forget what the exact number is off the top of my head but want
>> to
>> say 85k ??? someone feel free to correct me) it is treated as a large
>> object
>> and put on a special heap. Large objects have differing life cycles and
>> are
>> not garbage collected in the same way as normal objects .. this can lead
>> to
>> a possible memory usage attack by someone forcing this operation to
>> happen
>
> Creating a large amount of large objects is also not good, but it's not
> semanticly required.
>
>> repeatedly ... I have never tried to take an app down in this way but I
>> would imagine that it is feasible.
>
> That can most certainly be done :) I have seen code (in effect) like:
>
> while ( true ) {
> using ( MemoryStream s = new MemoryStream() )
> using ( Stream connection = awaitConnectionAndGetStream() ) {
> // workaround Deserialize invoke s.Close()
> Util.StreamCopy(connection, s);
> try {
> object o = formatter.Deserialize(s);
> }
> /// process and send reply
> }
> }
>
> Conversing with this server for any significant amount of time,
> transmitting a few objects of 85k the .NET runtime would break down with
> an out-of-memory error.
>
> If you expect (or risk receiving) large amounts of input, you should
> process the data from that stream in a streaming manner, that is -- a
> "little" at a time.
>
> In the end i rewrote the above-code to use pass a proxy for the
> connection to Deserialize. That proxy workarounds the Close, and some
> other issues and it allows the caller to limit the amount of data that
> can be read from the proxy.
>
> --
> Helge Jensen
> mailto:helge.jensen@slog.dk
> sip:helge.jensen@slog.dk
> -=> Sebastian cover-music:
http://ungdomshus.nu <=-