You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行php artisan key:generate时PHP内存耗尽报错求助

Fixing "Allowed memory size exhausted" When Running php artisan key:generate

Hey there, let's get this memory issue sorted out for you! First off, 32MB of memory is way too low for Laravel to run its commands—Laravel's core dependencies alone need more than that to load properly. Tweaking the bootstrap/autoload.php file isn't the right fix here (and it'll get overwritten the next time you run Composer updates anyway), so let's focus on the actual solutions:

Quick Temporary Fix

If you just need to run the command once without changing any config files, add a memory limit flag directly to your command:

php -d memory_limit=512M artisan key:generate

This sets a 512MB limit just for this single command, which should be more than enough to generate the app key.

Permanent Global Fix

To make the change stick for all PHP commands and your Laravel app:

  • Locate your php.ini file. You can find its path by running:
    php --ini
    
    Look for the "Loaded Configuration File" line in the output.
  • Open the file and search for the memory_limit directive. Update it to:
    memory_limit = 512M
    
  • Restart your web server (like Apache/Nginx) or PHP-FPM service if you're using it, so the changes take effect.

Composer-Specific Memory Adjustment

If you're running Artisan commands via Composer (like during deployment), you can also set a higher memory limit for Composer itself:

COMPOSER_MEMORY_LIMIT=-1 composer run-script post-install-cmd

The -1 means "unlimited" memory, which is safe for one-off commands like this.

Why You Shouldn't Edit bootstrap/autoload.php

That file is generated and managed by Laravel and Composer. Any changes you make here will be erased the next time you run composer install or composer update. Memory limits are a PHP configuration setting, not something that should be adjusted in framework files.

If you still run into issues after increasing the memory limit, double-check that you're editing the correct php.ini file (sometimes systems have separate ones for CLI and web PHP) or try updating your Laravel version to the latest stable release—older versions might have less efficient memory usage.

内容的提问来源于stack exchange,提问作者EgK

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:32:47