Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.1 Failed to set memory limit to ... bytes (Current memory usage is ... bytes) #8116

Closed
brainfoolong opened this issue Feb 18, 2022 · 17 comments

Comments

@brainfoolong
Copy link

brainfoolong commented Feb 18, 2022

Description

Hi!

We recently upgraded to PHP 8.1 and now we sporadically get this kind of new errors:

Failed to set memory limit to 536870912 bytes (Current memory usage is 870318080 bytes)

The problem is, the memory usage cannot be that high at that moment, because we use ini_set on the very start of the script, where almost nothing happened.

Furthermore, we can call the same page 100x times, the error does not appear, but sometime, it happens. It is impossible to reproduce. It definitely only is on PHP 8.1 - With PHP 8.0 we never had this kind of issues.

I guess that the system has sometimes wrong internal memory usage stats, maybe from another thread on the same system?

Is there something we can do about it, without just error suppressing or try/catch?

We are on Windows Server, with Apache 2.4.52 and PHP 8.1.2 TSx64.

As said above, unfortunately i have no reproducible example, but the code that throw the error is this line
ini_set("memory_limit", "512M");

Edit: Have now a reproducible script at #8116 (comment)

PHP Version

PHP 8.1.2 TSx64

Operating System

Windows Server 2019

@iluuu1994
Copy link
Member

Not sure if it helps, but you could try setting memory_limit in your php.ini so you'll get an error when you actually allocate the memory. But yeah, if ini_set is virtually the first call that sounds strange, although I know anything about the peculiarities of Windows + Apache. What does var_dump(memory_get_usage()); when you put it at the very top of index.php?

@brainfoolong
Copy link
Author

brainfoolong commented Feb 18, 2022

Thanks for feedback. I cannot change values in php.ini, we don't want all scripts to take 512M of memory. Also i cannot add a dump, because this happen in production in very very rare circumstances, no luck to reproduce it. The dump would bomb our logs, as it's a very big application where every request, no just the buggy one, runs through a single entry point of index.php. Production has a lot more requests, so maybe it's an indicator that concurrent requests on Apache will cause memory usage stats to glitch through. I don't think the number PHP has at this point is correct.

It is really hard to nail it. Out of 5000 requests we had it currently happen only a few times.

I would understand if at that point, the system memory is probably too low to allocate this memory, but the error says that it is already using over 800M when trying to set 512M. Which is pretty much impossible, because the same script run even when it runs fine at 99.9% of time, it doesn't crash because it never hit the 512M limit. I am pretty sure that this was already so in PHP 8.0 but from what i've read in the old bug-tracker, before 8.1 this error was silently ignored...

@brainfoolong
Copy link
Author

brainfoolong commented Feb 18, 2022

Furthermore, the funny thing is, the initial memory limit from php.ini is just 128M before trying to set 512M at runtime, so the script, if really has 800M of memory used should have stopped long before.

@cmb69
Copy link
Contributor

cmb69 commented Feb 18, 2022

I think this behavior is caused by fixing https://bugs.php.net/bug.php?id=81585 (PHP 8.0.15 and 8.1.2); there has been a follow up fix that should be in PHP 8.0.17 and 8.1.4. Unfortunately, there are no snapshot builds for Windows currently, so you either have to build yourself, or wait for PHP 8.1.4RC1 (expected on 2022-03-03) to verify.

@brainfoolong
Copy link
Author

@cmb69 Looks somewhat similar yes but isn't there a other problem with this? Because the initial limit is 128M and the error shows a usage of 800M, the script should have terminated a way before we even can try to set a new memory limit that is 512M? For me this sounds to different levels of memory checks or at least a missing check when the actual limit if 128M reaches. ini_set seems to then force to do a proper memory_check which causes the error.

TLDR: When having 128M initially, trying to set it to 512M and the errors says it already has 800M at this point -> can this be a correct error message?

@brainfoolong
Copy link
Author

Thanks to https://bugs.php.net/bug.php?id=81585 an the posted test script, i've created a testscript that can now be reproduced. The error is similar, but not quite the same, i guess?

Just call it 2 times in your browser, the first time, it just runs fine, second time, it fails.

Output first call:

Limit initial: 256M
finished successfully

Output second call:

Limit initial: 256M
error - Failed to set memory limit to 536870912 bytes (Current memory usage is 578813952 bytes)

Hope this helps
test.php.txt

@cmb69
Copy link
Contributor

cmb69 commented Feb 18, 2022

The script works as expected (at least with the built-in Webserver) with current PHP-8.1 head. I still think the mentioned follow up fix is responsible for that.

@brainfoolong
Copy link
Author

Ok, i will test when a 8.1.4 windows build is available.

@brainfoolong
Copy link
Author

With the new 8.1.4 RC1 build from https://phpdev.toolsforresearch.com/php-8.1.4RC1-Win32-vs16-x64.zip i can verify that this error is gone.

@cmb69
Copy link
Contributor

cmb69 commented Mar 3, 2022

So I'm closing this ticket.

@patrickteng
Copy link

patrickteng commented Jul 21, 2022

Currently using 8.1.8 (just upgraded few days ago), and this issue is still happening.

Difference is that the server is originally 128M.
We will change it to 512M/1024M/-1 when doing something heavy (downloading very big pdf files via mpdf library, for example), then proceed to change it back to 128M.

We will sometimes get the below warning (not always replicable, even downloading the same pdf file might not always have the warning).

Severity: Warning --> Failed to set memory limit to 134217728 bytes (Current memory usage is 3557695488 bytes)

PHP Version 8.1.8
Operating System Ubuntu 20.04.4 LTS (Focal Fossa)

@cmb69
Copy link
Contributor

cmb69 commented Jul 21, 2022

We will change it to 512M/1024M/-1 when doing something heavy (downloading very big pdf files via mpdf library, for example), then proceed to change it back to 128M.

This is not really well suited to the current ZendMM behavior, since it keeps some of the memory allocated, to avoid frequent reallocations.

Severity: Warning --> Failed to set memory limit to 134217728 bytes (Current memory usage is 3557695488 bytes)

And that is the result of that ZendMM behavior. I don't think there is anything to fix in the engine; consider to silence the warning (@).

@patrickteng
Copy link

And that is the result of that ZendMM behavior. I don't think there is anything to fix in the engine; consider to silence the warning (@).

Before i proceed with this, just wanting to know, is there any downsides/dangers of this behavior? Will there be memory leak issues or it should be fine as long as this warning is encountered at the very end of the script?

@brainfoolong
Copy link
Author

I guess, setting the limit higher in a script and then lower it again in the same script will make no sense. So i would recommend never decrease the limit at runtime, only increase if needed, to avoid those kind of errors. Memory is freed automatically on script termination but may still be hold during the script once it has been taken. Decrease will then result in your kind of error here.

Anyway, i think that this thread is not the correct place to discuss this further @patrickteng , because this issue here is not your issue. Just to avoid off-topic.

@bantustic
Copy link

I believe we need to narrow down the culprit here where it'd Wordpress or php. As far php have tried both 8.1 and 8.2, same thing.
For Wordpress I have tried 6.3 and 6.4 same thing
Alternated between 2 different servers still same.
So php error says:

  1. /customize.php?return=%2Fwp-admin%2Fmedia-new.php
    [Sat Mar 02 19:39:18.026375 2024] [php:warn] [pid 560364] [client 162.210.194.1:61025] PHP Warning: Unknown: POST Content-Length of 351 bytes exceeds the limit of 256 byte

2.themes.php%3Fpage%3Dsimple-custom-css.php
[Sat Mar 02 19:52:48.748255 2024] [php:warn] [pid 560931] [client 143.47.228.220:32948] PHP Warning: Failed to set memory limit to 512 bytes (Current memory usage is 2097152 bytes) in Unknown on line 0

This issue has been logged with Wordpress here;
https://core.trac.wordpress.org/ticket/48778#comment:22
ANY HELP WILL BE APPRECIATED

@bantustic
Copy link

Got it
So if you are having issues with media library wordpress (and write permissions are correct) or some php script and you getting these errors

Failed to set memory limit to
PHP Warning: POST Content-Length of
Failed to set memory limit to

so if using apache all you have already taken care of:
memory_limit, upload_max_filesize with reasonable values for your server,

all you have to do is ensure.
post_max_size 0M  (zero)

And walla, you will upload larger images, you will also won't have issues with like events posts, etc

@bantustic
Copy link

Got it So if you are having issues with media library wordpress (and write permissions are correct) or some php script and you getting these errors

Failed to set memory limit to
PHP Warning: POST Content-Length of

so if using apache all you have already taken care of: memory_limit, upload_max_filesize with reasonable values for your server,

all you have to do is ensure. post_max_size 0M  (zero)

And walla, you will upload larger images, you will also won't have issues with like events posts, etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants