user2736158
user2736158

Reputation: 419

How do I simulate 100% CPU usage?

To simulate 100% CPU usage, I placed an infinite while loop in my code ( while (true) { } ) . This seemed to spike the CPU usage up to 30% (ordinarily it is 2% for the same program that I run without the while loop). Why does it not go above 30%? This is a dual core Intel i7 processor. The app is a simple console app running c# code on .net 4.0

 private static void SimulateCPUSpike()
 { 
       while(true) { }
 }

Upvotes: 2

Views: 3504

Answers (3)

Andacious
Andacious

Reputation: 1172

Surprisingly, this worked from a PowerShell prompt:

while(1) {Start-Job {while(1){}}}

Upvotes: 0

Stefan
Stefan

Reputation: 9329

Dual core cpu, 2 hyperthreads per core. So you might expect 1 thread at 100% usage to take 25% of the total. The other 5% is everything else that the system is doing.

Even with 4 threads, you may not get 100% usage, due to scheduling.

The real question is, why do you want 100% usage...are you trying to cook a steak on the CPU?

Upvotes: 0

SLaks
SLaks

Reputation: 887225

CPU usage is a percentage of all CPU cores.
If your code is only running a single thread, it cannot occupy more than one core.

You need to make a separate thread for each core. (Environment.ProcessorCount)

Upvotes: 4

Related Questions