How to use Rx when "T" is a large, expensive object


  • It seems that the IObservable<T>/IObserver<T> works really well when "T" is small and inexpensive. The producer just creates a new instance each time it has something to send down the chain and shoots it into the pipe. All the observers do their thing and when the last reference goes out of scope the garbage collector just deals with the cleanup.

    What is not clear to me is how this pattern should work when "T" is really big (like arrays of a million elements) and expensive - something you can't afford to "new" and "dispose" for every message. Clearly you need to pass these things by reference, and you may want to create a pool of them, but in either case you have the same problem - how does the producer know when it can reuse one of these elements? The subscription chain may be arbitrary in length and shape, and some subscribers may be observing on different threads so their use of the data may be asynchronous to the producer's call to "OnNext". How do you know when everyone is done using an element? Is there a pattern for this?

    Saturday, June 17, 2017 4:08 PM