How to Limit CPU Usage for Specific Processes in Windows (Alternative to ulimit)


21 views

Unlike Unix-like systems with ulimit, Windows doesn't provide a direct command-line utility to limit CPU usage per process. This becomes problematic when:

  • A buggy application starts consuming 100% CPU
  • Background processes need performance constraints
  • Developing resource-intensive applications that require throttling

1. Using Windows System Resource Manager (WSRM)

For Windows Server editions:

# Install via Server Manager
Add-WindowsFeature Resource-Manager

# Configure CPU limits through GUI
# Sets 20% CPU limit for notepad.exe
New-WsrmPolicy -Name "NotepadLimit" -ProcessName "notepad.exe" -CpuLimitPercent 20

2. Job Objects API (Programmatic Solution)

Create a C++ program to enforce limits:

#include <windows.h>
#include <stdio.h>

int main() {
    HANDLE hJob = CreateJobObject(NULL, L"CPU_LIMITED_JOB");
    JOBOBJECT_CPU_RATE_CONTROL_INFORMATION cpuLimit;
    
    // Set to 30% CPU usage
    cpuLimit.ControlFlags = JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP;
    cpuLimit.CpuRate = 30 * 100; // Percentage times 100
    
    SetInformationJobObject(hJob, 
        JobObjectCpuRateControlInformation, 
        &cpuLimit, 
        sizeof(cpuLimit));
    
    // Now launch your target process here
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;
    CreateProcess(NULL, L"your_process.exe", NULL, NULL, 
        FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
    
    AssignProcessToJobObject(hJob, pi.hProcess);
    ResumeThread(pi.hThread);
    
    WaitForSingleObject(pi.hProcess, INFINITE);
    return 0;
}

1. Process Lasso (GUI Solution)

  • Right-click process → CPU Limit → Set percentage
  • Persistent rules can be saved
  • Free version available with basic functionality

2. BES (Battle Encoder Shirase)

Lightweight open-source alternative with CLI support:

bescmd -limit 25 "C:\path\to\program.exe"

For temporary CPU throttling:

# Requires admin rights
$process = Get-Process -Name "chrome"
$process.ProcessorAffinity = 0x1 # Limit to first core only

When working with WSL processes:

wsl --exec sh -c "ulimit -t 30 && ./your_linux_program"

This sets a 30-second CPU time limit for the WSL process.

  • Job Objects provide the most precise control (1% granularity)
  • Third-party tools often introduce 5-10ms latency
  • For .NET applications, consider ProcessThread.ProcessorAffinity

Verify CPU constraints with:

Get-Counter "\Process(*)\% Processor Time" -Continuous

Or through Task Manager's "Details" tab by adding the "CPU Time" column.


In Linux/Unix systems, ulimit is a powerful command for setting resource limits on processes, including CPU usage. Windows administrators often ask whether there's a direct equivalent. While Windows doesn't have an identical ulimit command, we can achieve similar process control through several methods.

The Windows API provides several ways to control process resources:

// Using WMI to set process priority (PowerShell)
Get-WmiObject Win32_process -filter 'name="your_process.exe"' | 
ForEach-Object { $_.SetPriority(64) }  # 64 = Below Normal

Windows Job Objects provide the most granular control:

// C++ example creating a job object with CPU rate limit
#include <windows.h>

void LimitProcessCPU(DWORD pid, DWORD percentage) {
    HANDLE hJob = CreateJobObject(NULL, NULL);
    JOBOBJECT_CPU_RATE_CONTROL_INFORMATION cpuLimits;
    
    cpuLimits.ControlFlags = JOB_OBJECT_CPU_RATE_CONTROL_ENABLE | 
                           JOB_OBJECT_CPU_RATE_CONTROL_HARD_CAP;
    cpuLimits.CpuRate = percentage * 100;  // 1-10,000 (1-100%)
    
    SetInformationJobObject(hJob, 
        JobObjectCpuRateControlInformation, 
        &cpuLimits, 
        sizeof(cpuLimits));
    
    AssignProcessToJobObject(hJob, OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid));
}

For those needing immediate solutions without coding:

  • Process Explorer (Sysinternals): Right-click process → Set Priority
  • BES (Battle Encoder Shirase): Specialized CPU limiter with percentage controls
  • CPULimit for Windows: Ported Unix-style tool

For administrative scripting:

# Limit process to 50% CPU cycles (PowerShell 7+)
$process = Get-Process -Name "hungry_process"
$process.ProcessorAffinity = 0x5555  # Alternate cores (0101...)
$process.PriorityClass = "BelowNormal"

For persistent settings across reboots:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\problem.exe\PerfOptions]
"CpuPriorityClass"=dword:00000003  # 3 = Below normal