Take the 2-minute tour ×

ok well i have a simple game that uses really high of memory and cpu. cpu goes over 44% and memory goes over 5000. here is my code


how to fix this?

EDIT memory: 5000 bytes.
cpu: 44% on i5
the program get slower by the time it runs.

share|improve this question
1) Indent your code. 2) See the help on getting it to format when you post to Stack Overflow. 3) Narrow your code down to the problem section or pastebin it somewhere if absolutely need be, but don't dump a whole unformatted problem on SO. If you want us to help you, make it easy for us to help you. –  Thanatos Aug 17 '10 at 19:23
formatted (what a mess...) –  Federico Culloca Aug 17 '10 at 19:24
lol@frames inside frames inside frames –  Falmarri Aug 17 '10 at 19:25
This code kinda reminds me of the movie inception. One piece frame inside other... –  sabertooth Aug 17 '10 at 19:25
"and memory goes over 5000" <- Can we have a unit here? 5000 bytes? (Wow!) 5000 KiB? (not so much) 5000 MiB? (Yikes!) –  Thanatos Aug 17 '10 at 19:28
show 8 more comments

3 Answers

up vote 1 down vote accepted

The best way to tackle something like this is to comment-out big chunks of code until you stop seeing runaway CPU/memory, and then gradually uncomment those chunks until you identify the problem.

After a quick scan of the code, I do wonder why you are starting eleven timers to update your game objects. It would be better to have a single timer that updates everything at once and then does a single Invalidate call.

share|improve this answer
You solved my cpu problem thanks –  Ramilol Aug 17 '10 at 20:19
add comment

That's too much code to examine thoroughly.

Some general tips though: Evaluate how much memory you expect it to use. Is memory use growing as it runs or does it stop growing at some point? If it continually grows you probably have a leak. There are packages out there that can help you track down where it's leaking, or make sure that you use RAII (for example shared_ptr) to manage your memory. If memory holds steady at a large number you may want to revisit your algorithm and see where the memory is being used. Are you allocating a lot of duplicate data?

As for CPU use, the only way to figure out where the time is going is to profile your application and see where the profiler says the CPU is being spent. Then you can approach that smaller section of code and determine how to improve it. The most likely improvements are finding polynomial (or worse) time algorithms and making them sub-polynomial time.

share|improve this answer
well the memory won't stop growing so as the cpu –  Ramilol Aug 17 '10 at 19:35
@Ramiz use oprofile + valgrind –  Anycorn Aug 17 '10 at 19:37
where can i get them –  Ramilol Aug 17 '10 at 19:42
+1 for profiling. –  Brian Aug 17 '10 at 20:02
@Ramiz: Just google for C++ profiler and pick the one that is most appropriate (i.e. will work on the setup you are testing on). –  Brian Aug 17 '10 at 20:03
show 3 more comments

Your high cpu load may come from your main loop.

I usually use something like

while(gameIsRunning) //Set this to false when the WM_QUIT message arrives
  //handle all messages, if any
  while (PeekMessage(&msg, hwnd,  0, 0, PM_REMOVE)) 

  //addition per loop code


Additionally, you should abandon timers and look into QueryPerformanceTimer and QueryPerformanceFrequency for timing and measuring.

share|improve this answer
add comment

Your Answer


