> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Limit CPU and Memory for a Process on Linux (systemd)

> Limit CPU and memory for a process on Linux with systemd cgroups: CPUQuota= and MemoryMax= on a service, systemd-run for one command, or nice and cpulimit.

To limit a service's CPU and memory on Linux, run `sudo systemctl set-property myapp.service CPUQuota=50% MemoryMax=1G`. systemd applies the limit through cgroups immediately and keeps it after reboots. For a one-off command, use `sudo systemd-run --scope -p CPUQuota=50% -p MemoryMax=1G command`. `CPUQuota=50%` means half of one core; `MemoryMax` is a hard cap that triggers the out-of-memory killer inside that unit only.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Intermediate | 10 minutes | Ubuntu 22.04 / 24.04, Debian 12 / 13, AlmaLinux / Rocky 9 / 10 (systemd with cgroups v2) | sudo access |

## Before you start

* [Connect with SSH](https://help.freakhosting.com/portals/other/how-to-connect-to-a-linux-server-with-ssh.md) as a user with `sudo`. Root users can leave out `sudo`.
* Decide whether you need a **cap** (never more than X) or a **priority** (gets less when others need it). A cap wastes idle CPU; a priority doesn't. The table below helps.
* Find the service name with `systemctl list-units --type=service`, or which service a busy process belongs to with `systemctl status <PID>`. See [find what is using CPU](https://help.freakhosting.com/portals/other/how-to-find-what-is-using-cpu-on-linux.md).

## Which limit should I use?

| Goal | Use | Type |
| - | - | - |
| Never let a service use more than N% CPU | `CPUQuota=` | Hard cap |
| Give a service less CPU only when others are busy | `CPUWeight=` or `Nice=` | Priority |
| Throttle memory before it gets too big | `MemoryHigh=` | Soft limit |
| Stop a service taking all RAM | `MemoryMax=` | Hard cap |
| Limit a single command you start by hand | `systemd-run --scope -p …` | Cap for that command |
| Slow down an already-running process quickly | `renice` or `cpulimit` | Priority / throttle |

## How to limit a systemd service

**1. Set the limits**

```bash theme={null}
sudo systemctl set-property myapp.service CPUQuota=50% MemoryHigh=768M MemoryMax=1G
```

The change applies straight away and is saved for future boots. Add `--runtime` to apply it only until the next reboot.

**2. Check the values**

```bash theme={null}
systemctl show myapp.service -p CPUQuotaPerSecUSec -p MemoryHigh -p MemoryMax
```

```text theme={null}
CPUQuotaPerSecUSec=500ms
MemoryHigh=805306368
MemoryMax=1073741824
```

Prefer to keep the limits in a file you can read later? Put them in a drop-in instead:

```bash theme={null}
sudo systemctl edit myapp.service
```

```ini theme={null}
[Service]
CPUQuota=50%
MemoryHigh=768M
MemoryMax=1G
```

Then `sudo systemctl restart myapp.service`.

| Setting | Meaning |
| - | - |
| `CPUQuota=50%` | At most half of one CPU core. Use `200%` for two full cores |
| `CPUWeight=50` | Relative share (1 to 10000, kernel default 100). Lower gets less CPU only when there is contention |
| `MemoryHigh=768M` | Above this, the service is slowed down and its memory is reclaimed aggressively |
| `MemoryMax=1G` | Absolute limit. If the service can't stay under it, the out-of-memory killer runs inside this service only |

The systemd documentation recommends `MemoryHigh=` as the main control and `MemoryMax=` as the last line of defence. Sizes use base 1024 (`1G` = 1 GiB), and you can also give a percentage of RAM, such as `MemoryMax=25%`.

> [!WARNING]
>
> A `MemoryMax=` lower than the program really needs makes it crash over and over. Measure normal use first (`systemctl status myapp` shows `Memory:`), then set the limit with headroom. Don't put limits on `sshd`, `systemd-journald` or your database without testing; a starved SSH service can lock you out. If that happens on a VPS, use the [VNC console](https://help.freakhosting.com/portals/vps-panel/how-to-enable-and-use-vnc-console-access.md).

## How to limit a single command with systemd-run

```bash theme={null}
sudo systemd-run --scope -p CPUQuota=25% -p MemoryMax=512M \
  tar -czf /backup/site.tar.gz /var/www/example.com
```

`--scope` runs the command in your terminal, inside a temporary cgroup with these limits. When the command ends, the scope disappears. Without `--scope`, `systemd-run` starts the command as a background service instead.

## How to lower priority with nice and renice

Start a command with low CPU priority:

```bash theme={null}
nice -n 19 ./import-script.sh
```

Lower the priority of a running process (PID `30043`):

```bash theme={null}
sudo renice -n 10 -p 30043
```

```text theme={null}
30043 (process ID) old priority 0, new priority 10
```

Nice values run from `-20` (highest priority) to `19` (lowest). A niced process still uses idle CPU, so this is the gentlest option. For a service, set `Nice=10` in a drop-in to make it permanent.

## How to throttle a running process with cpulimit

`cpulimit` stops and resumes a process many times a second to hold it near a percentage of one core. It needs no restart, but it is less precise than cgroups.

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt install cpulimit
> sudo cpulimit -p 30043 -l 30 -b
> ```

> [tab] AlmaLinux / Rocky 9
>
> `cpulimit` is in EPEL 9:
>
> ```bash theme={null}
> sudo dnf install epel-release
> sudo dnf install cpulimit
> sudo cpulimit -p 30043 -l 30 -b
> ```

`-l 30` is 30% of one core and `-b` runs cpulimit in the background. While throttled, the process shows state `T` (stopped) part of the time in `top`; that is how cpulimit works.

## How to keep a process on certain cores

```bash theme={null}
taskset -cp 0 30043
```

```text theme={null}
pid 30043's current affinity list: 0,1
pid 30043's new affinity list: 0
```

The process now runs only on core 0. For a service, use `CPUAffinity=0` in a drop-in.

## How to check it worked

```bash theme={null}
systemd-cgtop -1 --order=cpu
```

The limited service should stay at or below its `CPUQuota` in the CPU column. For memory, `systemctl status myapp.service` shows the current use and the limits on the `Memory:` line. If the out-of-memory killer acted inside the unit, `journalctl -u myapp.service` records it.

## Frequently asked questions

### What does CPUQuota=100% mean on a 4-core server?

One full core out of four. `CPUQuota` is relative to one CPU, so the maximum on a 4-core server is `400%`.

### How do I remove a limit?

Set it back to empty or `infinity`: `sudo systemctl set-property myapp.service CPUQuota= MemoryMax=infinity`. If you used a drop-in, delete it with `sudo systemctl revert myapp.service` (this removes all drop-ins for that unit).

### Can I limit a Docker container instead?

Yes, with Docker's own flags, for example `docker run --cpus=1.5 --memory=1g`. See [essential Docker commands](https://help.freakhosting.com/portals/other/essential-docker-commands.md).

### Does this work on cgroups v1?

These settings are written for the unified (v2) hierarchy that current Ubuntu, Debian, AlmaLinux and Rocky releases use. Check with `stat -fc %T /sys/fs/cgroup`; `cgroup2fs` means v2.

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every server guide in one place.

> [card] [Find what is using CPU](https://help.freakhosting.com/portals/other/how-to-find-what-is-using-cpu-on-linux.md)
>
> Find the process to limit.

> [card] [Check memory usage](https://help.freakhosting.com/portals/other/how-to-check-memory-usage-on-linux.md)
>
> Measure before you cap.

> [card] [Manage services with systemctl](https://help.freakhosting.com/portals/other/how-to-manage-services-with-systemctl.md)
>
> Start, stop and edit services.

> [card] [Create a systemd service](https://help.freakhosting.com/portals/other/how-to-create-a-systemd-service.md)
>
> Run your app as a service.

> [card] [Fix out of memory errors](https://help.freakhosting.com/portals/other/how-to-fix-out-of-memory-killed-processes.md)
>
> When processes get killed.

***

> [!NOTE]
>
> **Checked against the [systemd.resource-control(5)](https://www.freedesktop.org/software/systemd/man/latest/systemd.resource-control.html), [systemd-run(1)](https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html) and [systemctl(1)](https://www.freedesktop.org/software/systemd/man/latest/systemctl.html) manuals and the cpulimit and taskset manuals** on 4 November 2026; `nice`, `renice`, `taskset` and `cpulimit` tested on Ubuntu 24.04 LTS. systemd limits could not be applied in our test environment. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.