"Go" cocos2d-x with iOS memory management analysis (reduces memory pressure in the game)

Source: Internet
Author: User

Monkey Original, welcome reprint. Reprint Please specify: reproduced from COCOS2D Development network –cocos2dev.com, thank you!

Original address: http://www.cocos2dev.com/?p=281

Note: I have previously written about how Cocos2d-x optimizes memory usage and how to deal with games when memory is low. Today in micro-blog saw a friend introduced the next memory, quite detailed. I don't know who wrote it, I recorded it.

one, iOS and picture memory

On iOS, the image is automatically scaled to 2 of the n-th square size. For example, a picture of 1024*1025, the memory occupied by a picture of a 1024*2048 is consistent. The equation for the calculation of the memory size of the picture is the length * width. The memory occupied by such a 512*512 is 512*512*4 = 1M. Other dimensions and so on. (The maximum size supported on Ps:ios is 2048*2048).

two, Cocos2d-x's picture cache

Cocos2d-x will use Spritewithfile or spritewithspriteframename when constructing an elf, and Cocos2d-x will load the image into the cache regardless of the way it is used. If this is the first time the image is loaded, it will be loaded into the cache and then read from the cache. If the cache already exists, it is extracted directly from the cache, excluding the loading process.

The cache of pictures is mainly handled by the following two classes: Ccspriteframecache, Cctexturecache

Ccspriteframecache Loaded is a large piece of stitching, each small image is only a large map of the area, these areas of information are stored in the plist file. It can be loaded into this area only by the name of the small map.

Cctexturecache is a normal image cache, and all of our directly loaded images are put into this cache by default to improve the efficiency of the call.

As a result, the entire picture is loaded into memory each time a picture is loaded, or when a mosaic image is loaded via plist. If you don't release it, it will always be occupied.

third, render the memory.

Do not assume that when calculating memory, only the memory loaded into the cache can be computed. Take a picture of 1024*1024 as an example.
Ccsprite *psprite = Ccsprite::spritewithfile ("A.png");

After calling the upper line of code, you can see in the leaks tool that it adds approximately 4M of memory. and then call
AddChild (Psprite);

At this point, the memory is increased by 4M. That is, a picture, if it needs to be rendered, the memory it occupies will be X2.

Take a look at the pictures loaded by plist, such as the large image size of 2048*2048. Want to load a small picture of one of the 32*32
Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("B.plist");

Memory increase at this time 16M (sweat)

Ccsprite *pspriteframe = Ccsprite::spritewithspriteframename ("B1.png");
B.png size is 32*32, thinking that is to add a little memory, but the reality is to increase the 16M of memory. That is, if you render a portion of it, the entire picture is loaded together.

But the situation is not so bad, these already rendered pictures, if loaded again, memory will not continue to rise, such as the addition of 100 b.plist another area, the image memory or total increase 16+16 = 32M, and will not continue to rise.

four, Cache release

If the game has many scenes, the memory of the previous scene can be completely freed when switching scenes, preventing the total memory from being too high.

Cctexturecache::sharedtexturecache ()->removealltextures (); Release all pictures loaded so far

Cctexturecache::sharedtexturecache ()->removeunusedtextures (); Release the Cctexturecache::sharedtexturecache ()->removetexture () from the picture with the reference count of 1; Release a picture individually

Ccspriteframecache and Cctexturecache release the same way.

It is important to note that the timing of the release, generally when switching scenes to release resources, if you switch from A scene to the B scene, call the function Order of B::init ()-->a::exit ()-->b::onenter ().

If you use a switch effect, such as a function such as ctransitionjumpzoom::transitionwithduration, the function's call order changes to B::init ()-->b::onenter ()-->a:: Exit ().

And the second way there will be a moment to stack two of scenes of resources together, if not to take excessive, it is likely to crash due to memory crunch.

Sometimes forcing the release of all resources causes an animation that is being executed to lose its reference and eject the exception, which can be called Ccactionmanager::sharedmanager ()->removeallactions ();

Five, Memory optimized

Optimization of the experience is to try to splice pictures, so that the picture side length as long as possible to maintain 2 of the N-side and loaded very full. Note, however, that a logically related picture is packaged in a larger picture, and the other is that the distribution of layers is considered when packing. Because Ccspritebatchnode may be used in order to render efficiency, the images in the same batchnode are at one level, so they have to be packaged into different plist depending on the hierarchical relationship of each image. Sometimes memory and efficiency can not be combined, only as far as possible to balance.

Six, other

Finally, a memory limit for each generation of iOS devices is attached.
Device recommended Memory Max memory
Ipad2/iphone4s/iphone4 170-180MB 512MB
Ipad/ipod touch3,4/iphone3gs 40-80MB 256MB
IPod touch1,2/iphone3g/iphone1 25MB 128MB

The above recommended memory is only some people's own test results, the available RAM is not more than half of the maximum memory, if the program exceeds the maximum memory of half, it may be hung off.
In addition, in the leaks to see the simulator and the total memory of the real machine, there will be a large discrepancy. The results in the simulator are closer to the actual ones.

    • This article fixed link: http://www.cocos2dev.com/?p=281
    • Reprint Please specify: Houzi July 24, 2012 in COCOS2D Development Network published

"Go" cocos2d-x and iOS Memory Management analysis (reduce memory pressure in game)

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.