Description
The issue below was reported by @sejas. It negatively impacts user experience of Playground and wp-now
. Let's make sure:
- PHP has ample memory limit available. Confirm
php.ini
defaults to, say, 1GB memory. php_wasm.c
doesn't contribute to the memory leak problem. PHP itself suffers from one, see pm.max_requests. Is php_wasm.c making it worse?- PHP runtime is automatically rotated when an out of memory error is detected AND more than 10 requests were handled to avoid an "infinite rotate trap". This could rely on the error logging API implemented by @bgrgicak.
A few memory leaks were already patched in this repo, find old PRs for more context.
pm.max_requests int
The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries. For endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS. Default value: 0.
What @sejas reported:
I've created a PHP function to make it much easier to benchmark the memory usage:
I'm currently adding it to the index.php, but you can also try the plugin in Studio or wp-now.
- You can also try on Playground WEB. https://playground.wordpress.net/#{%22landingPage%22:%22/%22,%22steps%22:[{%22step%22:%22installPlugin%22,%22pluginZipFile%22:{%22resource%22:%22url%22,%22url%22:%22https://raw.githubusercontent.com/sejas/playground-plugin-use-all-system-memory/main/playground-plugin-use-all-system-memory.zip%22}}]}
function useAllMemory() {
echo "Initial memory usage: " . (memory_get_usage()/(1024*1024)) . ' MB <br>';
// ini_set('memory_limit', '1024MB'); // The php limit seems to be 128MB but it doesn't affect the results.
$data = '';
while (true) {
$data .= str_repeat('a', 1024 * 1024); // Increase string size by 1MB in each iteration
echo "* " . (memory_get_usage()/(1024*1024)) . ' MB <br>';
}
}
useAllMemory();
die();
Here are my results:
Observe that the site screenshot displays almost 60MB of maximum memory.
The next (2nd) page load displays 29MB
The third page load 4.4MB
and then 2.4 MB
and 1.3 MB
Screenshot:
- I'll replicate same workaround as WP-NOW: Use rotatePHPRuntime to avoid memory error playground-tools#152, but that doesn't fix the issue, only mitigates it's effect.