Disposed and eligible for garbage collection mean two different things. In particular, your stream
is eligible for garbage collection not because it has been disposed, but because you have no outstanding references to that object at the point you call GC.Collect
.
But, as @alexeilevenkov points out, the Debug version of the GC is not as aggressive during the Mark phase, holding references alive while a reference is within function scope (allowing you to inspect the stream reference until the function ends). Eligible for garbage collection means that, the object may get garbage collected. However, it's up to the GC to decide when things are actally collected.
In fact, in release mode, it's possible for an object to become eligible for collection and be collected even though a variable referring to the object is still in scope. If an object is created, assigned to a variable, and used near the top of a scope, but never used again in that scope, it's possible that the object gets collected before the variable goes out of scope.
Other than the rule that an object can't be collected until after the last active reference to that object finishes with it, you shouldn't make any assumptions about when a collection occurs.
Why do you think you have memory leaks in your code? Detecting true memory leaks in managed code is difficult