High memory usage in cocos2dx switching scenarios
High memory usage in cocos2dx switching scenarios
1. Running scenario:
CCScene * pScene = HelloWorld: scene ();
PDirector-> runWithScene (pScene );
2. replacement scenario:
(1)
CCScene * pScene = SceneTestScene: scene ();
CCDirector: sharedDirector ()-> replaceScene (pScene );
(2) CCScene * pScene = SceneTestScene: scene ();
CCDirector: sharedDirector ()-> pushScene (pScene );
(3) CCScene * pScene = SceneTestScene: scene ();
CCDirector: sharedDirector ()-> popScene (pScene );
3. Specific Code Execution Process
Assume that scene A is an active scenario. Now we use scene B to replace pushScene A, and the lifecycle of A and B is as follows:
B ---- init ();
A ---- onExit ();
B ---- onEnter ();
B ---- onEnterTransitionDidFinish ();
In this case, popScene B is displayed. The function call is as follows:
B ---- onExit ();
B ---- destructor called
A ---- onEnter (); (the init function of sceneA will not be executed, because pushScene will execute the onExit function of sceneA to release some resources. When pup, does not execute the init function to load the required resources. The error occurs)
We can see the following points from the above,
1. The destructor of A has never been called, so A is always in the memory.
2. Execute the init () function of B before calling the onExit () method of A and then calling onEnter () of B. Therefore, it is best to put the initialization in init for initialization. Initialize the resources required by the previous scenario before exiting. (The memory usage is too high. Now)
Similarly, let's take a look at the replaceScene switchover scenario and the lifecycle of scene.
Assume that scene A is an active scenario. Now we use scene B to replaceScene to replace A. the lifecycle of A and B is as follows:
B ---- init ();
A ---- onExit ();
A ---- destructor called
B ---- onEnter ();
B ---- onEnterTransitionDidFinish ();
At this time, B replace A's return call is the same as above, as shown below:
A ---- init ();
B ---- onExit ();
B ---- destructor call
A ---- onEnter ();
4. Run the code of a specific engine to view the object lifecycle. See the link http://blog.csdn.net/tonny_guan/article/details/28121973 below
----------------- Gorgeous split line ---------------- when switching a scenario using replaceScene, the memory occupied by the game is the current memory peak value, which is terrible when the current memory value is used, add the memory occupied by the resources in the next scenario. Cause .... In each scenario, there will be an init function, an onEnterTransitionDidFinish function, an Onexit function, and init to implement initialization. onEnterTransitionDidFinish is executed after init, onexit recycles the resources allocated during init when the scenario exits. This is an interesting phenomenon found during debugging, that is, when switching from scenario 1 to scenario 2, the memory will be very high at the moment of switching, but after a while, the memory will return to a stable State. For example, when switching, the memory will reach 80 Mb, and after switching, the memory will drop to 50 MB. Cause Analysis: it is suspected that the memory in the previous scenario has not been released, and the memory in this scenario has been allocated. Therefore, the two are stacked together, which is relatively high. So I add a breakpoint to the Onexit function of the first scenario, add a breakpoint to the init and onEnterTransitionDidFinish functions of the second scenario, and then run the program, the program first goes to init In the second scenario, then returns to Onexit In the first scenario, and finally to onEnterTransitionDidFinish In the second scenario. I suddenly realized that when switching the scenario, the Onexit function of the first scenario was not executed immediately, but the resource was first loaded in the init of the second scenario, then return to the first scenario to release the resource, and finally to onEnterTransitionDidFinish. The peak value in memory is amazing.
Solution 1: place necessary resources in onEnter for processing, and other resources in onEnterTransitionDidFinish for processing. Solution 2: add an excessive scenario for scenario switching. Currently, mainstream rpg games or horizontal Combat games are all handled in this way. Because of the large amount of resources, this process is efficient and secure.
-- Conclusion
1. replaceScene instead of pushScene is recommended when switching to a full-screen scenario.
Because pushScene does not destroy the previous scene, it simply adds the latter scene to the previous scene as a stack. If the memory management in your code is not well written, it is difficult to identify this problem using pushScene. Once the crash is located, it is difficult to locate it. ReplaceScene can expose hidden problems as early as possible.
2. Try not to initialize Sprite in onEnter
This is a bit related to the previous one. If pushScene is used, the init method of the previous scenario will not be called when popScene is used again (some resources on the previous interface have been released, there is an unknown error), so some people like to put some initialization in onEnter. The specific reason is not good. Let's take a look at the lifecycle of each Scene in different switching scenarios.