High CPU utilization

High CPU utilization
0.0 0


I am experiencing constant very high CPU utilization with the framework, even when there are 1 layer and 1 sprite. This behavior can also be seen with the tests provided by the framework (just by initiating the application and standing in the mainmenu). I am seeing it consistently on two different machines. The way I currently workaround it is by setting the process affinity to one CPU. It consumes 100% of the allocated CPU and leaves the other one for other processes on the machnies.
Is this a known problem? Is there a better workaround or configuration that I might need to do to prevent it?



Modify the CCApplicaiton_win32.cpp:Line70, function : CCApplicaiton::run(): replace Sleep(0) with a lager number for reduce cpu

    while (1)
        if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            // Get current time tick.

            // If it's the time to draw next frame, draw it, else sleep a while.
            if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)
                nLast.QuadPart = nNow.QuadPart;
                Sleep(0); // replace 0 with a lager number for reduce cpu


I tried it but it didn’t really do the trick. The code that I am using is the same as the above, so I think that you are suggesting to change the Sleep time. With 60fps the max sleep time that will not damage the refresh rate will be 16mili. I have tried it and other values and it didn’t really improved the cpu utilization - it did get the refresh rate worst though :(.
I think that the problem might be in the actual time it takes to do mainLoop 60 times per second, and not in the sleep time.
Any suggestion?


No suggestions, but I’m having the same issue here. I’d love to see this get fixed but haven’t found a nice solution yet, aside from dynamically setting the refresh rate interval and starting/stopping the animation system whenever possible.

It actually makes my system crash and shutdown automatically, because it can’t stand the heat generated by Cocos2d-x apps running for about a minute :frowning:
(but hey, I think that’s good, because now I’ll have to work harder to optimize my apps - instant win on the actual mobile device!)


I guess the very point is in the while loop, in CCApplication or CCDirector.
Just like writing a media player, get the timestamp, and passby the redundant loops to control the framerate.




Humm… all of a sudden it seems to work now! It’s probably Windows doing its weird thingy again…

It would be mucho appreciated if you could put this change (ie. Sleep(1) instead of Sleep(0)) into the git version as well :slight_smile:


By the way, you can see the difference much better when using the parallax demo from the tests example. The main menu doesn’t really improve a lot, but most of the invidual demos will need less cpu time.


Even though using “Sleep(0)”, it won’t reduce the app performance.
The reason is that the cocos2d-x is single thread mode.


I got the same problem here. Although Sleep(1) reduced the CPU utilization, the frame rate becomes unstable. From accurately 60fps to 57~59. Sleep(1) doesn’t mean sleep 1 millsecond but 20 or more…


Yes, that is true. On windows sleep(1) usually sleeps for 10ms unless you change timer granularity with timeBeginPeriod(1). But usually this isn’t a problem if you have 58fps instead of 60fps.