它是没有效率的高度频繁地创建一类短命的新实例? [英] Is it inefficient to highly frequently create short-lived new instances of a class?

查看:136
本文介绍了它是没有效率的高度频繁地创建一类短命的新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有跟踪游戏玩家的位置,C#程序。在这个节目,我有一个名为航点(X,Y,Z)类,它代表了游戏地图上的位置。在我派生一个线程,我把从某个目标航点检查玩家的距离,之后相当迅速,而对方(真)循环。在航点类都呼吁公众双距离(航点WP),即计算从当前航点的距离,作为参数传递的航点的方法。

I have a C# program that tracks a player's position in a game. In this program I have a class called Waypoint (X, Y, Z), which represents a location on the game map. In one of the threads I spawn, I keep checking the player's distance from a certain target Waypoint, quite rapidly after each other in while(true) loops. In the Waypoint class have a method called public double Distance(Waypoint wp), that calculates the distance from the current waypoint, to the waypoint passed as a parameter.

问:什么情况下可以创造玩家的位置我要检查目标航点从玩家的距离,每次一个新的航点,?该方案随后将可能在一段时间(true)循环,一遍又一遍地创建这个球员航点,只是为了计算距离的目的。

Question: Is it okay to create a new Waypoint for the player's position, every time I want to check the distance from player to the target waypoint? The program would then potentially, in a while(true) loop, create this player Waypoint over and over again, just for the purpose of calculating the distance.

PS:我的程序可能需要巧妙地利用资源,因为它是运行多个线程连续while循环做各种工作,如张贴玩家的X,Y,Z位置到用户界面。

PS: My program probably needs to smartly use resources, as it is running multiple threads with continuous while loops doing various work such as posting the player's X,Y,Z location to the UI.

非常感谢!

推荐答案

对象与一个寿命很短的实际创建是微不足道的。创建一个新对象非常简单,只是涉及由物体的尺寸递增堆指针和置零出这些比特。这不会成为一个问题。

The actual creation of an object with a very short lifetime is minuscule. Creating a new object pretty much just involves incrementing the heap pointer by the size of the object and zeroing out those bits. This isn't going to be a problem.

至于那些对象的实际集合,当垃圾收集执行收集它走所有的对象<青霉>还活着的和复制它们。任何对象不是活着不是由GC感动在这里,所以他们不增加工作的集合。如果您打造永不,还是非常非常罕见的对象,是GC然后收集他们不加入任何费用中有活着。

As for the actual collection of those objects, when the garbage collector performs a collection it is taking all of the objects still alive and copying them. Any objects not "alive" are not touched by the GC here, so they aren't adding work to collections. If the objects you create never, or very very rarely, are alive during a GC collection then they're not adding any costs there.

有一件事,他们可以做是减少目前可用的存储器,使得由GC需要明显更经常比否则将执行集合的量。当它需要更多的内存GC将实际执行的集合。如果找你经常使用所有可用内存创建这些短命的对象,那么你可以增加你的程序收缴率。

The one thing that they can do, is decrease the amount of currently available memory such that the GC needs to perform collections noticeably more often than it otherwise would. The GC will actually perform a collection when it needs some more memory. If you'r constantly using all of your available memory creating these short lived objects, then you could increase the rate of collections in your program.

当然,它会采取一个的很多的对象实际上有藏品多长时间进行一次有意义的影响。如果您担心,你应该花一些时间测量多久你的程序是有和无的代码块进行收藏,看看它是有什么样的影响。如果你真的是造成集发生更加频繁地比,否则他们会和你发现性能问题,结果,然后考虑尝试解决这一问题。

Of course, it would take a lot of objects to actually have a meaningful impact on how often collections take place. If you're concerned you should spend some time measuring how often your program is performing collections with and without this block of code, to see what effect it is having. If you really are causing collections to happen much more frequently than they otherwise would, and you're noticing performance problems as a result, then consider trying to address the problem.

有两个途径浮现在脑海中为解决这个问题,如果你发现了藏品的数量衡量的增加。你可以使用调查值类型,而不是引用类型的可能性。这可能会或可能没有意义概念在上下文你,它可以或可以不实际帮助的问题。它会依赖于没有提到具体的方式太多,但它至少东西看看。另一种可能性是试图积极缓存对象,使它们可被重新使用的一段时间内。这也需要仔细加以研究,因为它可以大大提高程序的复杂性,并使其更难写的是正确的,可维护,易推理程序,但它的可以的是一个有效的工具,如果正确使用重新引入内存。

There are two avenues that come to mind for resolving this problem, if you have found a measurable increase in the number of collections. You could investigate the possibility of using value types instead of reference types. This may or may not make sense conceptually in context for you, and it may or may not actually help the problem. It'd depend way too much on specifics not mentioned, but it's at least something to look into. The other possibility is trying to aggressively cache the objects so that they can be re-used over time. This also needs to be looked at carefully, because it can greatly increase the complexity of a program and make it much harder to write programs that are correct, maintainable, and easy to reason about, but it can be an effective tool for reintroducing memory if used correctly.

这篇关于它是没有效率的高度频繁地创建一类短命的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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