没有不值得去解决的问题,也没有不值得去学习的技术!

Resolving a PHP Fatal Error for Memory Exhaustion When Uploading Files With the Yii 2 HTTP Client Extension

The uploaded file has a size of 50.5 MB. In the local environment, this file was successfully uploaded, with memory usage: 164.813 MB and time consumed: 43,244 ms.

1. PHP Fatal Error ‘yii\base\ErrorException’ with message ‘Allowed memory size of 2147483648 bytes exhausted (tried to allocate 1084897244 bytes)’. See Figure 1.

PHP Fatal Error 'yii\base\ErrorException' with message 'Allowed memory size of 2147483648 bytes exhausted (tried to allocate 1084897244 bytes)'.
Figure 1
Plaintext

The command "'/usr/local/php/bin/php' '/mcloud/www/channel-pub-api/yii' 'upload-asset-queue/exec' '45' '7200' '1' '1010' '--verbose=1' '--color=0'" failed.

Exit Code: 255(Unknown error)

Working directory: /

Output:
================
2020-12-04 14:31:27 [45] common\jobs\UploadAssetJob (attempt: 1, pid: 1010) - Started


Error Output:
================
PHP Fatal Error 'yii\base\ErrorException' with message 'Allowed memory size of 2147483648 bytes exhausted (tried to allocate 1084897244 bytes)'

in /mcloud/www/channel-pub-api/vendor/yiisoft/yii2-httpclient/src/Request.php:381

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}


2. Downloaded file: https://webtv-test.oss-cn-beijing.aliyuncs.com/rmhadmin/vod/2020/12/02/b73b9e8c3796447494a9dbe6dc66e2ad/b73b9e8c3796447494a9dbe6dc66e2ad_h264_1200k_mp4.mp4 . Its size is 1.44 GB. In the local environment, this file was successfully downloaded, with memory usage: 6.376 MB and time consumed: 916,308 ms. See Figure 2.

Downloaded file: https://webtv-test.oss-cn-beijing.aliyuncs.com/rmhadmin/vod/2020/12/02/b73b9e8c3796447494a9dbe6dc66e2ad/b73b9e8c3796447494a9dbe6dc66e2ad_h264_1200k_mp4.mp4 . Its size is 1.44 GB. In the local environment, this file was successfully downloaded, with memory usage: 6.376 MB and time consumed: 916,308 ms.
Figure 2

3. The uploaded file has a size of 50.5 MB. In the local environment, this file was successfully uploaded, with memory usage: 164.813 MB and time consumed: 43,244 ms. The uploaded file has a size of 117 MB. In the local environment, this file was successfully uploaded, with memory usage: 362.813 MB and time consumed: 97,774 ms. It can be concluded that the memory usage is approximately 3 times the file size. See Figure 3 and Figure 4.

The uploaded file has a size of 50.5 MB. In the local environment, this file was successfully uploaded, with memory usage: 164.813 MB and time consumed: 43,244 ms.
Figure 3

 

The uploaded file has a size of 117 MB. In the local environment, this file was successfully uploaded, with memory usage: 362.813 MB and time consumed: 97,774 ms. It can be concluded that the memory usage is approximately 3 times the file size.
Figure 4

4. There is currently no better solution for optimizing memory usage. Therefore, the issue was ultimately resolved by modifying the resource limit configuration option in php.ini. memory_limit = 4096M. This sets the maximum amount of memory a script is allowed to allocate. The uploaded file has a size of 1.01 GB. No further errors occurred.

5. However, when uploading a file with a size of 1.44 GB, the error still occurred. memory_limit = 6144M. No further errors occurred.

Plaintext

The command "'/usr/local/php/bin/php' '/mcloud/www/channel-pub-api/yii' 'upload-asset-queue/exec' '68' '7200' '1' '1012' '--verbose=1' '--color=0'" failed.

Exit Code: 255(Unknown error)

Working directory: /

Output:
================
2020-12-07 17:54:21 [68] common\jobs\UploadAssetJob (attempt: 1, pid: 1012) - Started


Error Output:
================
PHP Fatal Error 'yii\base\ErrorException' with message 'Allowed memory size of 4294967296 bytes exhausted (tried to allocate 1551797080 bytes)'

in /mcloud/www/channel-pub-api/vendor/yiisoft/yii2-httpclient/src/Request.php:391

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系