使用委托会产生垃圾吗 [英] Does using a delegate create garbage

查看:28
本文介绍了使用委托会产生垃圾吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XNA 为 xbox360 开发游戏.Xbox 上的垃圾收集器与 PC 上的垃圾收集器相比性能相当差,因此将产生的垃圾保持在最低限度对于流畅运行的游戏至关重要.

I'm working on a game for the xbox360, using XNA. On the Xbox the garbage collector performs rather badly compared to the one on a PC, so keeping garbage generated to a minimum is vital for a smoothly performing game.

我记得曾经读到过调用委托会产生垃圾,但现在我一生都找不到任何对委托产生垃圾的引用.是我胡编乱造的还是代表乱七八糟的?

I remember reading once that calling a delegate creates garbage, but now for the life of me can't find any references to delegates creating garbage. Did I just make this up or are delegates messy?

如果代表搞砸了,建议解决方法的奖励积分.

If delegates are messy, bonus points for suggesting a workaround.

public delegate T GetValue<T>(T value, T[] args);

public static T Transaction<T>(GetValue<T> calculate, ref T value, params T[] args) where T : class
{
    T newValue = calculate(value, args);
    return foo(newValue);
}

目前我的代码看起来很模糊,我能想到的摆脱委托的唯一解决方案是传入一个继承接口 IValueCalculator 的类,然后我可以调用该接口上的方法,那不是不过真的很整洁!

My code looks vaguely like that at the moment, the only solution I can think of to rid myself of delegates is to pass in a class which inherits an interface IValueCalculator, and then I can call the method on that interface, that's not really very neat though!

推荐答案

委托本身就是一个对象,所以如果你创建一个委托,也许是为了一个匿名方法,然后把它交给其他方法来执行,不要't 存储委托以供将来参考,那么是的, 将产生垃圾.

A delegate is itself an object, so if you create a delegate, perhaps for an anonymous method, and give this to some other method to execute, and don't store the delegate for future reference, then yes, that will produce garbage.

例如,这个:

collection.ForEach(delegate(T item)
{
    // do something with item
});

在这种情况下,会创建一个新的委托对象,但除了对 ForEach 的调用之外,它没有被引用,因此可以进行垃圾回收.

In this case, a new delegate object is created, but beyond the call to ForEach it is not referenced, and thus eligible for garbage collection.

但是,调用委托本身不会产生垃圾,这与调用任何其他相同类型的方法一样.例如,如果您调用一个接受 Object 参数的委托,并传入一个 Int32 值,则该值将被装箱,但如果您调用普通方法,则会发生这种情况同样的方法.

However, calling delegates does by itself not produce garbage, any more so than calling any other method of the same type would. For instance, if you call a delegate that takes an Object parameter, passing in an Int32 value, this value will be boxed, but that would happen if you called a normal method the same way as well.

所以使用委托应该没问题,但是过多创建委托对象会是个问题.

So using delegates should be fine, but excessive creation of delegate objects will be a problem.

编辑:关于 Xbox 和 XNA 内存管理的好文章在这里:XNA 的 Xbox 360 上的托管代码性能:第 2 部分 - GC 和工具.注意这句话:

Edit: A good article on memory management for Xbox and XNA is here: Managed Code Performance on Xbox 360 for XNA: Part 2 - GC and Tools. Pay attention to this quote:

那么如何控制 GC 延迟?与设备的 NetCF 一样,Xbox GC 是非分代的.这意味着每个集合都是托管堆上的完整集合.因此,我们发现 GC 延迟与活动对象的数量大致呈线性关系……然后再加上堆压缩的成本.我们的基准测试表明,深层对象层次结构与浅层对象层次结构之间的差异可以忽略不计,因此重要的是对象的数量.小物体也往往比大物体更便宜.

So how does one control GC latency? Like NetCF for devices, the Xbox GC is non-generational. That means every collection is a full collection on the managed heap. Thus, we find that GC latency is approximately linear to the number of live objects… then add the cost of heap compaction on to that. Our benchmarks show that the difference between deep object hierarchies vs. shallow ones is negligible, so it’s mostly the number of objects that matter. Small objects also tend to be a somewhat cheaper to deal with than big objects.

如您所见,尽量避免创建大量不必要的对象,您应该会做得更好.

As you can see, try to avoid creating lots of unnecessary objects, and you should fare better.

这篇关于使用委托会产生垃圾吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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