定时器与 While 循环 - 内存使用

发布于 2024-11-30 01:35:44 字数 169 浏览 1 评论 0原文

我正在用 C# 编写一种游戏,我想知道什么是更好的内存使用方法。现在我有了它,所以它会经历一个 while 循环,当游戏运行时它会检查某些事情(这个人是否死了等),休眠一秒钟,然后重试。游戏结束后, while 被设置为 false 并退出该方法。计时器是执行此操作的更好方法吗?当我运行这个游戏时,有时我会注意到内存问题。

I am programming a type of game in C# and I want to know what would be a better approach for memory usage. Right now I have it so it goes through a while loop and while the game is running it will check for certain things (is this person dead, etc), sleep for one second, then try again. Once the game is over the while is set to false and it exits the method. Would a timer be a better way of doing this? I'm noticing memory issues sometimes when I run this game.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧时浪漫 2024-12-07 01:35:44

while 循环方法可能不是您的问题。 Timer 实际上比这种方法重一点,并且会消耗更多的系统资源,但这将是一次性分配。换句话说,将主循环代码从一种结构更改为另一种结构不太可能显着改变应用程序的内存配置文件。

您是否尝试过分析应用程序的内存使用情况?您可能会保留不再需要的物品。探查器也许能够帮助您确定位置。

(如果您好奇的话,使用 Timer 可能会对性能产生非常小的影响,因为每次迭代都会导致函数指针调用,而 CPU 和 JIT 编译器无法很好地优化该函数指针。 while 循环意味着您的代码始终在运行;考虑到 Thread.Sleep() 调用不是通过函数指针实现的,因此它可以得到很好的优化。 >Timer 方法也可能导致可读性较差的代码,我强烈建议您继续使用 while 循环。)

The while loop approach is probably not your problem. Timer is in fact a bit heavier than this approach, and will consume more system resources, but this will be a one-time allocation. In other words, changing your main loop code from one structure to the other is unlikely to change your application's memory profile significantly.

Have you tried profiling your application's memory usage? You're likely holding on to objects that you don't need anymore. A profiler might be able to help you determine where.

(In case you are curious, using a Timer will likely have a very small performance impact, since each iteration will cause a function pointer call, which the CPU and JIT-compiler cannot optimize very well. Using a while loop means that your code is always running; the Thread.Sleep() call can be optimized fairly well since it is not through a function pointer. Considering that the Timer approach is also likely to result in less readable code, I would strongly suggest that you continue using a while loop.)

堇色安年 2024-12-07 01:35:44

将其更改为计时器与睡眠将不会(实际上)对内存使用产生影响。主要区别在于应用程序运行的逻辑 - 使用计时器,您不会在 while 循环中执行顺序过程,而是定期发生一系列“事件”。

Changing this to a timer vs. a sleep will have (effectively) no impact on memory usage. The main difference will be in the logic of how your application runs - with a timer, you won't have a sequential process in a while loop, but rather a series of "events" that occur on a regular interval.

说谎友 2024-12-07 01:35:44

我认为 while 循环是完成此类任务的最佳方法(也许与线程结合使用)。计时器主要用于更长且更精确的时间段。

I think a while loop is the best approach for such a task (maybe in combination with a thread). A timer is mainly for longer and exact time periods.

塔塔猫 2024-12-07 01:35:44

是的,您应该在主游戏循环中使用 while 循环,但您不应该休眠。相反,您应该在上次绘制帧时添加时间戳,并且每次执行循环时检查是否已经过了 1/30 秒或其他时间。如果没有,则一遍又一遍地忙循环,直到它过去。

你的每秒“人员死亡”之类的事情(尽管在我看来这是一个坏例子)可以简单地使用不同的时间戳来完成,并通过检查来查看自上次“人员死亡”代码以来是否已经过了 1 秒跑步。除非您有非常令人信服的理由在计时器线程中执行该处理,否则最好同步执行。

您的内存问题很大程度上与使用 while 循环无关。确保您没有重复地将项目添加到列表或其他内容中,并确保所有资源初始化都是使用 using 构造完成的:

using (StreamReader sr = File.OpenText(path))
{
        // blah blah using sr
}

// The streamreader has been automatically disposed by this point.

Yes, you should use a while loop for your main game loop, but you shouldn't Sleep. Instead, you should timestamp the last time you drew a frame, and every time you go through the loop check if 1/30th of a second or whatever has passed. If not, busy-loop around over and over until it has passed.

Your every-second "person dead" kind of thing (even though that's a bad example IMO) can simply be done with a different time stamp, guarded by a check to see if it's been 1 second since the "person dead" code was last run. Unless you have some extremely compelling reason to do that processing in a timer thread it's best to do it synchronously.

Your memory problems are largely unrelated to using a while loop. Make sure you're not repeatedly adding items to a list or something, and make sure that any resource initialization is done using a using construct:

using (StreamReader sr = File.OpenText(path))
{
        // blah blah using sr
}

// The streamreader has been automatically disposed by this point.
忆离笙 2024-12-07 01:35:44

不完全是一个答案,而是一个替代方案。我认为事件驱动的方法比任何重复检查更好(无论您如何实现重复检查)。当一段代码杀死玩家时,它还应该调用其他代码可以挂钩的事件。

至于你的内存问题,没有任何来源,我只能建议你分析你的代码,看看内存是什么。

Not exactly an answer, but an alternative. I think an event driven approach would be better than any reoccurring check (regardless how you implement the reoccurring check). When a section of code kills the player, it should also call an event that other code can hook into.

As for your memory problem, without any source I can only recommend you profile your code to see what all that memory is.

久隐师 2024-12-07 01:35:44

你的 while 循环是不错的选择。游戏循环是构建游戏的最常见方法。

您可以查看这篇文章来了解不同类型的游戏循环。

如果您正在学习,您可能还想使用 xna 作为游戏的基础。游戏循环已经为您实现了。

无论如何,它不应该是你记忆问题的原因。

Your while loop is the good choice. Game loop is the most common way to build up a game.

You can check this article to have an overvue of the different kinds of game loops.

You may also want to use xna as a base for your games if you are learning. The game loop is already implemented for you.

In any case, it shouldnt be the cause of your memory problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文