How cocos2d-x optimizes Memory Applications, cocos2d-x optimizes memory

Source: Internet
Author: User

How cocos2d-x optimizes Memory Applications, cocos2d-x optimizes memory

I have previously written about how to optimize the memory of cocos2d-x applications, and how to deal with the punishment of the game in the case of insufficient memory. Today, I saw a friendly introduction to memory on Weibo, which is quite detailed. I don't know who wrote it. I keep a record.

1. IOS and images are stored on IOS, and the images are scaled to the Npower of 2. For example, a 1024*1025 image occupies the same memory as a 1024*2048 image. The formula for the total memory size occupied by images is: length * width * 4. What a memory occupied by 512*512 is 512*512*4 = 1 M. Other dimensions, and so on. (Ps: the maximum size supported on IOS is 2048*2048 ). 2, cocos2d-x image cache Cocos2d-x in the organs of a genie will use spriteWithFile or spriteWithSpriteFrameName and so on whatever way, the cocos2d-x will load this picture into the cache. If this is the first time this image is loaded, the image will be first loaded to the cache and then read from the cache. If the cache already exists, it is extracted directly from the cache, eliminating the need to load the process. The following two types of image caching are taken by surprise: CCSpriteFrameCache and CCTextureCache CCSpriteFrameCache load a spliced big image. Each small image is only one region in the big image, all the region information is stored in the plist file. The hour of use is only important according to the title of the thumbnail can be loaded to this area. CCTextureCache is an ordinary image cache. All of our directly Loaded Images will be stored in this cache to facilitate the use of and obedience. Therefore, the entire image is loaded into the memory each time an image is loaded or a spliced image is loaded by the plist. If you do not want to be imprisoned, it will continue to be occupied. 3. Rendering Memory. Do not think that when the total memory is used, it is enough to calculate the memory loaded into the cache. Take a 1024*1024 image as an example. CCSprite * pSprite = CCSprite: spriteWithFile ("a.png"); after using the above code, you can see in the LEAKS stuff that the memory is increased by about 4 MB. Then, addChild (pSprite) is called. At this time, the memory is increased by 4 MB. That is, if rendering is necessary for an image, the memory occupied by it will be X2. Take a look at the image loaded by plist. For example, the size of this large image is 2048*2048. A 32*32 small image CCSpriteFrameCache: sharedSpriteFrameCache ()-> addSpriteFramesWithFile ("B. plist "); at this time, the memory is increased by 16 M (perspiration) CCSprite * pSpriteFrame = CCSprite: spriteWithSpriteFrameName (" b1.png "has a size of 32*32, and I want to increase the memory, the practice is to increase the memory by 16 Mb. That is, if only one part of the image is rendered, the whole image will be loaded together. However, the environment is not that bad. The previously rendered images, if loaded again, will not continue to increase the memory. For example, we have added 100 B. in another plist region, the image memory is increased by 16 + 16 = 32 M, rather than increasing. 4. cache bans assume that there are many scenarios in the game. When switching the scenario, you can release all the memory in the previous scenario to prevent the total memory from being too high. CCTextureCache: sharedTextureCache ()-> removeAllTextures (); Detain all images loaded so far CCTextureCache: sharedTextureCache ()-> removeUnusedTextures (); release the CCTextureCache: sharedTextureCache ()-> removeTexture. It is worth noting that the reason for detention is mostly to block capital at the moment of switching the scenario. Assume that the order of the called function is B: init () ---->:: exit () ----> B: onEnter (). If the switching effect is used, for example, CTransitionJumpZoom: transitionWithDuration, the order of misappropriation of the function is B: init () ----> B: onEnter () ---->:: exit (). In addition, the second method will overlay the profits of the two scenarios in an instant. If the two scenarios are not extreme, it is likely to collapse due to the short memory promotion. When you do not want to forcibly monitor all resources, an exception will occur when an animation is being referenced. You can use CCActionManager: sharedManager ()-> removeAllActions (); to solve this problem. 5. The experience of memory optimization is equivalent to simply splicing images, so that the side length of the image is as long as possible to keep the n power of 2 and the installation is very full. But to be patient, a logically coherent picture can only be packaged in a large image. The other point is to think about the diffuse hierarchy during packaging. CCSpriteBatchNode may be used for rendering. The image in the opposite BatchNode is at the same level and must be packaged into a different plist Based on the hierarchical relationship of each image. Occasionally, the memory and dependency cannot be both done, but even if it is out of order. 6. Memory constraints for the other generation of IOS devices. Recommended maximum memory: iPad2/iPhone4s/iphone4 170-180 mb 512 mbiPad/iPod touch3, 4/iphone3gs 40-80 mb 256 mbiPod touch1, 2/iPhone3g/iPhone1 25 mb the above advocates the fact that the memory is only tested by some people themselves, the available RAM is not more than half of the maximum memory, if it exceeds half of the maximum memory, it may fail. For other LEAKS, checking the total memory of the simulator and the real machine will result in a large balance of payments. The ultimate goal in the simulator is closer to practice.
I. How to optimize the memory usage of cocos2dx (advanced)

P = 93 I. Memory optimization principle in order to optimize the application memory, you should know what consumes most of your application's memory. The answer is Texture )! It occupies almost 90% of the application memory. We should try our best to reduce the texture memory usage of our application, otherwise our application process may be killed by the system. Here we provide two general guidelines for cocos2dx game memory optimization to reduce memory warnings. 1) understand the bottleneck, and then solve what kind of texture consumes most of the application's memory? Or how much memory does these textures consume? You do not need to perform manual computation or speculation. Here we have a tool. It is Apple's development tool-Allocations & Leaks. In xcode, you long press the Run button and select Profile to start these two tools. Here we have one: You can use the Allocation tool to calculate the memory usage of your application and view the memory leakage. You can also use some code to obtain useful information about the game memory usage. Run the following code: CCTextureCache: sharedTextureCache ()-> dumpCachedTextureInfo (); as follows: when you call the code and run your game in DEBUG mode, in your xcode console window, you will see some formatted log information Cocos2d: cocos2d: "cc_fps_images" rc = 5 id = 3 256x32 @ 16 bpp => 16 KB Cocos2d: cocos2d: "XXX/hd/actor. pvr. ccz "rc = 1059 id = 4 2048x2048 @ 32 bpp => 16384 KB Cocos2d: cocos2d: CCTextureCache dumpDebugInfo: 2 textures, for 16400 KB (16.02 MB) these logs show the texture name, reference number, id, The size and the bit value of the pixel. The most important thing is that it shows the memory usage. As shown above, cc_fps_images consumes 16 KB, and actor. pvr. ccz consumes 16 MB of memory. 2) do not over-optimize this is a regular optimization rule. When optimizing the memory, you should make a trade-off. Because sometimes the image quality is opposite to the image memory usage. Therefore, do not over-optimize it. Ii. Memory optimization level here we divide cocos2dx memory optimization into three levels. At each level, we have different points of view and some changes in strategies. 1. The cocos2dx client level is the most important optimization level we can consider. Because we develop games on the cocos2dx engine, the engine provides many optional optimization solutions. At this level, we can do the most work. First, let's take a look at texture optimization. In order to optimize the texture memory usage, we must know what factors affect the memory usage. Three factors affect texture memory usage. Texture format (compressed or non-compressed), color, and size. We can use PVR format textures to reduce memory usage. The most recommended texture format is pvr. ccz. The higher the bit value of each color, the better the image quality. But it also consumes a lot of memory. Then we use the color depth of RGBA4444 texture instead of RBGA8888, which will consume half of the memory. We will also find that large textures will also cause memory-related problems. So you 'd better use a moderate size. Next, let's do something about sound. There are three factors that affect file memory usage. It is the audio file format, bit rate, and sample rate. We most want the mp3 format for audio files. Because it is supported by both android and ios. It is also compressed and the hardware is accelerated. Make sure that the size of your background music file is kb. The simplest way is to reduce the playing time of background music and... the remaining full text>

How to optimize the drawing of CCSprite in cocos2d-x

You first use TexturePacker to generate a pvr diagram, then use CCSpriteBatchNode to load pvr, add CCSpriteBatchNode to the Layer, and add CCSprite to CCSpriteBatchNode after creating CCSprite, let CCSpriteNode optimize CCSprite on its own.
I don't know if you are satisfied with this answer.

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.