Here’s the short answer: llama.cpp itself doesn’t expose a “–max‐ram” flag on Windows, but you can still cap its working‐set via OS tooling or by wrapping the process. Below are four approaches—pick whichever best fits your workflow:
1) Use Docker on Windows
2) Use WSL2 + cgroups
3) Use Windows Job Objects (or a tiny wrapper)
4) Use a 3rd‐party “process governor” tool
— — — 1) Docker on Windows — —
If you have Docker Desktop, you can build a tiny container image for llama.cpp and then run:
This will ensure the container (and hence llama) never exceeds 4 GB of RAM.
— — — 2) WSL2 + cgroups — —
Enable WSL2 and install Ubuntu (or your favorite distro).
In your Windows user folder (%USERPROFILE%\.wslconfig), add something like:
ini
[wsl2]
memory=4GB
processors=2
Restart WSL (wsl --shutdown).
Inside WSL launch llama.cpp; the whole VM is then capped at 4 GB.
— — — 3) Windows Job Objects — —
You can create a tiny C (or C++) “launcher” that:
• Creates a Job Object
• Sets its ProcessMemoryLimit
• Spawns llama.exe into that job
Here’s minimal C++ (Win32) example:
cpp
#include <windows.h>
#include <iostream>
int main(int argc, char* argv[]) {
// 1) Create a job
HANDLE hJob = CreateJobObject(NULL, NULL);
if (!hJob) { std::cerr<<"Failed to CreateJobObject\n"; return 1; }
// 2) Configure RAM limit (bytes)
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info = {};
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
info.ProcessMemoryLimit = 4LL * 1024 * 1024 * 1024; // 4 GB
if (!SetInformationJobObject(hJob, JobObjectExtendedLimitInformation,
&info, sizeof(info))) {
std::cerr<<"Failed to set JOB limit\n"; return 1;
}
// 3) Prepare llama.exe and its args
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
// reconstruct command‐line
std::string cmd = "llama.exe ";
for (int i = 1; i < argc; i++) {
cmd += argv[i];
if (i+1<argc) cmd += " ";
}
// 4) Launch llama.exe
if (!CreateProcess(
NULL,
cmd.data(),
NULL, NULL, FALSE, CREATE_SUSPENDED,
NULL, NULL, &si, &pi)) {
std::cerr<<"CreateProcess failed\n"; return 1;
}
// 5) Assign to job
if (!AssignProcessToJobObject(hJob, pi.hProcess)) {
std::cerr<<"Assign to job failed\n"; TerminateProcess(pi.hProcess,1);
}
// 6) Resume and wait
ResumeThread(pi.hThread);
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
CloseHandle(hJob);
return 0;
}
Compile that with Visual Studio or mingw-w64, drop the exe next to llama.exe, and call:
— — — 4) Third‐party “Process Governor” — —
There’s a tiny tool called ProcessGovernor (by craigbarnes on GitHub) that can limit memory/CPU without you writing any code. Once installed, you do:
FAQ
• Q: Aren’t there llama.cpp flags to reduce memory?
A: You can use --low_vram, reduce n_ctx, or use --no-mmap, but that only lowers consumption—it doesn’t hard-cap it.
• Q: I’m on native Windows, no Docker/WSL:
A: Your best bets are #3 (Job Object wrapper) or #4 (ProcessGovernor).