IOS CPU share reached 100% or more, and then led to app Flash back

Source: Internet
Author: User
Tags vars

Today in the process of real machine debugging, found a serious problem, found that the CPU utilization rate reached 100%, so that will lead to run memory consumption is too high, by the system watchdog mechanism to kill.

Let's talk about how to locate the problem:

1. Open Xcode, run the project and select this tab

2. Now you can see this picture

3. Now we can see this page, and found that my CPU reached 105%, which is definitely a problem, then what to do now, we can see the graph on the right, click on the profile in Instruments. ---"and then click Transfer.

4. Now go into the instruments, and we'll see what happened and what exactly it was that led to the problem.

1. First of all, I look at the CPU utilization, the various pages to check, see what is the operation, the emergence of this CPU occupancy is high.

2. I'm glad that I was quick to pinpoint the reason. So I can see that it's going to be a page that triggers some sort of action, and then that happens.

3. It is now possible to navigate through instruments to see what code is being executed, causing this very time-consuming operation to keep the CPU busy.

4. Select Xcode to run the program (COMMAND + R) first

5. Select Xcode and press the shortcut key (Command + control + i) to run, and leaks is running.

6. Since leaks is dynamic monitoring, we need to manually operate the app, while operating, while observing the changes in the leaks, when the Red Fork, the memory leaks are monitored, click the second in the upper right corner, to pause detection (also can continue to detect, when more than one pause, more than one processing).

Extension: Check memory leak specific method click Open Link L

5. Computer card burst, eh. Went back in, anyway, eventually tracked down to the runloop. There is a behavior has been occupied the main thread, and does not release, so the CPU has been a lot of consumption, memory slowly gradually grow, the general can cause this situation is only the loop, and has not been released, I use instruments in the leaks, Then there were a variety of memory leaks detected and repaired, and that's exactly how I found out where the problem was. I wrote it. There is a problem with the method. I wrote the following code:

We clearly see that if the condition is true, this is a dead loop, my pm then, here would like to do a picture has been flashing effect, here can use three kinds of scenarios, one is to use this circular reference to execute a set of methods, one is to call a method by Nstimer. I began to choose the former, then also know the consequences, perhaps this cycle will continue to exist until the VC is Dealloc, the last one is through the core animation to achieve. This is the most recommended, specific wording, I will open a blog in the back to explain

    1. -(void) Animationaction: (bool) isneedbreak{
    2. if (!isneedbreak) {
    3. [self performselector:@selector (animationaction:) withobject: [NSNumber numberwithbool:YES  ] Afterdelay:2];
    4. }
    5. }

2. Because of the above worry so, I popviewcontroller, controller out of the stack, I called the following method, that time too careless, presumably, is I want to delay 2s execution of a method, the process, I want to terminate the method, it is only by invoking the following two kinds of random, I was naïve to think that it would be perfect to terminate the call of the dead loop.

    1. This is the cancellation of all deferred execution functions.
    2. [NSObject cancelpreviousperformrequestswithtarget:self selector:@selector (animationaction:)   Object:[nsnumber Numberwithbool:YES]];
    3. [NSObject cancelpreviousperformrequestswithtarget: Self];



3. Found that the problem persists, so only with my second solution, use Nstimer instead of him. The code is as follows

 
    1. Nstimer *animationtwotime = [Nstimer scheduledtimerwithtimeinterval:2 target:self selector:@ Selector (productbrandiconanimationwithisbreak:) userinfo:[nsnumber numberwithbool:NO] repeats:  YES];

4. Then the VC out of the stack, and then put Nstimer invalidate under.

Summarize:

1. Be sure to use the For loop for continuous execution of animations in the future.
2. This animation effect as far as possible with the core animation to solve.

IOS CPU share reached 100% or more, and then led to app Flash back

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.