开发者

Timer vs While Loop - Memory Usages

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 exit开发者_如何学JAVAs the method. Would a timer be a better way of doing this? I'm noticing memory issues sometimes when I run this game.


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.)


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜