PHP and MySQL
How to choose a PHP memory_limit
Set PHP memory per process from measured workloads and total worker concurrency without exhausting the host.
By AppLaunch Editorial · Reviewed 2026-08-25
Direct answer
memory_limit constrains an individual PHP request or process, while the host must support multiple workers plus MySQL, caches and the OS. A 256 MB limit with 20 simultaneous workers represents a theoretical 5 GB PHP ceiling before other services.
What this means in plain English
PHP memory_limit applies to one PHP process or request. The server may run many PHP workers at the same time, so a 256 MB limit does not mean the whole site needs only 256 MB. Twenty workers could theoretically ask for 5 GB before counting MySQL or the operating system.
Measure the heaviest normal actions, such as imports or image processing, and separate unusual command-line jobs from ordinary web pages. Fix unexpected growth before raising limits across every request.
Why is per-request memory not the same as server RAM?
| Per process | memory_limit |
|---|---|
| Concurrency | Maximum active PHP workers |
| Host model | limit × workers plus database, cache and OS headroom |
A simple example
Normal pages use under 80 MB, while a monthly import needs 400 MB. Instead of giving every web request 512 MB, the site keeps a smaller web limit and runs the controlled import with its own setting and worker limit.
What to do, step by step
1. Measure peak memory for representative endpoints and jobs.
Start here before buying anything or changing several settings at once. It gives you a clear starting point based on per process: memory_limit. Write the result down so you can compare it later.
2. Separate web and CLI job requirements.
Use the same files, versions and settings that the real project will use. A quick test with an empty or different setup can look successful while completely missing the problem you are trying to solve.
3. Cap worker concurrency to fit the host.
Try the busiest realistic situation, not the easiest one. Include the people, data, traffic or background work you genuinely expect, then watch for slowdowns and errors rather than relying on a single headline number.
4. Fix abnormal growth before increasing limits.
Finish by checking the result against host model: limit × workers plus database, cache and os headroom. Keep the old setting or release available until you know the change works and can be reversed safely.
One more useful tip
A memory-exhausted error includes a file and line, but that is where allocation finally failed, not always where the waste began. Profile the whole operation.
Common mistakes and how to avoid them
Setting -1 in production.
This gives a misleading or unsafe result because it leaves out per process. A better approach is to measure peak memory for representative endpoints and jobs, then check the result before making the change permanent.
Multiplying average rather than peak demand.
This gives a misleading or unsafe result because it leaves out concurrency. A better approach is to separate web and cli job requirements, then check the result before making the change permanent.
Letting one import setting dictate every web request.
This gives a misleading or unsafe result because it leaves out host model. A better approach is to cap worker concurrency to fit the host, then check the result before making the change permanent.
Words explained
- PHP
- The programming language that runs the server-side part of many websites and applications.
- MySQL
- A database that stores structured information such as accounts, orders, settings and website content.
- worker
- A running process that handles one job or request at a time. More workers allow more work at once but also use more memory.
Quick checklist
- Measure peak memory for representative endpoints and jobs.
- Separate web and CLI job requirements.
- Cap worker concurrency to fit the host.
- Fix abnormal growth before increasing limits.
Common questions
What is the simple answer?
memory_limit constrains an individual PHP request or process, while the host must support multiple workers plus MySQL, caches and the OS. A 256 MB limit with 20 simultaneous workers represents a theoretical 5 GB PHP ceiling before other services.
What should I check first?
Start with per process: memory_limit. That is usually more useful than choosing from a marketing label or copying somebody else’s setting.
How can I make the change safely?
Measure peak memory for representative endpoints and jobs. Then change one thing at a time, keep a backup or old version, and use the same real-world test after each change.
What is the easiest mistake to avoid?
Setting -1 in production. Avoiding that one mistake makes the rest of the comparison much more trustworthy.