Search code examples
macoscpu

Sudden Drop in CPU Utilization in macOS App After Extended Operation


I'm struggling with CPU performance and memory optimization for a macOS app I'm developing.

The app I am developing consists of continuous real-time calculations and graphical elements. If I monitor the individual components, the CPU usage is as follows

  • FetchAudioData (captures real-time data with Timer & performs calculations): 20%-30%.
  • MEMU (graphics operation 1): 30% to 40%.
  • FullScreen (graphics task 2): 20% to 40%.

I run the app and see a CPU load of 50% to 100% consistently, based on 600% full capacity.

However, after 3-4 hours of operation, the CPU utilization suddenly drops to around 10%. (It's hard to determine the exact cause because it's hard to monitor continuously during this time)

Additionally, I aware of some memory issues and are dealing with them, but they don't appear to be fatal leaks, so I relegated them to lower priority tasks for now.

At a crossroads, Should I prioritize code optimization, fix the small memory issue first, spend three to four hours of close monitoring, or perform a deeper understanding of the Mac system?

The analysis process through Profile in Instruments is also tricky in terms of catching the exact moment of a sudden change in CPU utilization.

I'd even be open to suggestions from iOS devs who might have any insights.

  • I'm also wondering why the thread count in the picture goes into the 3000s after 3-4 hours.

I'm also wondering if this is an issue with asynchronous processing.

Any advice would be greatly appreciated in the comments.

Before / After 3-4 hours, 1-2 hours occationally


Solution

  • https://developer.apple.com/forums/thread/737982?page=1#765567022 by: ngb

    you are probably having thread contention. they could be blocking each other. it is possible to overwhelm the system with too many threads. sometimes u get better utilisation with less threads. really u only want as many as you have cpu cores running at the same time otherwise they interrupt each other.

    u can look at the concurrency tool in instruments for some hints.