Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 exits the method. Would a timer be a better way of doing this? I'm noticing memory issues sometimes when I run this game.

like image 663
Mike S Avatar asked Aug 16 '11 19:08

Mike S


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

like image 98
cdhowie Avatar answered Oct 28 '22 04:10

cdhowie


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.

like image 35
Reed Copsey Avatar answered Oct 28 '22 03:10

Reed Copsey


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.

like image 33
Johni Avatar answered Oct 28 '22 05:10

Johni


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.
like image 23
Rag Avatar answered Oct 28 '22 04:10

Rag