Reputation: 452
I need my curiosity to be cleared on php memory_limit
I included a file memory.php
in my index.php
file
Below is the script i used memory.php, used to get the memory usage of a page.
<?php function formatBytes($bytes, $precision = 2) {
$units = array("b", "kb", "mb", "gb", "tb");
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . " " . $units[$pow];
}
print formatBytes(memory_get_peak_usage());
?>
My index.php uses 500KB
memory.
in my .htaccess
file i added this line
php_value memory_limit 2M
What i am curious about is that i read about users concurrent
action on a page.
My question is that:
for example, if 5 users opened my index.php concurrently, is the memory usage combined i.e 5x500KB=2.5MB or the 500KB memory is for each user and doesn't matter if even 10 users opened my index.php concurrently
If the memory usage is combined for all users do i still have to change my .htaccess
to a larger memory limit.
I have searched on stackoverflow for similar questions but i need a layman's explaination.
Upvotes: 1
Views: 1704
Reputation: 3742
Each request that comes in is handled by your web server (ex. Apache). Now, depending on how the webserver is configured it forks a new process for each request.
php_value memory_limit 2M
That individual process that reads and handles the PHP script is allowed to consume up to 2mb.
Here's a decent write-up. https://haydenjames.io/understanding-php-memory_limit/
PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle. For example, although PHP’s memory limit may be set high to 1GB, that does not mean that scripts will pile up to use that 1GB.
So, as an example, if 10 users all hit /index.php at the same time, your web server would be using 10x 500kb (5mb) of memory in total, but it would be fine because each one is allowed to reach 2mb before and error is thrown.
- for example, if 5 users opened my index.php concurrently, is the memory usage combined i.e 5x500KB=2.5MB or the 500KB memory is for each user and doesn't matter if even 10 users opened my index.php concurrently
The question is worded a bit weird, but no, PHP's memory limit is per process/request so you'll be OK. Each person would be allowed to consume up to 2mb in memory.
- If the memory usage is combined for all users do i still have to change my .htaccess to a larger memory limit.
It is not combined.
Finally, PHP's default memory limit (128mb) is fine for most applications. My recommendation is to leave it unless you have a good reason to change it.
Upvotes: 4
Reputation: 98005
The PHP manual description of memory_limit
says:
This sets the maximum amount of memory in bytes that a script is allowed to allocate.
When this says "a script", it's not referring to a particular URL or file on disk, it's referring to a particular execution of that script - most commonly, a particular request to your web server.
So if you have multiple users requesting the same page, they each get an "allowance" of 2 megabytes (which is a very low limit on a modern server!).
Note that this does not mean that 2 megabytes are pre-allocated for each request. What it means is that if PHP decides it needs more than that, it will immediately stop processing the page, leaving the user with a plain white page, and you with an error in your logs.
Upvotes: 1