Is it okay not to use a MemoryStream / StringReader?

I would like to create a method that returns an XmlReader. Depending on the circumstances, the XmlReader can be powered by various types of streams, either StringReader or MemoryStream.

Usually I delete a StringReader or a MemoryStream using a block, but since I want to return an XmlReader instead, I cannot do this if I want to go with this design. I do not expect MemoryStream to allocate huge amounts of memory, so I can live with a slight delay in freeing up resources.

Are there any consequences that the GC may use StringReader and MemoryStream in this case?

I must clarify that this is a practical question, not a question with best practice. Obviously, the theory dictates that I have to clean my own resources, but the theory also says that I should prefer the simplest design possible for maximum maintainability. In some cases, a violation of best practice may be justified IMHO, and my question is whether this particular case justifies a violation of best practice.

Also, we are talking only about StringReader and MemoryStream, and not about a common stream or reader. My reason for justifying it in this case is that the actual creation of StringReader / MemoryStream is well encapsulated in the method that XmlReader returns, so it is controlled that XmlReader will not load the stream with a limited resource.

+3
source share
3 answers

In this case, nothing will suffer, but IMO is still a very bad practice. You own them - why not do it right? In fact, removing a MemoryStream still cannot free, etc. - it is still associated with the GC. But somewhere there is some kind of code smell. This can become real problems if something changes, and suddenly it is not a MemoryStream , but something else, etc.

We cannot make you dispose of it, but personally: I try about my using s

+9
source

Answer: NO . You should always have disposable resources. If the stream returns from the method, deletion must be performed by the caller.

+2
source

If you allocate a resource for the explicit internal use of your class, then your class is responsible for managing this resource. If you assign a resource on behalf of the caller, then he is responsible for managing the lifetime of the requested resources.

While the CLR ultimately frees up resources allocated by any object, no attributes are allocated when a specific object is collected (freed).

Therefore, if an object uses a relatively scarce resource, such as a file descriptor, and this object is not deleted by the code that created it, the file descriptor will remain inaccessible for use by the system or other applications until the garbage collector collects the object holding the handle.

On one computer desktp [machine], it is unlikely that you started the file descriptors, but on a busy server it is more likely that you will approach the maximum number of file descriptors available, and the closer the system resource is exhausted, the more likely the macho will experience performance degradation, therefore, timely release of resources is becoming a much more serious problem.

+1
source

Source: https://habr.com/ru/post/1446574/


All Articles