Cocos2d-x memory management (Insight), cocos2d-x Memory Management

Source: Internet
Author: User

Cocos2d-x memory management (Insight), cocos2d-x Memory Management

Cocos2d-x continues the memory management mechanism of cocos2d and OC reference count!

Next let's take a look at the CCDriectro class CCPoolManager: sharedPoolManager ()-> push (); Basics: familiarize yourself with release to release the memory! CCPoint * temp = new CCPoint (controlPoint. x, controlPoint. y );

M_pControlPoints-> addObject (temp );

Temp-> release ();

 

Autorelease is a tag for automatically releasing objects! The following describes how to perform automatic memory release.

CCString * pRet = new CCString (str );

PRet-> autorelease ();

Return pRet;

 

CCPoolManager: sharedPoolManager ()-> pop (); let's take a look at these two methods! It is equivalent to the memory release pool of OC! During each rendering in CCDriectro! Check whether there are objects with a memory count of 0 in the memory release pool! If yes! It will be cleared! Release memory!

In fact, we should pay attention to some memory management during game development, which is of great help to the game! Accumulate! The game memory will be tight! There are essential solutions!

1. If the scenario is switched to memory, we can clear the cache first!

// Clear the cache CCDirector: sharedDirector ()-> purgeCachedData ();

2. Pay attention to scenario switching! Scenario switching has the force-on join! Replace to join!

Here we will talk about pushScene and popScene ). They can be used to display the scenario and retain the current scenario and display new scenarios. The difference is that they do not release the old scenario from the memory, which can improve the loading speed, if the memory is insufficient, we recommend using the replaceScene function.

The preceding process consists of the following three steps:

1) Call CCDirector: sharedDirector ()-> purgeCachedData () to clear the cache.
2) create a scenario.
3) Call CCDirector: shareddire()-> replaceScene (this) to replace the new scenario. The Cocos2D-x provides special effects for switching between scenarios, which will be described in the next section.


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 enable the default cocos2d template in the Project to Support ARC Memory Management

1. In your project, select the libs folder from Control-Click, and select Delete from the pop-up menu. Then, in the Delete dialog box, select the Delete button again to completely Delete it. This will delete the Cocos2D file from the project, but this is required. Because we will introduce it to the project separately, we need to do this by allowing the use of ARC to set our project (but Cocos2D code still does not include ARC ). 2. Find the cocos2d-ios.xcodeproj file from the original Cocos2D 2.0 project we downloaded and drag it into our development project.

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.