Instruments is a dynamic analysis tool that integrates with Xcode and can be launched from the menu Product→profile in Xcode. Start, instruments has many trace templates that dynamically analyze and track memory, CPU, and file systems.
Each trace template has a different purpose, where leaks can detect a memory leak point, and the Allocations trace template can view memory usage. Let's use an example to introduce some of the instruments tools, we'll make a memory leak, and we'll modify the code in HelloWorldScene.cpp:
| 1234567891011 |
boolHelloWorld::init(){ if( !Layer::init() ) { returnfalse; } __String *s = new__String(); log("%s",s->getCString());… … returntrue;} |
We added the __string *s = new __string () statement to the code and allocated memory using the New keyword, but we didn't use the delete s statement to release it until the end of the init function. Such statements can cause a memory leak.
Start the Instruments tool first, select the leaks tracking template in the Diagram dialog box, and click the button profile to enter the interface.
In instruments, although the leaks template is selected, the Allocations template is added by default. Basically, the allocations template is used to analyze memory, which monitors the distribution of memory. With the Allocations template selected (the ① area in the figure), the ③ area on the right shows the line chart with memory usage over time, and the ④ area shows details of memory usage and object assignment. Click on the leaks template (② area in the figure) to see the memory leak, 20-3, if there is a red line in the ③ area, there is a memory leak, the ④ area will display the leaked objects.
Click on the leak point (red Line) in the ④ area to open the detailed interface, as shown, you can find a Cocos2d::__string object, you can see its memory address, the consumption of bytes, the framework and the owning function and other information.
Click the button in the toolbar view, open the extension detail view, 20-5, you can see the tracking stack information on the right, where the icon shows the entry is our own application of the code, double-click Helloworld::init () to enter the program code.
The 33rd line of code shown in the figure is the leak Point program code, which is not difficult to find.
If we want to further evaluate its memory footprint, you can look at the line chart for the Allocations template. See, the use of various variables of memory consumption, you can also know the memory consumption of a certain moment. Area 4th is the heap memory footprint because heap memory needs to be artificially freed, and stack memory is not artificially managed.
In fact, memory leaks are extremely complex issues, and tools are used on the one hand, and experience is on the other. Improve your experience and then use tools to solve the underlying memory leaks.
Source URL: http://blog.csdn.net/tonny_guan?viewmode=contents
Introduction to performance optimization tools in cocos Development (i): Xcode instruments Tool Use