Memory Management--cocos2d-x learning process (v)

Source: Internet
Author: User

Cocos2d-x uses a memory management mechanism for reference counting and auto-recycling.

Each object contains a reference counter that is used to control the life cycle, which is the member variable m_u-referenceof the Ccobject. We can get the object's current reference count value through the Retaincount () method.

When an object is created through a constructor, the reference value is assigned to 1, indicating that the object is referenced by the creator. when referencing an object elsewhere, we call the Retain () method to increase its reference count by 1, to get the reference to the object, to call the release () method at the end of the reference, and to subtract 1 from its reference count value, which means that the object's reference is disposed.

Another interesting approach is autorelease (), which is to put objects in the auto-recycle pool (ccautore-leasepool). When the recycle pool itself is freed, it executes the release () method once for all objects in the pool, enabling a flexible garbage collection. the recycle pool can be created and freed manually. In addition, the engine creates a recycling pool before each game cycle starts, releasing the recycle pool after the loop is over. Therefore, even if we do not manually create and release the Recycle pool, at the end of each frame, the objects in the auto-recycle pool are also executed once the release () method. We will soon have a glimpse of the convenience of Autorelease ().

Although Cocos2d-x has ensured that the Recycle pool is released at the end of each frame and a new recycle pool is created before the next frame starts, we should also consider that the recycle pool itself maintains a list of objects that will perform the release operation, and if a large number of Autorelease objects are generated within a frame, will cause the recovery pool performance to degrade. Therefore, before and after generating Autorelease object-dense areas (usually loops), it is better to manually create and release a recycling pool.

We can create or release the Recycle pool through the Recycle pool manager Ccpoolmanager's push () or pop () method, where Ccpoolmanager is also a singleton object.

An auto-recycle pool is nested. Typically, the engine maintains a recycling pool, and all Autorelease objects are added to the pool. Multiple auto-recycle pools are arranged into a stack structure, and when we manually create the recycle pool, the recycle pool is pushed to the top of the stack, and the Autorelease object is added only to the top pool. When the recovery pool of the when top layer is ejected, all objects inside it are freed once, and then the Autorelease objects that appear are added to the next pool.

Http://blog.163.com/[email protected]/blog/static/60709702201210122112624/

Memory Management--cocos2d-x learning process (v)

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.