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
- Bool helloworld: Init ()
- {
- If (! Layer: Init ())
- {
- Return false;
- }
- _ String * s = new _ string ();
- Log ("% s", S-> getcstring ());
- ... ...
- Return true;
- }
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