First, COCOS2DX How to optimize memory usage (Advanced article)
This article by qinning199 Original, reprint please specify: http://www.cocos2dx.net/?p=93
First, the principle of memory optimization
To optimize the application memory, you should know what is consuming most of your application's memory, the answer is texture (texture)! It occupies almost 90% of the application memory. Then we should try our best to reduce the texture memory usage of our application, otherwise our application process may be killed by the system.
To reduce memory warnings, here are two general guidelines for memory optimization for COCOS2DX games.
1) understand the bottleneck and then resolve the
What textures consume most of the application's memory? Or how much memory is consumed by these textures? You don't have to calculate or guess by hand.
Here we happen to have a tool. It's Apple's development tool-allocations & Leaks, in Xcode you press and hold the Run button and select profile to start the two tools. Here we have a:
You can use the allocation tool to calculate your app's memory usage and to see memory leaks.
You can also use some code to get some useful information about the memory usage of the game.
The following code can be:[CPP]View Plaincopy
As shown below: When you call this code and run your game in debug mode, you will see some formatted log information in your Xcode console window
These logs show the texture's name, reference number, ID, size, and the bit value of the pixel, and most importantly it shows the memory usage. As the cc_fps_images consumes 16KB,ACTOR.PVR.CCZ consumes 16M of memory.
2) do not over-optimize
This is a general rule of optimization. When you are doing memory optimization, you should make a tradeoff. Because sometimes picture quality and picture memory usage are opposite on both sides. So don't over-optimize.
Second, memory optimization level
Here we divide the COCOS2DX memory optimization into three levels. At each level, we have different opinions and strategies are also somewhat varied.
1. COCOS2DX Client Level
This is the most important level of optimization that we can care about. Because we are developing games on the COCOS2DX engine, the engine itself offers a number of options to optimize the program. At this level, we can do most of the work.
First, let's take a look at the texture optimization
In order to optimize the use of texture memory, we must know what factors affect the use of memory.
There are three factors that affect the memory usage of textures. Texture format (compressed or non-compressed), color, size.
We can use the PVR format texture to reduce memory usage. The most recommended texture format is PVR.CCZ, the higher the bit value per color, the better the picture quality. But it also consumes a lot of memory.
Then we use the color depth is the RGBA4444 texture to replace the RBGA8888, this will consume half of the memory.
We also find that large textures can also cause memory-related problems. Then you'd better use a moderate size.
Second, let's do something about the sound
There are three factors that affect the use of file memory. is the audio file format, bit rate, and sample rate
We most want audio files when mp3 format. Because it is supported by both Android and iOS. Also it was compressed and the hardware accelerated.
You should make sure your background music file size is 800KB at a moment. The simplest way is to reduce the playback time of the background music and repeat the call.
You should keep your audio file sample rate between 96-128kbps, and the bit rate at 44kHz is enough.
Finally, we talk about font and particle system optimization.
Here we have two suggestions: when using BM font to display game scores, select the smallest number characters in your image file, for example:
If you want to display only the numbers, you can remove all the characters.
In particle systems, we can reduce the number of particles to reduce memory usage.
2. COCOS2DX Engine class
If you are not good at opengles and the game engine kernel, you can leave this part to an engine developer. If you are an open source game engine enthusiast, if you have done some optimization of the engine level, please inform the engine developers!
3. C + + language level
At this level, my advice is to write some code without memory leaks. Use the COCOS2DX Engine memory management tool and do your best to avoid memory leaks.
III. recommendations and techniques
1, a frame of the load game resources.
2, reduce draw calls. Using Ccspritebatchnode
3. Load textures in order of maximum to smallest
4. Avoid peak memory usage,
5. Use the loading interface to pre-load the game resources.
6. Release useless resources when you don't need them.
7. Release cached resources when there is a memory warning
8, use Texturepacker to optimize the texture size, format, color depth value and so on.
9. Be careful with JPG files
10. Use 16-bit RBGA4444 color depth texture
11. Replace pot texture with npot texture
12, avoid loading large-size pictures
13. Use 1024*1024 npot PVR.CCZ texture atlas instead of native pictures
Turn COCOS2DX memory optimization (bis)