How can you activate a long exe command line to avoid CPU binding?

I have an exe command line written in C #. It is a log parser that processes log files of a few GB each night.

Shows the processor 100%. Not ideal.

How could I "throttle" the application? This can take as much time as it wants, but I would prefer it to use the least CPU.

Is there any coding method or theory to get the EXE to slow down and take up less CPU? I can’t do anything in the environment in which it works (for example, change the priority of the process), so any changes should be in the code of the application itself.

+4
source share
6 answers

You can prioritize threads using the Thread.Priority property.

Of course, if you don’t try to start anything else, you will still use a 100% processor, but I think in this case you do not mind.

Does your application create any other threads or use a thread pool? If so, you probably want to make sure that these threads also have lower priority.

EDIT: setting the overall priority of the process is probably the best solution overall, but I will leave this as an alternative.

+6
source

Try the following:

 using System.Diagnostics; Process myProc = Process.GetCurrentProcess(); myProc.PriorityClass = ProcessPriorityClass.BelowNormal; 

If it's not low enough, there is a ProcessPriorityClass.Idle

MSDN Link 1
MSDN Link 2

+8
source

You can also introduce slow sleep into the main loop that binds the processor.

For example, this is often done by game loops - calling Thread.CurrentThread.Sleep(1); once per frame will force most games to use a 100% processor to use a 1-2% processor and still allow reasonable access.

----- Edit ------

There are potential advantages to hibernation while lowering the priority of a process or thread. The main two:

  • Sleep mode allows you to control how much CPU time you are giving up. If you lower your priority, and there are other processes of processor starvation, you can refuse more processor time than you want. [This is why I did not do Sleep (0) either, since this does not always refuse CPU time, although it does refuse some. I did this on embedded systems with longer dreams to reduce CPU usage.]
  • If your goal is to reduce overall CPU usage, lowering the priority of the process will not help if nothing works. You will still have a 100% processor. This can potentially be useful if you are trying to reduce power consumption (save energy costs) or try to reduce heat depending on the architecture of your system.

----- From the original -----

For other style apps (UIs), another option is to reorganize your procedure as asynchronous, partition your processing and process it in blocks in a call, subscribing to something like Application.OnApplicationIdle. This may allow your user interface to remain somewhat responsive, as it will work in pins.

If this is a mathematical procedure, and it performs a lot of calculations, then reducing the priority of the thread and the ability to use most of the available processors may be better.

+6
source

Since you said that you cannot change the priority of the process, the only option is to insert artificial delays, as indicated in the previous answer. If you insert Thread.Sleep (1) into your write loop, then at most you will process 1000 records per second. If you need something more granular, then you will need to insert a sleeping every N entries.

+4
source

try

 Process thisProc = Process.GetCurrentProcess(); thisProc.PriorityClass = ProcessPriorityClass.BelowNormal;//Or ProcessPriorityClass.Idle 
+3
source

If a program spends a lot of time processing file data, then you should consider asynchronous I / O. You can also divide the program into several sections / several threads that are scheduled with pauses between them.

0
source

All Articles