Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High CPU Usage when run as Debug vs as Release (Instruments run as Release). How to Debug?

I've been having what I would think as performance issue on my app when running on old hardware (iPad mini 2 which is close to 7 years old). It's unfortunately seems like sporadic whereby 2/10 times it runs OK and the rest, CPU Usage creeps upwards of 90% as time goes by.

The App is doing some intensive(?). drawRect stuffs where I use bezierpath to draw a live HeartRate (among others) chart. Using Instruments does not really show me where the possible bottleneck is occurring. (TBH, I'm not very good at reading the output tho)

(for this image, I used 'invert call tree', 'hide system libraries' enter image description here

I tried putting in some time profiling of my own

let start = CFAbsoluteTimeGetCurrent()
let end = CFAbsoluteTimeGetCurrent() - start
print("drawRect END:\(Date()) - total Secs:\(end)")

and while it's slower than my iPhone 7, I think it's still acceptable.

drawRect END:2021-01-16 00:39:26 +0000 - total Secs:0.002599000930786133 
drawRect END:2021-01-16 00:39:27 +0000 - total Secs:0.001813054084777832
drawRect END:2021-01-16 00:39:28 +0000 - total Secs:0.0019180774688720703
drawRect END:2021-01-16 00:39:29 +0000 - total Secs:0.0016759634017944336 

In the end, I found this post --> https://developer.apple.com/forums/thread/19936 and when I started the run as "release" then the high CPU Usage basically went away.

Question:

  1. During normal app development, I agree that running as Debug would be beneficial, but what are the actual benefit? How is the Debug config different? (Some reading material / Links would be good) TQ

  2. If instruments are running as "release" config, then how does one actually be able to figure out what's happening under the hood? Like in my case, I had no idea Instruments is running as "release" and normal run (Cmd-R) is running as "Debug". This is a big mismatch and obviously threw me off.

  3. So.. Do I have an issue or do I not? It's highly likely that I do, but I, for the life of me, can't seems to figure out why.

  4. The iPad Mini2 is definitely old and slow. I downloaded some CPU/System monitor app from the store and just running it I am seeing 40% CPU usage already (!!) Is there a way to collect CPU usage data within the app? like in a Print statement.

Thanks.

like image 496
app4g Avatar asked Oct 27 '25 09:10

app4g


1 Answers

  1. The benefit of the Debug configuration is that it lets you debug your app. The Debug configuration lets you see things like the values of variables. If you tried to debug your app with the Release configuration, you would see memory addresses instead of variable names. The Debug build configuration sacrifices speed for the ability to debug. The Release build configuration sacrifices debugging for speed.

  2. If you want to see the problem you're having when running in the Debug configuration, edit your project scheme and tell Xcode to use the Debug configuration for profiling. Then Instruments can help you find where the slow spots are in the code in the Debug configuration.

Xcode Change Build Configuration

  1. Focus on time profiling for the Release build configuration, the configuration the people using your app will see.

  2. Take a look at the os and OSLog framework references in Apple's documentation, which you can read in Xcode by choosing Help > Developer Documentation. The os and OSLog frameworks are in the System section. Those frameworks are how you collect profiling data from your code.

For a more detailed explanation of the Time Profiler instrument, read the following article:

Finding the Slow Spots in Your Code with the Time Profiler Instrument

like image 199
Swift Dev Journal Avatar answered Oct 29 '25 23:10

Swift Dev Journal