
To simulate high CPU on Linux, run stress --cpu 2 --timeout 60. On Windows, start a PowerShell background job that loops forever. For memory, use stress --vm or stress-ng --vm on Linux and a counted allocation loop in PowerShell. You do this to test monitoring, alerts and autoscaling before a real load does it for you.
I ran the Linux commands in an Ubuntu 24.04 container (Docker Desktop, 8 CPUs, stress 1.0.7, stress-ng 0.17.06) and the PowerShell ones in PowerShell 7.7 on macOS. Testing them also showed that two things in the first version of this post were wrong: the dd command does not use memory, and the PowerShell memory script does not allocate what its limit says. Both are fixed below, and the last section lists what I did not test.
The quick answer
| Goal | Linux | Windows (PowerShell) |
|---|---|---|
| Load 2 cores | stress --cpu 2 --timeout 60 | Two Start-Job loops |
| Load every core | stress --cpu $(nproc) --timeout 60 | One Start-Job loop per logical processor |
| Hold 512 MB | stress --vm 1 --vm-bytes 512M --vm-hang 60 | Counted 1 MB chunks, touched and kept |
| Stop it | pkill stress | Get-Job | Stop-Job, then Get-Job | Remove-Job |
Monitoring CPU Utilization
Before you load a machine, know how to read it. You need two views: the whole system and the process that is causing the load.
Windows (PowerShell)
The Get-Counter cmdlet reads Windows performance counters. It is only available on Windows.
Get-Counter '\Processor(_Total)\% Processor Time'
That returns one sample of the overall CPU usage as a percentage. To watch it change, take a sample every second for ten seconds:
Get-Counter '\Processor(_Total)\% Processor Time' -SampleInterval 1 -MaxSamples 10
-SampleInterval and -MaxSamples are documented parameters of Get-Counter; I could not run this cmdlet because I tested on macOS.
Linux (Command Line)
The top command shows CPU usage per process in real time. For a single snapshot you can paste into a ticket, use batch mode:
top
top -bn1 | head -12
In my test with two stress workers running, top -bn1 showed both at 100.0 %CPU. That figure is per core, so two busy processes on an 8-CPU machine are about a quarter of the machine; the system line read 36% user CPU at that moment, which includes other activity in the Docker VM.
Monitoring Memory Utilization
Insufficient memory causes swapping, slow responses and, eventually, killed processes, so watch it as closely as CPU.
Windows (PowerShell)
Available memory is one counter away:
Get-Counter '\Memory\Available MBytes'
The value is megabytes of free memory. A steady fall toward zero means a shortage. Like the CPU counter, I did not run it.
Linux (Command Line)
free -m
free -m prints total, used, free and available memory in megabytes. Read the available column, not free: Linux uses spare memory for cache, so free looks small on a healthy machine. For one process, ps -o pid,pcpu,rss,cmd -p <PID> shows its resident memory (RSS) in kilobytes, which is the number that actually moves when you simulate a memory load.
Inside a Docker container,
free -mcan show the memory of the Docker VM instead of the container’s limit: in my test it reported 7,836 MB total. To measure the process you started, usepsas above.
Simulating High CPU and Memory Utilization
Start with CPU. Every method below makes one thread spin, so you need one worker per core you want to fill.
Windows (PowerShell)
Start-Job -ScriptBlock {
while ($true) { 1+1 }
}
This starts a background job that calculates in a loop and keeps one core busy. In my test, two of these jobs left two child pwsh processes with more than 2 seconds of CPU time after 4 seconds, so each job fills one core. To fill every core, start one job per logical processor:
1..[Environment]::ProcessorCount | ForEach-Object {
Start-Job -ScriptBlock { while ($true) { 1+1 } }
}
Jobs keep running until you stop them. Clean up with:
Get-Job | Stop-Job
Get-Job | Remove-Job
Linux (Command Line)
The usual tool is stress. Install it first:
sudo apt-get install stress # Debian/Ubuntu
sudo yum install stress # CentOS/RHEL: needs EPEL first
On Rocky Linux 9 the plain install failed with “Unable to find a match: stress”. It installed (version 1.0.4) only after dnf install epel-release. On Debian and Ubuntu it is in the main repository.
Then load two cores for 60 seconds:
stress --cpu 2 --timeout 60 # Utilize 2 CPU cores for 60 seconds
--cpu 2 starts two workers, not “all cores”. In top I saw exactly two stress processes at 100.0%, and the other six cores idle. To fill every core, pass the core count:
stress --cpu $(nproc) --timeout 60
With nproc returning 8, top showed eight workers at about 91% each and the system at 98% user CPU. If you want a target such as 80% instead of 100%, stress-ng can do that:
stress-ng --cpu 0 --cpu-load 80 --timeout 60s
--cpu 0 means one worker per core, and --cpu-load 80 is the load per worker. In my run top showed the workers between 30% and 100% and the system at about 83% user CPU, so treat 80 as a target, not a guarantee. A plain shell works too, with nothing to install:
yes > /dev/null &
That held one core at 99.6% in my test. Stop it with pkill yes.
Simulating High CPU Utilization with Python
If you would rather not install anything, a Python loop does the same job on any operating system. Create a file called cpu_stress.py:
# cpu_stress.py
while True:
pass
Run it with python3 cpu_stress.py (or python on Windows). In top it sat at about 92% of one core. Like every example above, it fills one core only. To fill more, start more copies.
For memory, the goal is different. You need a process whose resident memory really grows and stays there.
Windows (PowerShell)
The first version of this post used this script:
$memoryLimit = 512
$memoryArray = @()
while ($true) {
$memoryArray += New-Object byte[] 1MB
if ($memoryArray.Length -ge $memoryLimit * 1MB) { break }
}
It does not allocate 512 MB. In PowerShell, += on an array with an array on the right flattens the new chunk into the old array, so each “1 MB” chunk adds 1,048,576 separate elements. $memoryArray became an Object[] of boxed bytes, and Length counts elements, not megabytes. With the limit set to 4, the process went from 177 MB to 433 MB of working set. With a limit of 16 it grew from 1,078 MB to 2,651 MB and took 3.6 seconds, and each += copies the whole array again. A limit of 512 would try to build more than half a billion elements, which would very likely exhaust memory (I did not run it).
Count the chunks instead of measuring the array, and keep them in a list:
$memoryLimit = 512 # MB to hold
$chunks = New-Object System.Collections.Generic.List[byte[]]
while ($chunks.Count -lt $memoryLimit) {
$b = New-Object byte[] 1MB
for ($i = 0; $i -lt $b.Length; $i += 4096) { $b[$i] = 1 } # touch every page
$chunks.Add($b)
}
Start-Sleep -Seconds 60 # hold the memory
$chunks.Clear()
In PowerShell 7.7 this took the working set from 173 MB to 652 MB, about 480 MB for 512 chunks. Writing to one byte per 4 KB page makes sure the memory is really used instead of only reserved.
Linux (Command Line)
stress --vm 1 --vm-bytes 512M --vm-hang 60
--vm 1 starts one memory worker, --vm-bytes 512M sets how much it allocates, and --vm-hang 60 makes it hold the memory for 60 seconds instead of freeing and reallocating. In my run ps showed 524,924 kB of resident memory for that worker. Without --vm-hang, the worker keeps cycling and also burns a full core: it showed 99.6% CPU and 301,956 kB RSS at one sample.
stress-ng has the same idea. Add --vm-keep to hold the memory instead of rewriting it:
stress-ng --vm 1 --vm-bytes 1G --vm-keep --timeout 60s
That worker showed 1,050,528 kB RSS. With no tools at all, Python can hold memory:
python3 -c "b = bytearray(512*1024*1024); import time; time.sleep(60)"
RSS read 532,816 kB right after the allocation.
A correction: dd does not use memory
The first version of this post said dd if=/dev/zero of=/dev/null consumes memory. It does not. It reads zeros and discards them, so it is a CPU load:
dd if=/dev/zero of=/dev/null bs=1M count=200000
While it ran, ps showed 99.5% CPU and only 2,416 kB of resident memory. It is a fine one-core CPU load, but it will not trigger a memory alert.
Test it safely in a container
A memory test that is too large can freeze the machine you run it on. A container with a memory limit makes the failure safe and shows you what an out-of-memory kill looks like:
docker run --rm -m 256m --memory-swap 256m ubuntu:24.04 bash -c \
'apt-get update -qq && apt-get install -y -qq stress && stress --vm 1 --vm-bytes 512M --timeout 10'
The worker asked for 512 MB with a 256 MB limit, and stress reported worker got signal 9. Docker’s own record for the container said OOMKilled=true. That is the same signal an alert on killed containers would catch.
Stop and clean up
- Linux:
pkill stress(orpkill stress-ng). In my test, threestressprocesses were running before it and none after. - A background
yes:pkill yes. - PowerShell:
Get-Job | Stop-JobandGet-Job | Remove-Job. After both,Get-Jobreturned nothing in my test. - Anything started with
--timeoutstops by itself.
Frequently asked questions
How do I simulate 100% CPU on every core?
Start one busy worker per logical core: stress --cpu $(nproc) on Linux, or one Start-Job loop per [Environment]::ProcessorCount on Windows. A single loop, whether stress, yes, dd or Python, fills only one core.
Why does my memory test not raise the memory number?
Allocating is not the same as using. Touch the memory (stress --vm, or writing to each page) and hold it (--vm-hang, --vm-keep, or a sleep), then read RSS with ps or the working set in Task Manager.
Can this crash my machine?
Yes, if the memory size is larger than what is free. Keep --vm-bytes well under your available memory, set --timeout, and test inside a container with a memory limit when you can.
What I did not test
- Anything on Windows itself:
Get-Counter, Task Manager, and Windows PowerShell 5.1. The PowerShell memory and job examples ran in PowerShell 7.7 on macOS. - Linux distributions other than Ubuntu 24.04 (the
stressinstall, and the EPEL requirement, on Rocky Linux 9 only). - Real autoscaling or alert rules reacting to these loads.
- A bare-metal or cloud VM: the Linux runs were in a container on Docker Desktop, so the CPU and memory figures describe that VM.