简体   繁体   English

如何优化或限制 c# 中 zip 进程 (DotNetZip) 的 CPU 使用率?

[英]How can I optimize or limit the CPU usage a zip process (DotNetZip) in c#?

Good day guys美好的一天,伙计们

I have an app that I use to archive a folder using the DotNetZip library.我有一个应用程序,用于使用 DotNetZip 库归档文件夹。 I notice that when it goes to the actual "zipping" process, it uses up 100% of the CPU.我注意到,当它进入实际的“压缩”过程时,它会占用 100% 的 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.这个应用程序将与另一个应用程序(tcp 聊天应用程序)一起使用,所以我真的需要它来尽可能少地使用 CPU。

Is there any way I can safely limit the cpu?有什么办法可以安全地限制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%.我现在唯一拥有的是将亲和力设置为 1 个核心,以便它使用 50%。 But of course that would only work on multi-core computers.但当然,这只适用于多核计算机。

DotNetZip will run on multiple threads by default, for delivering faster compression, at the expense of CPU and memory utilization.默认情况下,DotNetZip 将在多个线程上运行,以提供更快的压缩,但会牺牲 CPU 和 memory 利用率。 On a multi-core system, this can consume 100% of all of your CPUs, given sufficient I/O throughput.在多核系统上,如果有足够的 I/O 吞吐量,这可能会消耗所有 CPU 的 100%。

If you don't want this, you can set the ZipFile.ParallelDeflateThreshold to -1.如果您不想这样做,可以将 ZipFile.ParallelDeflateThreshold 设置为 -1。 This says "never use multiple threads to compress".这说“永远不要使用多个线程来压缩”。 This will still consume all of the cpu a single thread can get;这仍然会消耗单个线程可以获得的所有 cpu; on a single-core, single-cpu machine, that will still be 100%.在单核、单 CPU 机器上,仍然是 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..在这种情况下,它将表现出 50% 的 cpu 使用率,因为一个核心将完全饱和。

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.如果您在多核机器上运行,并且希望您的 tcp 通信应用程序继续畅通无阻,您可以在后台线程中启动 DotNetZip 工作,并设置我上面提到的属性。 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.为了获得更多隔离,您可以将 DotNetZip 分解为一个单独的进程,并在该进程上设置关联性 + 优先级,此外还可以设置并行阈值属性。

Lowering the priority is what you want, not necessarily forcing the process to only use an arbitrary amount of the CPU like 50%.降低优先级是你想要的,不一定强制进程只使用任意数量的 CPU,比如 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.这意味着该进程将使用尽可能多的未使用的 CPU 处理能力,并且您的其他更重要的进程仍然能够 function 几乎就像 Zip 进程根本没有运行一样。

I should think that the low priority process is only using 100% of the CPU because nothing else is.我应该认为低优先级进程只使用 100% 的 CPU,因为没有别的。 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%.一旦您启动并运行其他进程并再次尝试,您会注意到 Zip 进程不会使用 100%。

By lowering the priority you are doing what you need to do.通过降低优先级,您正在做您需要做的事情。 Setting the affinity to one will also help when applicable.在适用时,将亲和力设置为 1 也会有所帮助。

If another process needs CPU, your process will gladly step aside, so the user should not notice a slowdown.如果另一个进程需要 CPU,您的进程将很乐意让步,因此用户不会注意到速度变慢。 There is nothing wrong with using 100% of the CPU unless you are hogging it.除非你占用它,否则使用 100% 的 CPU 没有任何问题。 In theory, you want your app to use CPU when it needs it, and give it up when it doesn't.理论上,您希望您的应用程序在需要时使用 CPU,并在不需要时放弃。

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

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