开发者

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 开发者_JAVA技巧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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜