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.
-
4I'm not sure why timing has anything to do with memory usage. – Joe Aug 16 '11 at 19:23
-
Ok, maybe a better word is performance. I just want to know what would be the better approach at this scenario. – Mike S Aug 16 '11 at 19:26
-
1What kinds of memory issues? .NET will consume memory naturally, as leaving memory filled but unused is not much different than clearing it out, from a managed memory standpoint. In other words, if the memory size is the issue, it is probably not an issue. If you are crashing the machine, then that IS a problem. – Gregory A Beamer Aug 16 '11 at 19:28
-
Well I am modifying a Minecraft Server and I notice that once I run these custom games over the next few days memory will start to skyrocket until a point where the program crashes from an OutOfMemory Exception. – Mike S Aug 16 '11 at 19:32
6 Answers
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.)

- 158,093
- 24
- 286
- 300
-
There's a few listed on [this question](http://stackoverflow.com/questions/399847/net-memory-profiling-tools). – cdhowie Aug 16 '11 at 19:35
-
1Wow, thank you guys. I didn't expect so many answers so fast. People actually care here. I think I'll go with the while loop and look for a profiler to see what is really going on with my program. Also the event-driven idea might work nicely too. – Mike S Aug 16 '11 at 19:45
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.

- 554,122
- 78
- 1,158
- 1,373
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.

- 2,933
- 4
- 28
- 47
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.

- 6,405
- 2
- 31
- 38
-
1Busy-looping (AKA "spin waiting") is a bad idea, as that disallows resource-conservation mechanisms on the CPU since it is never idle. The game would be using 100% CPU constantly. It's better to at least yield on each iteration (sleep for a small period of time) to conserve power. – cdhowie Aug 16 '11 at 19:31
-
@cdhowie If ever there's a time when you *don't* want background services interrupting you it's when drawing realtime graphics for a game. The operating system can preempt your process when it really needs that processor. Though I guess I should specify that if we're talking about, say, a chess game then he should use an event based model. I can't think of a case when you'd want to be tightly looping with a while loop, but giving up processor time every loop to anyone who wants it. – Rag Aug 16 '11 at 19:45
-
You can, of course, use an above-normal thread priority for the main loop. But presumably, he is sleeping in between drawing frames. That's a perfectly reasonable place to sleep. It would be better to making a blocking call to the graphics card that returns when the next buffer swap completes, as that would let the CPU rest as well. My main point is that using 100% of the CPU preempts any sort of CPU power-saving features... and spin-waiting is just bad design in general. – cdhowie Aug 16 '11 at 20:08
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.

- 24,072
- 31
- 113
- 188
-
I like this idea. It seems to me it might cause more coupling than needed, but it could work very nicely and be less error-prone. I might try this too. – Mike S Aug 16 '11 at 19:36
-
More coupled than a loop embedded in a class running the full length of the game? – Corey Ogburn Aug 16 '11 at 19:49
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.

- 4,605
- 4
- 40
- 60