简体   繁体   English

一段时间后dispatch_queue_t变慢

[英]dispatch_queue_t slows down after some time

I have a question about custom DispatchQueue . 我对自定义DispatchQueue有疑问。

I created a queue and I use it as a queue for captureOutput: method. 我创建了一个队列,并将其用作captureOutput:方法的队列。 Here's a code snippet: 这是一个代码片段:

//At the file header
private let videoQueue = DispatchQueue(label: "videoQueue")

//setting queue to AVCaptureVideoDataOutput
dataOutput.setSampleBufferDelegate(self, queue: videoQueue)

After I get the frame, I'm doing an expensive performance with it and I'm doing it for each frame. 拿到框架后,我将对其进行昂贵的性能测试,并针对每个框架进行此操作。

When I launch the app, my expensive performance takes to 17 ms to compute and thankfully to that I have around 46-47 fps. 当我启动该应用程序时,我昂贵的性能需要花17毫秒才能计算出来,幸运的是,我的速度约为46-47 fps。 So far so good. 到现在为止还挺好。

But after some time (around 10-15 seconds), this expensive performance starts taking more and more time and in 1-2 minute I end up with 35-36 fps, and instead of 17 ms I have around 20-25 ms. 但是,经过一段时间(大约10-15秒),这种昂贵的性能开始花费越来越多的时间,并且在1-2分钟内我以35-36 fps的速度结束拍摄,而不是17毫秒,而我只有大约20-25毫秒。

Unfortunately, I can't provide the code of expensive performance because there's a lot of it and at least XCode tells that I do not have any memory leaks. 不幸的是,我无法提供性能昂贵的代码,因为有很多代码,至少XCode告诉我没有任何内存泄漏。

I know that manually created DispatchQueue doesn't really work in its own because all tasks I put there eventually end up in iOS default thread pool (I'm talking about BACKGROUND , UTILITY , USER_INTERACTIVE , etc). 我知道,手动创建DispatchQueue并没有真正在自己的工作,因为我放在那里,最终全部任务的iOS默认的线程池结束了(我说的BACKGROUNDUTILITYUSER_INTERACTIVE等)。 And for me it looks like videoQueue looses a priority with some period of time. 对我来说, videoQueue在一段时间内失去了优先级。

If my guess is right - is there any way to influence that? 如果我的猜测是正确的-有什么方法可以影响这一点? The performance of my DispatchQueue is very crucial and I want to give it the highest priority all the time. 我的DispatchQueue的性能非常关键,我想一直给它最高的优先级。

If I'm not right, I would very much appreciate if someone can give me a direction I should investigate. 如果我做错了,如果有人可以给我指导我应该进行调查,我将不胜感激。 Thanks in advance! 提前致谢!

First, I would suspect other parts of your code, and probably some kind of memory accumulation. 首先,我会怀疑代码的其他部分,以及可能的某种内存累积。 I would expect you're either reprocessing some portion of the same data over and over again, or you're doing a lot of memory allocation/deallocation (which can lead to memory fragmentation). 我希望您要么一遍又一遍地重新处理同一数据的某些部分,要么您要进行大量的内存分配/重新分配(这可能导致内存碎片)。 Lots of memory issues don't show up as "leaks" (because they're not leaks). 许多内存问题不会显示为“泄漏”(因为它们不是泄漏)。 The way to explore this is with Instruments. 探索此问题的方法是使用Instruments。

That said, you probably don't want to run this at the default QoS. 就是说,您可能不想在默认QoS下运行它。 You shouldn't think of QoS as "priority." 您不应将QoS视为“优先级”。 It's more complicated than that (which is why it's called "quality-of-service," not "priority"). 比这要复杂得多(这就是为什么它被称为“服务质量”,而不是“优先级”)。 You should assign work to a queue whose QoS matches the how it impacts the user. 您应该将工作分配给其QoS与它如何影响用户相匹配的队列。 In this case, it looks like you are updating the UI in real-time. 在这种情况下,您似乎正在实时更新UI。 That matches the .userInteractive QoS: 匹配.userInteractive QoS:

private let videoQueue = DispatchQueue(label: "videoQueue", qos: .userInteractive)

This may improve things, but I suspect other problems in your code that Instruments will help you reveal. 这可能会有所改善,但是我怀疑您的代码中的其他问题可以帮助您发现Instruments。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM