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

Raphael picture Raphael · Apr 29, 2011 · Viewed 8k times · Source

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.

Answer

Cheeso picture Cheeso · May 7, 2011

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.