Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why will there be a memoryleak if I don't dispose the stream? #365

Open
szguoxz opened this issue Dec 21, 2024 · 3 comments
Open

Why will there be a memoryleak if I don't dispose the stream? #365

szguoxz opened this issue Dec 21, 2024 · 3 comments

Comments

@szguoxz
Copy link

szguoxz commented Dec 21, 2024

I don't quite understand. I thought the blocks are Garbage Collected if I do not dispose the stream. Why is there a memory leak?

Basically I am having trouble under this paragraph in the readme doc on this project:

If you forget to call a stream's Dispose method, this could cause a memory leak. To help you prevent this, each stream has a finalizer that will be called by the CLR once there are no more references to the stream. This finalizer will raise an event or log a message about the leaked stream.

So what the heck is it talking about? There will be a memory leak? Or could cause a memory leak depend on my Luck?

@udlose
Copy link

udlose commented Dec 21, 2024

I posted this answer on SO some years ago. It should help explain:

What happens if I don't call Dispose on the pen object

@szguoxz
Copy link
Author

szguoxz commented Dec 21, 2024

I know what will happen if you don't call dispose, that's why I am having this question.
Because in the document of README of this project, it said, if you do not dispose the stream, you will get memoryleak. I wonder if the DOC is wrong here. I thought eventually it will be garbage colelcted, thus there will be no memoryleak.

@sgorozco
Copy link

sgorozco commented Dec 23, 2024

You have a case here; the documentation wording might be a bit clearer - basically the documentation is stating that there exists a safety-net finalizer that will prevent a memory leak in case you forget to dispose the stream. The documentation also states that you should treat forgetting to dispose the stream as a problem, that is why the library gives you a lot of support to track undisposed instances. The problem with finalizers is that you do not know when they will eventually run, so you might end up pooling up more resources than strictly required (imagine having 50+ free buffers waiting to be finalized and returned to the pool and still having to allocate extra buffers because the pool has been drained, then eventually the finalizers run and now you end up with a surplus of pooled buffers). Whether this is a serious problem, I guess it will depend on the use-case, the amount of free memory, and your own idiosyncrasies. The library will still greatly reduce strain on the garbage collector, but will not perform optimally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants