(22) How to optimize memory usage

Source: Internet
Author: User

How to optimize memory usage memory optimization principle

To optimize the use of memory for applications, developers should first know what consumes the most memory, and the answer is texture! Textures will almost occupy 90% of the application memory. So try to minimize the application's texture memory usage, otherwise the application is likely to crash due to low memory. This article describes the two principles of memory optimization that are common to cocos2d-x games.

Understanding Bottlenecks and finding solutions

What kind of texture consumes the most memory? Or how much memory will these textures consume? Of course this doesn't have to be calculated manually, just guessing. The tool is ready here. Using Apple's Tools "Allocation & Leaks". You can start both tools by long pressing the "Run" command in Xcode and selecting "Profile". As shown below:

Use the allocation tool to monitor the memory usage of your app, and use the leaks tool to observe memory leaks. You can also use some code to get additional information about game memory usage, as follows:

    CCTextureCache::sharedTextureCache()->dumpCachedTextureInfo();

After calling this code, the game will run in debug mode, and you'll see some neat, formatted log messages in the Xcode console window.

Cocos2d: cocos2d: "cc_fps_images" rc=5 id=3 256 x 32 @ 16 bpp => 16 KBCocos2d: cocos2d: "XXX/hd/actor.pvr.ccz" rc=1059 id=4 2048 x 2048 @ 32 bpp => 16384 KBCocos2d: cocos2d: CCTextureCache dumpDebugInfo: 2 textures, for 16400 KB (16.02 MB)

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

Never over-optimize

This is a general rule of optimization. In the process of optimization, some tradeoffs should be made. Because sometimes image quality and image memory usage are in two levels of state. Never over-optimize!

Memory-Optimized levels

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

Client level

This is the most important level of optimization. Because we are going to compile the game on the top level of the Cocos2d-x engine, the engine itself provides some optimization options. At this level we can do most optimizations. 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 affect texture memory usage most. There are 3 main factors that affect texture memory, that is, texture formatting (compression or non-compression), color depth, and size. We can use PVR format textures to reduce memory usage. Recommended texture format is PVR.CCZ. The more bits per color the texture uses, the better the image quality, but the more memory is consumed. So we can use a color depth of RGB4444 texture instead of RGB8888, so memory consumption will be reduced by half. Also, oversized textures can cause memory-related problems. So it's best to use medium-sized textures.
    • Audio optimization, 3 factors affect the memory usage of audio files, i.e. audio file data format, bitrate, and sample rate. Audio files in the MP3 data format are recommended because both the Android platform and the iOS platform support the MP3 format, and the MP3 format is compressed and hardware accelerated. Background music file size should be less than 800KB, the simplest way is to reduce the background music time and then repeated playback. Audio file sampling rate is about 96-128kbps, bit rate 44kHz is enough.
    • Font and particle optimization, here are two tips: use a minimum amount of text when using Bmfont fonts to display game scores. For example, if you want to display only the number of units, you can remove all the letters. As for particles, you can reduce memory usage by reducing the number of particles.
Engine class

If you are not a opengles and game engine player, you can skip this section. Because Cocos2d-x is an open source game engine, let us know if you've done anything in the engine class! Welcome any improvements and code.

C + + language level

In this hierarchy, the recommendation is to write a memory-free leak code. Follow Cocos2d-x's built-in memory management principles to avoid memory leaks as much as possible.

Tips and Tricks
    1. Load game resources in one frame at a frame
    2. Reduce draw calls, using "Ccspritebatchnode"
    3. Load textures in order from large to small
    4. Avoid peak memory usage
    5. Pre-load game resources using the load screen
    6. Free free resources when needed
    7. Release the cache resource after receiving a memory warning.
    8. Use Texture Packager to optimize texture size, formatting, color depth, and more
    9. Use JPG format to be cautious!
    10. Please use RGB4444 color depth 16-bit texture
    11. Please use npot texture, do not use pot texture
    12. Avoid loading into hyperspace large textures
    13. Recommended 1024*1024 npot PVR.CCZ texture set instead of raw PNG textures

(22) How to optimize memory usage

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.