Introduction to performance optimization tools in Cocos development using the instruments tool in xcode

Source: Internet
Author: User

Instruments is a dynamic analysis tool. It is integrated with xcode and can be started in xcode through the menu product → profile. Start, instruments has many tracking templates for dynamic analysis and tracking of memory, CPU and file systems.



Each trace template has different purposes. leaks can detect memory leaks, and allocations trace templates can view memory usage. Here we will introduce the use of some instruments tools through an example. We will create a memory leak manually, and we will modify the code in helloworldscene. cpp:

[HTML]View plaincopy
  1. Bool helloworld: Init ()
  2. {
  3. If (! Layer: Init ())
  4. {
  5. Return false;
  6. }
  7. _ String * s = new _ string ();
  8. Log ("% s", S-> getcstring ());
  9. ... ...
  10. Return true;
  11. }



We added the _ string * s = new _ string () Statement in the Code and used the new keyword to allocate memory, however, we didn't use the delete s statement to release it until the end of the init function. Such a statement may cause memory leakage.
Start the instruments tool, select the leaks Tracking template in the figure dialog box, and click the Profile button to enter the interface.


In instruments, although the leaks template is selected, the allocations template is also added by default. Basically, the allocations template is used for memory analysis. It can monitor the memory distribution. Select the allocations template (area ① in the figure), and the area ③ on the right will display the line chart of memory usage that changes over time, and the details of memory usage will be displayed in Area ④, and object allocation. Click the leaks template (area ② in the figure) to view the memory leakage, as shown in Area 20-3. If there is a red line in Area ③, there is a memory leak, and area ④ shows the leaked objects.


Click the leak point (Red Line) that appears in Area 4 to open the detailed interface, as shown in, you can find a cocos2d :__ String object, you can see its memory address, occupied byte, framework, function, and other information.



Click the View button in the toolbar to open the extended Details View, as shown in-5. You can see the tracing stack Information on the right. The entries shown in the icon are our own code. Double-click helloworld :: init () can enter the program code ,.





The 33rd lines of code shown in the figure are program code leaks, which are not difficult to find out.
If we want to further evaluate the memory usage, we can look at the line chart of the allocations template. We can also see the memory usage of various variables at a certain time point. The heap (HEAP) memory usage in Area 4 is because the heap memory needs to be manually released, and the stack memory does not need to be manually managed.

 

In fact, memory leakage is extremely complicated. On the one hand, tools are used, while experience is on the other. Improve experience, and then use tools to solve Memory leakage.

 

 

More content please pay attention to the first domestic Cocos2d-x 3.2 version of the book "Cocos2d-x practice: C ++ volume" book exchange discussion site: http://www.cocoagame.net
More exciting Video Courses please follow Zhijie class Cocos Course: http://v.51work6.com welcome to join the Cocos2d-x Technology Discussion Group: 257760386 welcome to follow Zhijie IOS class public platform

Introduction to performance optimization tools in Cocos development using the instruments tool in xcode

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.