Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) Laravel
You are encountering a classic PHP resource exhaustion error, but the context here is specific: this happens immediately after a system restart and only on your restored environment. The log messages are unambiguousâPHP has hit its hard limit for memory allocation. Specifically, the interpreter attempted to allocate 20 KB (20480 bytes) but failed because the total allowed memory size was 134217728 bytes (exactly 128 MB).
The stack traces point to two critical areas: Illuminate/Foundation/Bootstrap/HandleExceptions and Doctrine\DBAL. This combination is significant. When PHP hits a fatal error, it tries to call handleException() to log the issue or dump backtraces. However, if you are already out of memory, that exception handler itself crashes. Simultaneously, Doctrine attempts to reconnect to your database, which requires allocating more memory than is available. This creates a deadlock where the application cannot even report its failure gracefully.
The fact that this worked before the system was restored suggests a change in the server's resource constraints rather than a bug in your Laravel code. When you migrate servers or perform a fresh install, the default php.ini configuration often remains unchanged from the distribution template. On many shared hosting environments or lightweight VPS plans, the default memory_limit is set to 128M. If your previous server had a higher limit (e.g., 512M or unlimited) and you simply copied the project files without adjusting the configuration, the new environment is strictly more constrained.
To resolve this, you must modify the memory limit for the PHP-FPM pool or CLI process. Since the error appears during php artisan serve (which runs in CLI mode), you need to target the SAPI-specific configuration.
Open your main configuration file located at /etc/php/8.x/fpm/php.ini (adjusting the version number to match your install) and search for the memory_limit directive. You will likely see:
memory_limit = 128M
Increase this value based on your server's available RAM. A safe starting point for a production application using Doctrine ORM is often 512M or higher, though you should monitor your actual usage to avoid swapping.
memory_limit = 512M
If you are using PHP-FPM, ensure this change applies to the specific pool defined in /etc/php/8.x/fpm/pool.d/www.conf. Sometimes the main php.ini is overwritten by pool-specific configurations. Look for a memory_limit entry there as well and align it with your main configuration.
For the CLI environment accessed via php artisan, you might need to edit /etc/php/8.x/cli/php.ini. It is best practice to ensure both FPM and CLI share the same limit to avoid confusion during development or deployment scripts.
After making these changes, restart PHP-FPM to apply the new settings:
sudo systemctl restart php-fpm
Once the memory limit is increased, the application should be able to allocate the necessary 20 KB buffer for exception handling and database connection initialization. If you are unsure how much memory your specific queries consume, consider using a tool like Blackfire or Xdebug with memory profiling to identify if any single query is approaching this threshold before raising the limit indefinitely. However, in the context of a post-restoration failure, simply aligning the new server's limits with the previous successful environment is the most direct fix.
