121

This is NOT a Beta issue. I am on Xcode 6.0.1, production release. The issue I am having is that when I try to do a Build or Run the code I am working on, Xcode becomes unresponsive for large periods of time and the SourceKitService consumes upwards of 400% of the CPU (according to Activity Monitor). This issue is new as of the last few days, although, oddly, I had been on Xcode 6.0 since it was officially released on Sept 17. I upgraded to 6.0.1 hoping it would contain a fix for this issue.

Any idea as to what the problem could be?

4
  • Have you checked memory consumption? I haven't had this issue for a while, but it was really bad in the betas wherein it would consume all RAM and then HCF. It was generally due to longer lines of arithmetic, especially with subscripts. You will need to divide and conquer to find the offending (but legal) code. When you find the line, please try to reproduce in Playground, and submit a bug report. Oct 1, 2014 at 22:56
  • Also see these admittedly dated posts: stackoverflow.com/questions/24873219/… and stackoverflow.com/questions/24873219/… Oct 1, 2014 at 22:58
  • There are still some known bugs as you can read in several threads on the Apple developer forums. Xcode 6.1 Beta 3 resolves the high CPU consumption but introduces different ones. Very disappointing.
    – Klaas
    Oct 2, 2014 at 12:38
  • 1
    I'm having a similar issue. I had the issue on Xcode 7 and now on 8. The only thing that changes is the code that comes into your Xcode. My guess is that reindexing or new code is root cause. Does this usually happen when you pull code from your upstream?
    – mfaani
    Nov 25, 2016 at 22:30

27 Answers 27

161

Ran into this problem with Xcode 6.1.1 earlier this afternoon (not beta, official released version). I had been running some code on Playground and was suspecting that to be the cause. CPU was pegged to nearly 100%, and Xcode was unable to complete builds.

So here's what I did:

1. Opened "Activity Monitor", which showed SourceKitService as the main CPU hog.

2. Within "Activity Monitor", double-clicked on the SourceKitService and clicked on "Open Files and Ports" section, which showed it was working on files under the /Users/myname/Library/Developer/Xcode/DerivedData/ModuleCache/ directory for a specific folder.

3. Deleted the specified folder (from a command-line, using rm -rf). The cache is regenerated based on Can I safely delete contents of Xcode Derived data folder? .

4. Using Activity Monitor again, Force-Quit SourceKitServer. Saw the now-all-too-familiar sign within Xcode saying that SourceKitService had crashed (so that's why SourceKitService sounded familiar!).

5. Repeated step 3.

The Mac is peaceful, again. No data was lost and Xcode didn't even have to be restarted (which I had tried unsuccessfully). Bottom line is that ModuleCache seems to be getting SourceKitService in a loop and deleting the folder seems to fix it. Hope this works for you too.

Bootnote:

By the way, the cause for SourceKitService issue was that I had too long an array declaration in my Swift class. I had over 200 entries in an array. Reduced it to 30 and the error went away. So the issue may have arisen due to some kind of stack overflow in apple code (pun intended).

10
  • Thanks for your answer. You should edit you answer instead of commenting on your own though.
    – Axalo
    Jan 28, 2015 at 2:46
  • 3
    I had a similar problem with a long array declaration in Swift that was relying on type inference during its initialization. I found that by explicitly annotating the type resolved the problem.
    – jay492355
    Mar 18, 2015 at 17:51
  • 2
    Same problem. big array with dictionaries. I just put all the data in a .PLIST file and read it. Sep 11, 2015 at 13:46
  • 70
    Does it bother anyone else that in 2016 we can't handle a 200-element array? I used longer arrays in BASIC on an Atari 600 back in the 80s. May 19, 2016 at 13:09
  • 2
    I had the same problem now and, as @jay492355 mentioned, you need to explicitly type your array.
    – Guy Kogus
    Aug 16, 2017 at 8:51
25

I was seeing the problem because I was declaring an array with about 60 elements that looked like this:

let byteMap = [

["ECG" : (0,12)],
["PPG" : (12,3)],
["ECG" : (15,12)],
["PPG" : (27,3)],
["ECG" : (30,12)]

By explicitly annotating the type like this:

let byteMap : [String: (Int, Int)] = [

["ECG" : (0,12)],
["PPG" : (12,3)],
["ECG" : (15,12)],
["PPG" : (27,3)],
["ECG" : (30,12)],

I was able to make it stop. I think it must have something to do with Swift's type-inference and type-checking that makes it go into a loop when it encounters a longish array.

This was in Xcode 6.2. I also deleted the ModuleCache as described above and now everything is good.

4
  • 2
    Yeah, this problem is famous spin.atomicobject.com/2016/04/26/swift-long-compile-time
    – onmyway133
    Jun 24, 2016 at 13:01
  • 1
    Similar problem for me on Xcode 8.1. I have an array of NSConstraintLayout objects. Worked fine with 4. Works OK with 6. Not so good with 7 and doesn't work at all with 8. I created two arrays with 4 objects each and it works fine. Nov 7, 2016 at 22:44
  • @onmyway133 I wonder why it doesn't happen all the time and it only happens occasionally
    – mfaani
    Nov 25, 2016 at 22:00
  • Do you think if you had something like return ["a", "b", "c", "d", "e", "f"] in a function that returns [String] that it would still have trouble with the type-inference?
    – shim
    Jun 15, 2017 at 3:35
11

This problem happened like 10 times, 8 times it happened when I connected an actual device and didn't run through simulator.

I am not so sure if my solution is a good one, but for me I believe the problem was due to switching between simulator and an actual device. It may sound weird but it was as if it was creating interference between cache files.

What solved my problem:

  • Clean Build Folder:( on Xcode) Alt + Shift + Command + K
  • Reset Content and Settings: (on Simulator) Command + Shift + K.
  • Waited a bit longer than normal and overload Xcode with constant clicks

So basically before you try to run on any new device, just delete any cache.

EDIT

I just had the problem without any device connection. I just quit Xcode and opened it again and the problem was gone. Not sure my guess is it could be some re-indexing issue after you fetch/pull merge new code.

2
  • I really wished this worked cus I'm desperate. Unfortunately, it starts spinning out of control again moments after I start coding.
    – Mr. T
    Dec 3, 2016 at 12:51
  • I'm not sure but sometimes just switching branches, pulling from upstream has solved my problem. My point is I don't do what the accepted answer is suggesting and yet my problem is solved somehow I haven't yet cracked :/
    – mfaani
    Dec 3, 2016 at 14:18
5

The problem still occurs in XCode 10.0. You can fix it by disabling "Show Source Control changes" in Source Control options.

enter image description here

1
  • Cool, but less than necessary if its grinding Xcode to a halt.
    – Shayne
    Jul 18, 2019 at 3:59
4

I resolved another issue that was causing SourceKitService use up to 13GB of memory...

I had String(format line with lots of arguments:

return String(format: "%d,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f", samples.count,sum1.x,sum1.y,sum1.z,sum1.rx,sum1.ry,sum1.rz,sum2.x,sum2.y,sum2.z,sum2.rx,sum2.ry,sum2.rz,sum3.x,sum3.y,sum3.z,sum3.rx,sum3.ry,sum3.rz)

when replaced with this it worked fine (no memory build up and normal CPU consumption)

    var output: String = ""

    output += String(format: "%d,", samples.count)
    output += String(format: "%.3f,%.3f,%.3f,", sum1.x, sum1.y, sum1.z)
    output += String(format: "%.3f,%.3f,%.3f,", sum1.rx, sum1.ry, sum1.rz)
    output += String(format: "%.3f,%.3f,%.3f,", sum2.x, sum2.y, sum2.z)
    output += String(format: "%.3f,%.3f,%.3f,", sum2.rx, sum2.ry, sum2.rz)
    output += String(format: "%.3f,%.3f,%.3f,", sum3.x, sum3.y, sum3.z)
    output += String(format: "%.3f,%.3f,%.3f", sum3.rx, sum3.ry, sum3.rz)

    return output
1
  • 4
    How did you figure out that this piece of code was the issue?
    – K.K
    Jan 19, 2018 at 6:33
3

I spend 4 hours to figure out problems in a long compilation of my project. The first try takes 42 min to compile.

I clear all cache from /Users/myname/Library/Developer/Xcode/DerivedData/ModuleCache/ as was suggested by @LNI, after restart SourceKitService and apply few changes for code:

1) To

    var initDictionary:[String:AnyObject] = [
                    "details" : "",
                    "duration" : serviceDuration,
                    "name" : serviceName,
                    "price" : servicePrice,
                    "typeId" : typeID,
                    "typeName" : typeName,
                    "url" : "",
                    "serviceId" : serviceID,
                    "imageName" : ""
                ]

From

    var initDictionary= [
                    "details" : "",
                    "duration" : serviceDuration,
                    "name" : serviceName,
                    "price" : servicePrice,
                    "typeId" : typeID,
                    "typeName" : typeName,
                    "url" : "",
                    "serviceId" : serviceID,
                    "imageName: "" ]

2) To

            if let elem = obj.property,
                let elem2 = obj.prop2,
                etc
                 {
                 // do stuf here
            }

From

           let value1 = obj.property ?? defaultValue

3)

To

           let serviceImages = images.filter { $0.serviceId == service.id }
           let sorted = serviceImages.sort { $0.sort > $1.sort }

From

            let serviceImages = images.filter { $0.serviceId == service.id }. sort { $0.sort > $1.sort }

As result compile time - 3 min, not so fast but better for 42 min.

As result, before SourceKitService - take ~5,2Gb of memory and after ~0.37Gb

enter image description here

1
  • I doubt step 2. That's a nil coalescing operator; if it can create such issues why the compiler suggests it in the first place? Sep 18, 2020 at 12:50
3

I had the same problem with SourceKitService.

I solved. NEVER ADD SUBVIEWS WITH FOR LOOP.

To detect issue I use: https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode

5
  • how did you solve? I added my subviews with for loop and now cleaning cache does not fix @Zhanserik
    – 10donovanr
    Dec 10, 2019 at 1:39
  • 1
    @10donovanr NEVER ADD SUBVIEWS WITH FOR LOOP. After that try clean app cache with CMD+SHITF+K
    – Janserik
    Dec 10, 2019 at 6:49
  • How would you add a list of views without a for loop? You mean the problem is the for or any loop? I mean, that it won't happen if I use while?
    – sebasira
    Apr 1, 2022 at 1:01
  • @sebasira If you use UIKit, you should not add UI elements via any loop. On SwiftUI, you can.
    – Janserik
    Apr 25, 2022 at 16:17
  • Oh! Ok, I did not understand it that way. I was talking about SwiftUI. Thanks for clarification.
    – sebasira
    Apr 26, 2022 at 2:21
3

I've been running into this issue with Xcode 9, and explored several solutions. For me, disabling Source Control seemed to do the trick.

Xcode -> Preferences -> Source Control -> uncheck "Enable Source Control"

If this doesn't work, I would recommend using the renice command at the terminal. More on that here

disabling Source Control

Other steps that I attempted, but did not help:

  1. Close Xcode -> Delete Derived Data
  2. cycling machine
  3. "clean" project
3

https://www.logcg.com/en/archives/2209.html

SourceKitService took charge of Swift's type inference work.

private lazy var emojiFace = ["?", "?", "?", "?"]

change to explicitly type

private lazy var emojiFace:[String] = ["?", "?", "?", "?"]

SourceKitService CPU usage immediately drop down。

2
  • That's it! and it perfectly worked for me .. thanks! Dec 16, 2020 at 23:07
  • to explain my comment: I had this statement: "public let RKIGraphNeededWidth: CGFloat = round((min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) - (10.0 * 2)) * 0.32)" without the explicit ":CGFloat" ... and that drove xCode mad. Adding just that explicit casting calms it down and is now working very smoothly! Thanks lbsweek, great job! Dec 17, 2020 at 18:39
2

For me it worked to delete the Derived Data. Select 'Product' from the menu and hold the Alt-key and select 'Clean Build Folder'. Shortkey: Alt + Shift + Command + K

2
  1. Quit Xcode
  2. Run in Terminal:

rm -rf ~/Library/Developer/Xcode/DerivedData/ModuleCache/*


Note the difference between LNI's accepted answer and this one:

  1. It's always better not to crash than to crash. Especially, when it comes to Xcode processes/components.
  2. I'm not an Apple developer, but partial deleting the cache can break its integrity. I didn't notice any significant delays after cleaning all the cache.
2

Don't create dictionary in swift without specifying data types or with [String:Any]

If we use 'Any' type the compiler might run into an infinite loop for checking the data type.

It won't create any compiling error, it will make our mac to freeze at 'compiling swift source files' with acquiring much memory for the tasks named 'swift' & 'SourceKitService'.

2

I ran into something similar combining multiple ?? operators to provide a default for optional string values.

I was experimenting with the debug code below when the fan on my trusty mid-2010 MacBook Pro began running hard. SourceKitService was sucking up every CPU cycle it could get. Commenting and uncommenting the offending line made it very clear what SourceKitService was choking on. It looks like using more than one ?? operator to provide a default is an issue on an old machine. The work around is just don't do it. Break it up into multiple assignments which makes some ugly debug code even uglier.

placeMark is an instance of CLPlacemark. The properties used here return optional strings.

I was using Xcode Version 8.3.2 (8E2002) running on OS 10.12.4 (16E195)

// one term is not an issue
let debugString1 = (placeMark.locality ?? "")

// two terms pushes SourceKitService CPU use to 107% for about 60 seconds then settles to 0%
let debugString1 = (placeMark.locality ?? "")  + ", " +  (placeMark.administrativeArea ?? "") 

// three terms pushes SourceKitService CPU use to 187% indefinitely 
let debugString1 = (placeMark.locality ?? "")  + ", " +  (placeMark.administrativeArea ?? "")  + (placeMark.postalCode ?? "")

// ugly but it's safe to use
var debugString1 = placeMark.locality ?? ""
debugString1 = debugString1 + ", " +  (placeMark.administrativeArea ?? "")
debugString1 = debugString1 + " " + (placeMark.postalCode ?? "")
1
  • 1
    I believe this is an issue with string concatenation and not ??. It'd be worth trying with "\() \()" (string interpolation) instead Jan 9, 2019 at 1:33
2

Converting long Arrays to Functions seem to resolve the problem for me:

var color: [UIColor] {
    return [
        UIColor(...),
        UIColor(...),
        ...
    ]
}

to:

func color() -> [UIColor] {
    return [
        UIColor(...),
        UIColor(...),
        ...
    ]
}
1
2

I have faced such an issue. Source kit service was using 10 gb of usage. Swift process in activity monitor reaches over 6 GB usage. I was using following code:

var details : [String : Any] = ["1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "11":11, "12":12, "13":13, "14":14, "15":15, "16":16]

I have changed code to following to solve this issue:

var details : [String : Any] = [:]

details["1"] = 1

details["2"] = 2

details["3"] = 3

details["4"] = 4

details["5"] = 5

details["6"] = 6

details["7"] = 7

details["8"] = 8

details["9"] = 9

details["10"] = 10

details["11"] = 11

details["12"] = 12

details["13"] = 13

details["14"] = 14

details["15"] = 15

details["16"] = 16

1
  • Man.... who would have thought.... I exactly had similar code and had SourceKitService sucking the life out the CPU. Changing the code just made it go away. Oct 24, 2017 at 22:25
1

Faced the same issue on Xcode 7.2 (7C68)

The solution was to implement a method of a protocol, which my class had in the definition.

1

This is still an issue in xcode Version 7.3.1 (7D1014) the cause for me was, like LNI pointed it out, a too long array, not so long actually. I fixed my problem by breaking the array into various arrays like this:

let firstLevel = [
            [1, 0, 1, 0, 1],
            [0, 0, 0, 0, 0],
            [1, 0, 1, 0, 1],
            [0, 0, 0, 0, 0],
            [1, 0, 1, 0, 1],
            [0, 0, 0, 0, 0]
        ]
        let secondLevel = [
            [0, 0, 0, 0, 0],
            [0, 1, 0, 1, 0],
            [0, 0, 0, 0, 0],
            [0, 1, 0, 1, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0]
        ]
        let thirdLevel =     [
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 1, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0]
        ]
        let map = [firstLevel, secondLevel, thirdLevel]
1

I had the same problem with XCode 8.2.1 (8C1002) and the following code:

import UIKit
import AVFoundation
import Photos
import CoreMotion
import Foundation


class TestViewController: UIViewController
{
    let movieFileOutput = AVCaptureMovieFileOutput()


var anz_total_frames = 0, anz_total_miss = 0

@IBOutlet weak var tfStatistics: UITextView!


func showVideoStatistics()
{
    let statisticText:String =             "frames: \(self.anz_total_frames)" + String.newLine +

        "frames/s: \(self.anz_total_frames / self.movieFileOutput.recordedDuration.seconds)" + String.newLine +

        "miss: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
    "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine +
        "nicht erkannt: " + formatText4FramesPercent(self.anz_total_miss) + String.newLine


    self.tfStatistics.text = statisticText
}

func formatText4FramesPercent(_ anz:Int) -> String
    {
        let perc = Double(anz)*100.0/Double(anz_total_frames)
        return String(perc.format(".1") + "%")
    }
}

and these extensions:

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }

    static var newLine: String {
        return "\r\n"
    }
}

extension Int {
    func format(_ f: String) -> String {
        return String(format: "%\(f)d", self)
    }
}

extension Double {
    func format(_ f: String) -> String {
        return String(format: "%\(f)f", self)
    }
}

I solved it by commenting this line in TestViewController:

        "frames/s: \(self.anz_total_frames / self.movieFileOutput.recordedDuration.seconds)" + String.newLine +

Took me more than an hour to find it, I hope a can save some time of somebody else. I filed a bug report to Apple with number 30103533

1

I was facing the same problem after migrating the project to swift 3, find out solution it was taking time because of dictionaries and array created without data type.

1

This behavior appeared in my project when I accidentally declared a class that inherited from itself. Xcode 8.2.1, using Swift 3.

1

I also had this issue, in my case I was declaring a big array like this:

var myArray: [(String, Bool?)]?
myArray = [("someString", someBool),
("someString", someBool),
("someString", someBool),
("someString", someBool),
("someString", someBool)
.
.
("someString", someBool)]

I solved the problem by adding the items 1 per line instead of all at the same time:

var myArray = [(String, Bool?)]()
myArray.append(("someString", someBool))
myArray.append(("someString", someBool))
myArray.append(("someString", someBool))
myArray.append(("someString", someBool))
myArray.append(("someString", someBool))
.
.
.

this fixed the problem.

1

For Objective-C projects:

I had the same problem, and there's zero Swift code in our project, so it wasn't the type inference checker.

I tried every other solution here and nothing worked - what FINALLY fixed it for me was rebooting the computer in recovery mode and running the disk repair. I can finally work in peace again!

I'm guessing that it happened because of some broken symlinks, probably pointing towards each other and making the service run around in an endless loop.

1

I'm having a similar issue with Xcode 8.2.1 - with a section of 1,000+ lines of code commented-out via /* */. Commenting-out the section caused the issue, and removing the commented-out code fixed it.

1

run in terminal:

killall Xcode
rm -rf ~/Library/Developer/Xcode/DerivedData/ModuleCache
open /Applications/Xcode.app

you could also create a terminal command using this alias:

echo alias xcodeFix='killall Xcode;rm -rf ~/Library/Developer/Xcode/DerivedData/ModuleCache;open /Applications/Xcode.app' >> ~/.profile
source ~/.profile

and then just run

xcodeFix
1

Happened to me on XCode 11.4.1 when calling @dynamicMemberLookup subscripts inside a SwiftUI @ViewBuilder block.

1
  • I have faced similar issue in XCode 11.6. Created observer class. which was trying to broadcast (publish) 'optional' elements. I was observing AVPlayer (optional) status in the class. The SourceKitService was consuming full memory, so Mac was like hanged. I modified and removed 'optional' elements. May be XCode will solve the bug in next version. We may keep "Activity Monitor" of Application -> Utilities to monitor memory while development :)
    – SHS
    Aug 21, 2020 at 13:52
0

I had the same issue and it was caused by a programming error.

In my case I was implementing the comparable and equatable protocols and the lhs.param and rhs.param did not correspond with parameters of the lhs and rhs classes.

0

This just happened to me in XCode 14.2 when I used .1 for a floating point constant instead of 0.1. The preview got wedged as I corrected the value. I selected Project / Clean Build Folder and within 10 seconds, the fan stopped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.