-5

I would like to know if this will work to avoid 100% of CPU usage.

Environment.ProcessorCount will get back Logical Processors Count, In my case i7 4 real cores 8 logical. In some other cases it can differently. But what matters is CPU usage. If I use my application to use Environment.ProcessorCount cores it will use 100% on my PC. What if I do

string lesscpu = Environment.ProcessorCount - 1;

So baisclly removing 1 core. On my PC it will be 7 cores and around 80-90% CPU usage. in some other cases:

2 cores 2 logical = 1 = 50% cpu

2 cores 4 logical = 3 = 70%+ cpu

2 cores 6 logical = 5 = 70%+ cpu

is this true?

UPDATE:

This heavy lifting happens when I'm idle the program will switch off to 1 thread when I'm back... I afraid that if I leave it for long hours on 100% CPU on all cores my pc can overheat and restart or even worse?...

Peak Dermutag
  • 103
  • 2
  • 12
  • 1
    What do you want to accomplish with CPU Utilization and CPU Cores? No. of cores do not directly convert to CPU Utilization. I mean, `((Used Count) / (Total Count))% !~ CPU Utilization %` – Vivek Jain Aug 10 '13 at 17:53
  • @theghostofc what only if we talk about total count, I dont want to look for used count :) Will this still theoretically work? – Peak Dermutag Aug 10 '13 at 18:03
  • 1
    This definitely seems like an [XY Problem](http://meta.stackexchange.com/a/66378). – Erik Philips Aug 10 '13 at 18:07
  • @PeakDermutag, I don't think so, IMHO. – Vivek Jain Aug 10 '13 at 18:09
  • @ErikPhilips I just want to know if this kind of logic is true... like theghostofc said It's more complex than that and I understand that. But i wonder if this logic works... the soultion can really be getting the real core numbers but this function is so bugged and doesn't work on all PC's that I better risk it and use this logic than CPU Cores. Also thanks for the link its useful :) – Peak Dermutag Aug 10 '13 at 18:11
  • @theghostofc why not? if my program uses equal amount of CPU power each core should be used by 100% if total 4 cores 2 core on 100% = total cpu at 50%...The real question here if logical count is always accurate – Peak Dermutag Aug 10 '13 at 18:16

2 Answers2

4

There is very little point in buying a nice processor and then not using its capabilities. If you observe 100% cpu utilization then you can be confident in doing it right. You get what you paid for and you wrote a program that gets maximum concurrency out of the threads. Good job.

The only possible mistake you could make is not lowering the Thread.Priority. Which is important if you care that other programs don't become sluggish when they compete for the processor.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Another 30% of the CPU that is not used could just as well be used for something else if he needs to. – Codecat Aug 10 '13 at 18:56
  • 2
    Kinda pointless, in most cases that just goes unused and only makes the program run slower. A 100% of the CPU can be used by *all* programs. The operating system schedules threads by priority. Covered in any introductory book on OS design btw. – Hans Passant Aug 10 '13 at 19:25
  • Sometimes you want your PC to do some heavy lifting while still being able to do other tasks like say - answering questions on SO or browsing Facebook. :) You're right though- 100% CPU would be more effective! – Codecat Aug 10 '13 at 19:27
  • This heavy lifting happens when I'm idle the program will switch off to 1 thread when I'm back... I afraid that if I leave it for long hours on 100% CPU on all cores my pc can overheat and restart or even worse?... – Peak Dermutag Aug 10 '13 at 20:03
  • 2
    This kind of myth is impossible to reason with. It's an attractive market opportunity perhaps, somebody could make a decent living selling fire extinguishers labelled "for PC use and even worse" at 200% markup. – Hans Passant Aug 10 '13 at 20:15
  • @PeakDermutag Modern CPUs can not over-heat, so there isn't a *even worse* scenario regarding CPUs. There are thermal limits that either cause the CPU to slow down (thus losing heat instead of gaining heat) or they turn off completely. If you're worried about this scenario then the program isn't at fault, the problem lies with the computer case/heat dissipation design. – Erik Philips Aug 10 '13 at 21:40
  • 1
    Here's a [fun video](http://www.youtube.com/watch?v=BSGcnRanYMM) where they overheat processors intentionally by taking the cooler off. It is old, AMD got a pretty good ding out of that. – Hans Passant Aug 10 '13 at 21:50
1

Take a look at SO Question How to get the CPU Usage in C#?. You may also set the priority of the threads as mentioned in SO How to restrict the CPU usage a C# program takes? You may want to review CPU Usage with graphical indication using C# .NET too.

Community
  • 1
  • 1
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47