简体   繁体   English

如何释放 C# 中的 MemoryStream?

[英]How can I free up MemoryStream in C#?

I'm a bit of a noob with C#.我对 C# 有点陌生。 I've tried to do the right thing in terms of managing memory, but I am now getting "out of memory" errors.我已经尝试在管理 memory 方面做正确的事情,但我现在遇到了“内存不足”错误。

UPDATE #2 5 DEC更新 #2 12 月 5 日

I solved it, (sort of) I don't understand why, but when I just run the code from my test app with hard-coded parameters, it worked fine, but when I use a dialog (based on OokiDialog) to get some parameters.我解决了它,(有点)我不明白为什么,但是当我使用硬编码参数从我的测试应用程序运行代码时,它工作得很好,但是当我使用对话框(基于 OokiDialog)来获取一些参数。 that's when the memory doesn't get free up.那是 memory 没有释放的时候。

So, there's something about the dialog that seems to make it want to hold onto these objects, even though they have nothing to do with the dialog itself.所以,对话框似乎有一些东西让它想要抓住这些对象,即使它们与对话框本身无关。

In the end, I was able to "fix" the problem by running the code in it's own task:最后,我能够通过在它自己的任务中运行代码来“修复”问题:

Task taskA = Task.Run(() => MakeFiles(path, name));
taskA.Wait();

It gets me working again, but still, I'd LOVE some theories on WHY this is so?它让我再次工作,但我仍然喜欢一些关于为什么会这样的理论?

UPDATE 5 DEC 12 月 5 日更新

After continued struggles, I decided to copy the relevant code to a test project where I could experiment.经过不断的挣扎,我决定将相关代码复制到一个可以进行实验的测试项目中。 After copying, I ran it, and was stunned to see that the memory went up and then DOWN on each iteration.复制后,我运行它,惊讶地发现 memory 在每次迭代时先升后降。 No more "out of memory"!没有更多的“内存不足”!

Still trying to figure out how/why.仍在试图弄清楚如何/为什么。

UPDATE 4 DEC更新 12 月 4 日

I've ruled out the GIF encoder.我已经排除了 GIF 编码器。 I commented out that part of the code and the memory still went up and up.我注释掉了那部分代码,memory 还是涨涨涨涨。

I'm doing quite a bit of loading images and copying their pixels to byte[] arrays.我正在做很多加载图像并将它们的像素复制到字节[] arrays。 I've been pretty careful to use local variables for the most part, so they shouldn't be persisting.在大多数情况下,我一直非常小心地使用局部变量,所以它们不应该持续存在。

Is there any way to tell what object or at least class of object is creating those "StreamAsIStream [Strong Handle]" references?有什么方法可以告诉 object 的 object 或至少 class 正在创建那些“StreamAsIStream [强句柄]”引用吗? ORIGINAL QUESTION原始问题

I've done some profiling of the heap and can see that the main culprit is MemoryStreams.我已经对堆进行了一些分析,可以看到主要的罪魁祸首是 MemoryStreams。

I use MemoryStream in a couple of places, but have been careful to always use "using":我在几个地方使用 MemoryStream,但一直小心使用“使用”:

using (MemoryStream mem = new MemoryStream())
{
    ...
}

I noticed that most of the streams on the heap end up showing as:我注意到堆上的大多数流最终显示为:

StreamAsIStream [Strong Handle] StreamAsIStream [强句柄]

That "strong handle" seems like a clue, perhaps?那个“强大的手柄”似乎是一个线索,也许? Ie because it's a strong handle the GC won't clean it up?即因为它是一个强大的句柄,GC 不会清理它?

I've tried explicitly disposing the streams and then forcing garbage collections, but no matter what I do, they just seem to stick around and eventually I get memory errors.我已经尝试明确处理流,然后强制垃圾 collections,但无论我做什么,他们似乎只是坚持下去,最终我得到 memory 错误。

Could it be something else is internally using a MemoryStream and I'm looking in the wrong place?可能是其他东西在内部使用 MemoryStream 并且我在错误的地方寻找吗?

Any thoughts or other things I can look at?有什么想法或其他我可以看的吗?

You can call stream.Close();您可以调用stream.Close(); to free up the memory used by the object.释放 object 使用的 memory。 The using statement should handle it for you, but I have seen the occasional instance where garbage collection doesn't recognize that the object is no longer in use, especially if you happen to be using WPF. using 语句应该为您处理它,但我看到偶尔垃圾收集无法识别 object 不再使用的情况,特别是如果您碰巧使用 WPF。

You could also try implementing the IDisposable interface and then force disposal of objects as well as force garbage collection.您还可以尝试实现IDisposable接口,然后强制处理对象以及强制垃圾回收。 I've linked Microsoft doc for that process here .我已在此处为该过程链接了 Microsoft 文档。

Here is a more detailed description of Dispose() vs Close() as well. Dispose()Close()的更详细描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM