SourceKitService Consumes CPU and Grinds Xcode to

2019-01-30 02:07发布

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?

24条回答
ゆ 、 Hurt°
2楼-- · 2019-01-30 02:15

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-30 02:16

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

查看更多
趁早两清
4楼-- · 2019-01-30 02:17

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

查看更多
你好瞎i
5楼-- · 2019-01-30 02:19

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.

查看更多
男人必须洒脱
6楼-- · 2019-01-30 02:20

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.

查看更多
甜甜的少女心
7楼-- · 2019-01-30 02:21

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
查看更多
登录 后发表回答