First, if you encounter application lag or excessive memory usage, you can usually use Instruments to detect it. But for complex situations, you may need to use the child thread to monitor the main thread. Here are some introductions to these methods: Time Profiler You can view the methods that take too much time in multiple threads. First, check Hide System Libraries on the right to filter the information. Then, the Call Tree will be sorted by the time-consuming threads by default, and the single thread will also be sorted by the corresponding time-consuming methods. After selecting the method, you can double-click the Heaviest Stack Trace on the right to view the specific time-consuming operation code, so that you can optimize it in a targeted manner without over-optimizing in some places that will not affect performance much. Allocations Here you can perform Generations before and after each action, compare the memory increase, and view the specific method and code location that caused the memory increase. The specific operation is to click Mark Generation in Generation Analysis on the right, which will generate a Generation. When you switch to other pages or another event occurs after a period of time, click Mark Generation again to generate a new Generation. Repeat this process to generate multiple Generations. When you check these Generations, you will see the size of Growth. If it is too large, you can click in to view the corresponding thread that occupies a large amount of space and check the corresponding code block in the Heaviest Stack Trace on the right, and then perform corresponding processing. Leak You can see the overflow at the corresponding time point in the Leaks section in the upper area. After selecting it, you can see the leaked object in Statistics>Allocation Summary in the lower area. You can also view the specific corresponding code area through Stack Trace. When developing, you need to pay attention to how to avoid some performance issues NSDateFormatter Through the detection of Instruments, it is found that the time consumption of creating NSDateFormatter or setting the properties of NSDateFormatter is always at the front. How to deal with this problem? It is recommended to add properties or create static variables, so that the number of creation and initialization can be reduced to ***. Another way is to directly use C or this NSData Category to solve it https://github.com/samsoffes/sstoolkit/blob/master/SSToolkit/NSData%2BSSToolkitAdditions.m UIImage The main thing here is that it will affect the memory overhead. You need to weigh imagedNamed and imageWithContentsOfFile. After understanding the characteristics of the two, use the latter for images that only need to be displayed once. This will reduce memory consumption, but page display will increase Image IO consumption, which needs to be paid attention to. Since imageWithContentsOfFile does not cache, it needs to be loaded once before each page display. This IO operation is also a point that needs to be considered. Page Loading If a page has too much content and too many views, the partial view content that needs to be scrolled to be seen in the long page will be loaded synchronously by starting a new thread. Optimize *** loading time You can use Time Profier to view the time taken for startup. If it is too long, you can use Heaviest Stack Trace to find time-consuming methods and modify them. How to monitor lag Another way is to monitor performance issues in the program. You can take a look at this Demo first, the address is https://github.com/ming1016/DecoupleDemo. In this way, you can use this program to record the user's lag operations after going online, and send them to your own server at a regular interval, so that you can collect performance issues on a larger scale. As we all know, the lags perceived at the user level come from the main thread that handles all UIs, including large calculations performed on the main thread, a large number of IO operations, or heavier drawing work. How to monitor the main thread? First of all, you need to know that the main thread, like other threads, is driven by NSRunLoop. You can first take a look at the general logic of CFRunLoopRun.
Based on this RunLoop, we can measure it through CFRunLoopObserverRef. Use dispatch_semaphore_t in GCD to start a new thread, set a limit value and the number of occurrences, and then obtain the scenes on the main thread that exceed the limit value and the number of occurrences between kCFRunLoopBeforeSources to kCFRunLoopBeforeWaiting and then to kCFRunLoopAfterWaiting, dump the stack, and send it to the server for collection. Through the stack, we can find the method that has the problem.
Sometimes the jam is caused by abnormal, excessive, or large data, or by abnormal operation. Such situations may be difficult to encounter in daily development and testing, but they will occur in real life, especially when the user audience is wide. In this way, this method of collecting jams is still valuable. Stack dump method The first is to directly call the system function to obtain the stack information. This method can only obtain simple information. It cannot cooperate with dSYM to obtain the specific line of code that has a problem, and the types are also limited. The main idea of this method is to obtain the error signal through signal. The code is as follows
The report generated by PLCrashReporter seems to be able to locate the specific location of the problematic code.
The content in the stack in the test demo exceeds the number of characters in the WeChat text, so it is omitted in this article. |
<<: Talk about MVVM and chained network request architecture
>>: iOS system swipe right to return to the global control solution
Spring is the most beautiful season of the year, ...
This is the best of times, and the worst of times...
How to shoot a good Douyin video with strong sale...
With the continuous development of informatization...
"I heard that eating too much MSG is not goo...
A mysterious extraterrestrial ocean, who lives th...
During the epidemic, all walks of life were inevi...
Training course content: When it comes to OKR, yo...
Today, I was browsing the social network and sudd...
Diet management is an extremely important part of...
Famous Artists Gallery | Lou Shibai, a native of ...
LONDON (Reuters) - Britain's largest carmaker...
The rapid development of mobile Internet and smar...
In 2019, a beluga whale suspected to be a Russian...
There are not many articles with similar titles o...