应用程序在将纹理保存到 PNG 文件时内存不足 [英] Application runs out of memory while saving textures to PNG files

查看:36
本文介绍了应用程序在将纹理保存到 PNG 文件时内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每帧都在 RenderTarget2D 对象上使用 SaveAsPng().我也在每一帧上处理了 RenderTarget2D 和输入流.然而,就在保存大约 500 张图像之前,我收到一条消息,说应用程序内存不足.

I'm using SaveAsPng() on a RenderTarget2D object every frame. I dispose of both the RenderTarget2D and the input stream on every frame too. Yet, just before about 500 images saved I get a message saying the application ran out of memory.

我在调用 Dispose() 时尝试更改,但每次都得到相同的结果.

I tried changing around when I call Dispose(), but I got the same result every time.

RenderTarget2D rt;

protected override void Update(GameTime gameTime)
    {
    if (rt != null)
        {
        rt.Dispose();
        }

    rt = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);

    GraphicsDevice.SetRenderTarget(rt);
    GraphicsDevice.Clear(Color.Transparent);

    sb.Begin();
    //I draw stuff here.
    sb.End();

    GraphicsDevice.SetRenderTarget(null);
    GraphicsDevice.Clear(Color.Black);

    Stream stream = File.Create(path);
    rt.SaveAsPng(stream, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
    stream.Dispose();
    stream.Close();
    }

我希望这样做是释放与在前一帧上绘制的图片相关联的内存,为新的 RenderTarget2D 分配新的内存空间,因此,只使用与存储单个图像一样多的内存正在画画.出于某种原因,我认为,所有图像都存储在内存中,直到空间用完为止.(错误是由 SaveAsPng 方法调用生成的,根据 VS,内存使用似乎随着运行时间线性上升,应用程序中没有任何内容,除了这个小图像处理位使用大块内存.)

What I expect this to do is release the memory associated with the picture drawn on the previous frame, assign new memory space to the new RenderTarget2D and as such, only use as much memory as to store a single one of the images I'm drawing. For some reason, I think, all the images are stored in memory until space runs out. (The error is generated by the SaveAsPng method call, memory usage seems to go up linearly with run time according to VS and nothing in the application, other than this little image processing bit uses large chunks of memory.)

更新:这是我第一次使用内存快照,所以我一定是看错了,但在我看来,左边的数据似乎没有反映右边的内存使用情况.

Update: This is my first time ever using memory snapshots, so I must be looking at this wrong, but to me it looks like the data on the left doesn't seem to reflect the memory usage graph on the right.

快照对比

推荐答案

我有一些想法.

首先,建议将 using 结构与 File.Create 一起使用,因为这会为您清理(参见此处的示例) 类似:>

First, it's recommended to use the using construct with File.Create, since that does the clean up for you (see the examples here) Something like:

using (Stream stream = File.Create(path)) {
  rt.SaveAsPng(stream, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
}

这将确保正确处理流.

我的下一个想法是 Update() 被非常频繁地调用,所以你可能会让垃圾收集器不堪重负.由于图像会比较大,也许 C# 根本没有及时释放内存.

My next thought is that Update() is called very frequently, so you might be overwhelming the garbage collector. Since the images are going to be relatively large, perhaps C# simply isn't freeing up the memory in time.

也许您应该限制保存图像的速度(可能每 0.5 秒一次).如果您以某种方式泄漏内存或只是让垃圾收集器不堪重负,情况会更加明显.

Maybe you should limit the rate at which images are saved (maybe every 0.5 seconds).Then it'll be more obvious if you're leaking memory somehow or just overwhelming the garbage collector.

希望有所帮助.

这篇关于应用程序在将纹理保存到 PNG 文件时内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆