Git Product home page Git Product logo

Comments (6)

benmwatson avatar benmwatson commented on May 23, 2024

The call to memoryStream.ToArray() is undoing any possible optimizations of using a pooled stream. The memory is probably going up in production because you have so much spare memory that no GC happens.

In your case, you would need to keep the stream alive (undisposed) until after you were done with the TransferFile it was being used in. If TransferFile can only accept a buffer, it should also accept a length so that it knows how much of the buffer is valid.

from microsoft.io.recyclablememorystream.

Sicos1977 avatar Sicos1977 commented on May 23, 2024

The call to memoryStream.ToArray() is undoing any possible optimizations of using a pooled stream. The memory is probably going up in production because you have so much spare memory that no GC happens.

In your case, you would need to keep the stream alive (undisposed) until after you were done with the TransferFile it was being used in. If TransferFile can only accept a buffer, it should also accept a length so that it knows how much of the buffer is valid.

I can't avoid using the ToArray() because it is calling a webservice that only accepts a byte array. Is it in this case better to use a normal MemoryStream instead of a Recyclable one?

from microsoft.io.recyclablememorystream.

benmwatson avatar benmwatson commented on May 23, 2024

from microsoft.io.recyclablememorystream.

Sicos1977 avatar Sicos1977 commented on May 23, 2024

This goes a little bit above my head how all this works, the reason I started with the RecyclableMemoryStream was because I move a lott of data back and forward. I used to have regular out of memory exceptions even on byte arrays smaller than 200MB. These problems were gone when starting to use this library.

Is it possible to use GetBuffer() instead of ToArray() to get the underlying byte array to avoid that a new copy is made in memory? Or is this asking for other problems?

from microsoft.io.recyclablememorystream.

benmwatson avatar benmwatson commented on May 23, 2024

GetBuffer is preferred to ToArray because it will return a reference to the underlying buffer currently in use, but since those buffers are pooled, they will be larger than the amount of data you have stored. If the API you pass the buffer to doesn't understand that, it might assume the whole buffer has valid data, which isn't the case. That's why it's important to also use the length of the stream. ToArray always creates a new array of exactly the right length. Useful for those APIs that need just a buffer, but it destroys the benefits of pooling.

from microsoft.io.recyclablememorystream.

Sicos1977 avatar Sicos1977 commented on May 23, 2024

Thanks for helping me out with these questions, best chrismas wishes and have a good new year.

from microsoft.io.recyclablememorystream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.