是否使用委托创建垃圾 [英] Does using a delegate create garbage

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

问题描述

我工作的XBOX360游戏,使用XNA。在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!

推荐答案

一个委托本身就是一个对象,因此,如果您创建一个委托,也许是一个匿名方法,并把这个给一些其他的方法来执行,并且不保存以供将来委托。参考,那么,的的会产生垃圾

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.

然而,呼叫的代表本身并不不产生垃圾,任何更比调用将相同类型的任何其它方法。举例来说,如果你调用一个委托,它需要一个对象参数,传入一个的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是在这里:的托管在Xbox 360上的XNA代码性能:第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天全站免登陆