ios SourceKitService 消耗 CPU 并使 Xcode 停止运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26151954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:57:37  来源:igfitidea点击:

SourceKitService Consumes CPU and Grinds Xcode to a Halt

iosiphoneswiftxcodesourcekit

提问by zeeple

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.

这不是 Beta 版问题。我在 Xcode 6.0.1,生产版本。我遇到的问题是,当我尝试构建或运行我正在处理的代码时,Xcode 在很长一段时间内都没有响应,并且 SourceKitService 消耗了超过 400% 的 CPU(根据活动监视器)。这个问题是最近几天的新问题,但奇怪的是,自从 Xcode 6.0 于 9 月 17 日正式发布以来,我一直在使用它。我升级到 6.0.1,希望它包含针对此问题的修复程序。

Any idea as to what the problem could be?

关于问题可能是什么的任何想法?

回答by LNI

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.

今天下午早些时候在 Xcode 6.1.1 上遇到了这个问题(不是测试版,正式发布的版本)。我一直在 Playground 上运行一些代码,并怀疑这是原因。CPU 接近 100%,而 Xcode 无法完成构建。

So here's what I did:

所以这就是我所做的:

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

1. 打开“活动监视器”,显示 SourceKitService 是主要的 CPU 猪。

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.

2. 在“活动监视器”中,双击 SourceKitService 并单击“打开文件和端口”部分,这表明它正在处理 /Users/myname/Library/Developer/Xcode/DerivedData/ModuleCache/ 目录下的文件对于特定文件夹。

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?.

3. 删除指定的文件夹(从命令行,使用 rm -rf)。缓存是根据Can I safe delete contents of Xcode Derived data重新生成的.

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!).

4. 再次使用活动监视器,强制退出 SourceKitServer。在 Xcode 中看到现在非常熟悉的标志,说 SourceKitService 已经崩溃(这就是 SourceKitService 听起来很熟悉的原因!)。

5. Repeated step 3.

5. 重复步骤 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.

Mac 再次安静下来。没有数据丢失,甚至不必重新启动 Xcode(我曾尝试不成功)。底线是 ModuleCache 似乎在循环中获取 SourceKitService 并且删除文件夹似乎可以解决它。希望这对你也有用。

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).

顺便说一下,SourceKitService 问题的原因是我的 Swift 类中有太长的数组声明。我在一个数组中有 200 多个条目。将其减少到 30,错误就消失了。所以这个问题可能是由于苹果代码中的某种堆栈溢出(双关语)而出现的。

回答by jay492355

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

我看到了这个问题,因为我声明了一个包含大约 60 个元素的数组,如下所示:

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.

我能够让它停止。我认为它一定与 Swift 的类型推断和类型检查有关,这使得它在遇到较长的数组时进入循环。

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

这是在 Xcode 6.2 中。我也如上所述删除了 ModuleCache ,现在一切都很好。

回答by Honey

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

这个问题发生了 10 次,当我连接实际设备并且没有通过模拟器运行时发生了 8 次。

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
  • 清理构建文件夹:(在 Xcode 上)Alt + Shift + Command + K
  • 重置内容和设置:(在模拟器上)Command + Shift + K
  • 等待的时间比平时长一点,并且不断点击使 Xcode 超载

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 guessis it could be some re-indexing issue after you fetch/pull merge new code.

我只是在没有任何设备连接的情况下遇到了问题。我刚刚退出Xcode并再次打开它,问题就消失了。不确定我的猜测是否在您获取/拉取合并新代码后可能是一些重新索引问题。

回答by Matej Ukmar

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

我解决了另一个导致 SourceKitService 使用高达 13GB 内存的问题......

I had String(format line with lots of arguments:

我有 String(format 行,有很多参数:

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)

当替换为它时它工作正常(没有内存积聚和正常的 CPU 消耗)

    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

回答by mhit0

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

我一直在使用Xcode 9遇到这个问题,并探索了几种解决方案。对我来说,禁用源代码管理似乎可以解决问题。

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

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

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

如果这不起作用,我建议在终端使用renice命令。更多关于这里

disabling Source Control

禁用源代码管理

Other steps that I attempted, but did not help:

我尝试过的其他步骤,但没有帮助:

  1. Close Xcode -> Delete Derived Data
  2. cycling machine
  3. "clean" project
  1. 关闭 Xcode -> 删除派生数据
  2. 自行车机
  3. “清洁”项目

回答by DennyDog

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

这个问题在 XCode 10.0 中仍然存在。您可以通过在源代码管理选项中禁用“显示源代码管理更改”来修复它。

enter image description here

在此处输入图片说明

回答by Roland Keesom

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

对我来说,它可以删除派生数据。从菜单中选择“产品”并按住 Alt 键并选择“清理构建文件夹”。快捷键:Alt + Shift + Command + K

回答by Dmitry Isaev

  1. Quit Xcode
  2. Run in Terminal:
  1. 退出 Xcode
  2. 在终端运行:

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

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



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

请注意 LNI 接受的答案与此答案之间的区别:

  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.
  1. 不崩溃总是比崩溃好。特别是,当涉及到 Xcode 进程/组件时。
  2. 我不是 Apple 开发人员,但部分删除缓存会破坏其完整性。清除所有缓存后,我没有注意到任何明显的延迟。

回答by gbk

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

我花了 4 个小时来找出我项目的长编译中的问题。第一次尝试编译需要 42 分钟。

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

/Users/myname/Library/Developer/Xcode/DerivedData/ModuleCache/重新启动后,我按照@LNI 的建议清除了所有缓存,SourceKitService并对代码应用了一些更改:

1) To

1) 到

    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

2) 到

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

From

           let value1 = obj.property ?? defaultValue

3)

3)

To

           let serviceImages = images.filter { 
            let serviceImages = images.filter { ##代码##.serviceId == service.id }. sort { ##代码##.sort > .sort }
.serviceId == service.id } let sorted = serviceImages.sort { ##代码##.sort > .sort }

From

##代码##

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

结果编译时间 - 3 分钟,不是那么快,但 42 分钟更好。

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

结果,之前SourceKitService- 占用 ~5,2Gb 的内存,之后 ~0.37Gb

enter image description here

在此处输入图片说明

回答by Zhanserik

I had the same problem with SourceKitService.

我在 SourceKitService 上遇到了同样的问题。

I solved. NEVER ADD SUBVIEWS WITH FOR LOOP.

我解决了。永远不要用 for 循环添加子视图。

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

为了检测我使用的问题:https: //github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode