Memory Management in cocos2d-x Architecture

Source: Internet
Author: User

Memory Management in cocos2d-x Architecture

Garbage collection mechanism:

The so-called "garbage" refers to the memory space that has been used before and is no longer used or has no pointer. The mechanism for collecting and reusing these "spam" is called "Garbage Collection ".

There are two main methods for garbage collection:

Reference count: the number of times a system-recorded object is referenced. When the number of times an object is referenced is 0, the object is reclaimed as garbage.

Tracking-based processing: generates the relationship diagram of the tracking object before garbage collection.

Cocos2d-x Memory Management Mechanism

The memory management mechanism in cocos2d-x can be seen as a variant based on smart pointers.

The primary responsibility of the Ref base class is to manage the reference count of objects. The autorelease () method declares a "smart pointer". All these smart pointers are added to an AutoreleasePool. When each frame ends, the objects added to the AutoreleasePool are cleared, the lifecycle of a smart pointer in the cocos2d-x starts from being created to the end of the frame.

Manually declare a smart pointer: auto node = new Node (); node-> autorelease (); it is very troublesome to declare such a pointer every time. In the cocos2d-x, most classes can return a smart pointer object through the create () method, such as Node, Action.

The AutoreleasePool performs a release operation on each object in the pool. That is to say, if the current reference count of this object is 2 and the reference count is 1 after a frame, the object continues to exist. The object will be removed next time.

AutoreleasePool queue:

The cocos2d-x customizes the lifecycle of smart pointers by implementing an AutoreleasePool queue, and the PoolManager manages the AutoreleasePool queue. Define an object in the function body until the function ends and the object is destroyed. Similarly, AutoreleasePool can define a pool object in the function body until the function ends and the object is destroyed, we can customize the lifecycle of the AutoreleasePool.

 

Intelligent pointer in cocos2d-x:

All memory management methods in the cocos2d-x are based on reference counts.

Intelligent pointer RefPtr in cocos2d-x Provides thousands of constructors for overloading. RefPtr and Ref pointers are strongly referenced.

Construct an empty smart pointer:

RefPtr
 
  
Ref1; // RefPtr <_ String> ref2 (nullptr) by default; // RefPtr constructed by NULL pointer Parameter
  
   
Ref3 (ref1); // smart pointer replication structure with null references
  
 
There is a value assignment operator in the smart pointer. Unlike the constructor, the value assignment operator not only increases the reference count of its resources, but also releases the reference count of the old resources.

 

 

RefPtr <_ String> ptr = cocos2d: String: create (Hello); // assign a value to "hello"

 

 

From cocos2d-x I understand, author suggests that all UI elements need to be managed using autorelease, while in-game data is managed using smart pointer RefPtr.

Because autorelease relies entirely on the release of the automatic recycle pool, and RefPtr references any Node resource as a strong reference.


 

 

Related Article

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.