如何优化或限制 C# 中 zip 进程 (DotNetZip) 的 CPU 使用率?
大家好,
我有一个应用程序,我用它来使用 DotNetZip 库存档文件夹。我注意到,当它进入实际的“压缩”过程时,它会占用 100% 的 CPU。这个应用程序将与另一个应用程序(一个 TCP 聊天应用程序)结合使用,所以我真的需要它使用尽可能少的 CPU。
有什么办法可以安全地限制CPU吗?我尝试过降低优先级,但没有什么区别。我现在唯一能做的就是仅将亲和力设置为 1 个核心,以便它使用 50%。但当然这只适用于多核计算机。
Good day guys
I have an app that I use to archive a folder using the DotNetZip library. I notice that when it goes to the actual "zipping" process, it uses up 100% of the CPU. This app will be used in conjunction with another (a tcp chat application) so I really need this to use as less cpu as possible.
Is there any way I can safely limit the cpu? I've tried lowering the priority but it doesn't make a difference. The only thing I have right now is setting the affinity to 1 core only so that it uses 50%. But of course that would only work on multi-core computers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,DotNetZip 将在多个线程上运行,以提供更快的压缩,但会占用 CPU 和内存。在多核系统上,如果有足够的 I/O 吞吐量,这可能会消耗 100% 的 CPU。
如果您不想这样做,可以将 ZipFile.ParallelDeflateThreshold 设置为 -1。这表示“永远不要使用多线程来压缩”。这仍然会消耗单个线程可以获得的所有 cpu;在单核、单 CPU 机器上,这仍然是 100%。目前典型的笔记本电脑是双核机器;在这种情况下,它将显示 50% 的 cpu 使用率,因为一个核心将完全饱和。
如果您在多核计算机上运行,并且您希望您的 tcp 通信应用程序继续不受阻碍,您可以在以下位置启动 DotNetZip 工作:一个后台线程,并设置我上面提到的属性。为了获得更多隔离,除了设置并行阈值属性之外,您还可以将 DotNetZip 分解为单独的进程,并在该进程上设置亲和力 + 优先级。
DotNetZip will run on multiple threads by default, for delivering faster compression, at the expense of CPU and memory utilization. On a multi-core system, this can consume 100% of all of your CPUs, given sufficient I/O throughput.
If you don't want this, you can set the ZipFile.ParallelDeflateThreshold to -1. This says "never use multiple threads to compress". This will still consume all of the cpu a single thread can get; on a single-core, single-cpu machine, that will still be 100%. A typical current-issue laptop is a dual-core machine; it will exhibit 50% cpu usage in this case, because one core will be fully saturated..
If you are running on a multi-core machine, and you'd like your tcp communications app to continue unimpeded, you can start DotNetZip work in a background thread, and set the property I mentioned above. For more isolation you could factor out the DotNetZip into a separate process and set the affinity + priority on that process, in addition to setting the parellel threshold property.
降低优先级是您想要的,不一定强制进程仅使用任意数量的 CPU,例如 50%。
这意味着该进程将使用尽可能多的未使用 CPU 处理能力,并且其他更重要的进程仍然能够正常运行,就像 Zip 进程根本没有运行一样。
我应该认为低优先级进程只使用了 100% 的 CPU,因为没有其他进程。一旦您启动并运行其他进程并重试,您会发现 Zip 进程不会使用 100%。
Lowering the priority is what you want, not necessarily forcing the process to only use an arbitrary amount of the CPU like 50%.
This means that the process will use as much unused processing capacity of the CPU, and that your other more important processes will still be able to function almost as if the Zip process wasn't running at all.
I should think that the low priority process is only using 100% of the CPU because nothing else is. Once you get your other process(es) up and running and try it again, you will notice that the Zip process will not use 100%.
通过降低优先级,您正在做您需要做的事情。在适用时将亲和力设置为 1 也会有所帮助。
如果另一个进程需要 CPU,您的进程会很乐意让步,因此用户不会注意到速度减慢。 100% 使用 CPU 并没有什么问题,除非你霸占它。理论上,您希望您的应用程序在需要时使用 CPU,在不需要时放弃它。
By lowering the priority you are doing what you need to do. Setting the affinity to one will also help when applicable.
If another process needs CPU, your process will gladly step aside, so the user should not notice a slowdown. There is nothing wrong with using 100% of the CPU unless you are hogging it. In theory, you want your app to use CPU when it needs it, and give it up when it doesn't.