简体   繁体   English

处置和存储流

[英]Dispose and memorystream

I have the following code that gets called 4 times a second and updates the background of a grid. 我有以下代码每秒被调用4次并更新网格的背景。 When i don't dispose of of the memory stream, the memory output usage slowly grows and falls. 当我不处理内存流时,内存输出使用量会缓慢增长和下降。 MemoryStream has a Dispose function, But if I call it even after i dispose the Source Bitmap, the background is just white. MemoryStream具有Dispose函数,但是即使我在处置Source Bitmap之后仍调用它,背景也只是白色。

Do i need to dispose the stream? 我需要处置流吗? And if I do, what am I doing wrong? 如果我这样做了,那我做错了什么?

private void Viewer_OnUpdate(object self, Bitmap sourceBitmap, Int32Rect cropArea)
    {
        if (sourceBitmap == null)
        {
            return;
        }

        this.Dispatcher.BeginInvoke(DispatcherPriority.Render,
            new Action(
                () =>
                    {
                        MemoryStream stream = new MemoryStream();
                        sourceBitmap.Save(stream, ImageFormat.Bmp);
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.BeginInit();
                        stream.Seek(0, SeekOrigin.Begin);
                        bitmapImage.StreamSource = stream;
                        bitmapImage.EndInit();
                        this.GridContainer.Background =
                            new ImageBrush(new CroppedBitmap(bitmapImage,cropArea));
                        sourceBitmap.Dispose();
                    }));
    }

Note: i'm dispatching because the calling event is always from a Non-UI thread 注意:我正在调度,因为调用事件总是来自非UI线程

From MSDN documentation for BitmapImage (emphasis is mine): 来自BitmapImage MSDN文档(重点是我的):

Set the CacheOption property to BitmapCacheOption.OnLoad if you wish to close the stream after the BitmapImage is created . 如果要在创建BitmapImage之后关闭流,请将 CacheOption属性设置为BitmapCacheOption.OnLoad。 The default OnDemand cache option retains access to the stream until the bitmap is needed, and cleanup is handled by the garbage collector. 默认的OnDemand缓存选项将保留对流的访问,直到需要位图为止,并且清理由垃圾收集器处理。

First, you discovered that if you dispose the memory stream, the bitmap is affected, which means your ImageBrush seems white. 首先,您发现如果处置内存流,则位图会受到影响,这意味着ImageBrush看起来是白色的。 So - don't dispose the memory stream. 所以-不要丢弃内存流。

Second, and more importantly, the memory consumption pattern you're seeing - more and more memory, then a sudden fall - is normal. 其次,更重要的是,您看到的内存消耗模式-越来越多的内存,然后突然下降-是正常的。 That's what happens when the garbage collector runs at its own discretion. 当垃圾收集器自行运行时,就会发生这种情况。 It's not a problem at all. 根本不是问题。

So don't dispose of the memory stream, let the garbage collector do its job, and don't worry about it. 因此,不要处理内存流,让垃圾收集器完成其工作,也不必担心。

Look, maybe this answer will help you. 你看,也许这个答案会帮助你。

MemoryStream in Using Statement - Do I need to call close() 使用语句中的MemoryStream-是否需要调用close()

If you put your MemoryStream in a using statement, you will not need to Dispose, because it will do it automatically ;) 如果将MemoryStream放在using语句中,则不需要Dispose,因为它会自动执行;)

I hope this helps 我希望这有帮助

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

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