Amazing Cocos2d-x 3.0 15 memory Management (3): How to optimize memory usage __cocos2d-x3.0

Source: Internet
Author: User
1. Memory Optimization principle


To optimize application memory use, developers should first know what the most memory of the application, the answer is the texture. Textures can occupy almost 90% of the applied memory. So minimize the application of the texture memory usage, otherwise the application is likely to crash because of low memory. This paper introduces the two principles of memory optimization for Cocos2d-x game.


(1) Identify bottlenecks to find solutions

What kind of texture consumes most of the memory used. Or how much memory these textures will consume. Of course this is not manual calculation, just guess. The tools are ready here. Using the Apple tool "allocation & leaks". You can click "Run" in the Xcode and select "Profile" to start the two tools. As shown below:



Using the allocation tool, you can monitor the memory usage of the application and use the leaks tool to observe the memory leaks. In addition, some code can be used to get additional information about game memory usage, as follows:

Cctexturecache::sharedtexturecache ()->dumpcachedtextureinfo ();

When this code is invoked, the game will run in debug mode, and you will see some neatly formatted log information in the Xcode console window.

Cocos2d:cocos2d: "Cc_fps_images" rc=5 id=3 256 x/BPP => KB
cocos2d:cocos2d: "Xxx/hd/actor.pvr.ccz" rc=  1059 id=4 2048 x 2048 @ BPP => 16384 kb
Cocos2d:cocos2d:CCTextureCache dumpdebuginfo:2 textures, for 16400 kb (16.02 MB)

You can see the name of the texture, the reference count, the ID, the size, and the number of bits per pixel that are displayed. The most important thing is to display the memory usage. such as "cc_fps_images" refers to the consumption of 16KB memory, and "ACTOR.PVR.CCZ" consumes 16M memory.


(2) do not over optimize

This is a general optimization rule. In the process of optimization, some trade-offs should be made. Because sometimes image quality and image memory usage are in two levels of state. Never try to be overly optimized.


2. Memory Optimization level


The Ccos2d-x memory optimization is divided into three levels. Each level has a different description, and the policy is a little differently.


(1) client level

This is the most important level of optimization. Because we want to compile the game at the top of the Cocos2d-x engine, the engine itself provides some optimization options. We can do most of the optimization at this level. In short, we can optimize the memory usage of textures, audio, fonts, and particles.

First: Look at texture optimization, in order to optimize texture memory usage, you must know what factors have the greatest impact on texture memory usage. There are 3 main factors that affect texture memory, i.e. texture format (compression or uncompressed), color depth, and size. We can use PVR format textures to reduce memory usage. The recommended texture format is PVR.CCZ. The more each color the texture uses, the better the image quality, but the more memory it consumes. So we can replace the RGB8888 with a texture with a RGB4444 color depth, which reduces memory consumption by half. In addition, oversized textures can also cause memory-related problems. So it's best to use a medium sized texture.


Second: Audio optimization, 3 factors will affect the memory use of audio files, that is, audio file data format, bit rate and sampling rate. It is recommended to use audio files in MP3 data format because both the Android platform and the iOS platform support the MP3 format, in addition to the MP3 format compression and hardware acceleration. Background music file size should be less than 800KB, the easiest way is to reduce background music time and then repeat playback. The audio file sampling rate is about 96-128kbps, and the bit rate of 44kHz is enough.


Third: font and particle optimization, here are two tips: Use the Bmfont font to display the game score, please use the minimum amount of text possible. For example, if you want to display only the number of units, you can remove all the letters. As for particles, the use of memory can be reduced by reducing the number of particles.


(2) engine grade

Need opengles and Game engine master


(3) C + + language level

In this level, it is recommended to write a memory-free leak code. Follow the Cocos2d-x built-in memory management principles to avoid memory leaks as much as possible.


3. Tips and Tricks


(1)   frame-by-frame load game Resources
(2)   Reduce drawing calls, using "Use Ccspritebatchnode"
(3)   load textures in order from large to small
(4)   Avoid peak memory use
(5)   Use load screen preload game resources
(6)   free free resources when needed
(7)   receive memory warning to release cached resources.
(8)   Use Texture Packager to optimize texture size, format, color depth, and so on
(9)   use JPG format to be cautious.
  Use RGB4444 color depth 16-bit texture
  Please use npot texture, do not use pot texture
(a)   Avoid load large textures
  recommended 1024* 1024 npot PVR.CCZ Texture set instead of raw PNG texture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.