TuningFork icon indicating copy to clipboard operation
TuningFork copied to clipboard

App is constantly at 100% and over CPU usage

Open Pranoy1c opened this issue 8 years ago • 3 comments

App is constantly at 100% and over CPU usage. Any way to reduce this?

Pranoy1c avatar Aug 31 '17 16:08 Pranoy1c

@Pranoy1c are you sure that you use lib in right way?

asieiev avatar Oct 12 '18 11:10 asieiev

@Pranoy1c the problem is with this line: DispatchQueue.main.async { d.tunerDidUpdate(self, output: output) }

Probably dispatching on main queue is causing strong reference cycles. When I removed "DispatchQueue.main.async", it was working fine.

RamyRizkalla avatar May 13 '19 22:05 RamyRizkalla

@RamyRizkalla actually problem in another line of code.

public func start() {
    microphone.start()
    tracker.start()
    AudioKit.output = silence 
    AudioKit.start()
    if timer == nil {
      timer = DispatchTimer(interval: 0.03, closure: { (t, i) -> Void in
        if let d = self.delegate {
          if self.tracker.amplitude > self.threshold {
            let amplitude = self.tracker.amplitude
            let frequency = self.smooth(self.tracker.frequency)
            let output = Tuner.newOutput(frequency, amplitude)
            DispatchQueue.main.async {
              d.tunerDidUpdate(self, output: output)
            }
          }
        }
      })
    }
    
    timer?.start(true)
  }

In method start() replace line timer = DispatchTimer(interval: 0.03, closure: { (t, i) -> Void in with line timer = DispatchTimer(interval: 0.03, closure: { [weak self] (t, i) -> Void in

You should care about weak references in closures.

asieiev avatar May 14 '19 07:05 asieiev