升级到 PHP 8.0.16 后,PHPunit 失败并"Allowed memory size of 134217728 bytes exhausted (tried to allocate 524



在我的Laravel项目中,我升级到了当前最新的Laravel9.3.0和PHP8.0.16。

最初的版本是Laravel 8.64和PHP 7.4。

我在带有php:8.0.16-fpm-alpine映像的Docker容器中运行该项目。上一个是php:7.4-fpm-alpine

这是我在docker-compose.yml文件中的Docker容器配置:

version: "3"
services:
php:
build:
context: ./.docker-config/dockerfiles
dockerfile: php.dockerfile
volumes:
- ./laravel:/var/www/html:delegated
networks:
- mynetwork

这是php.dockerfile:

FROM php:8.0.16-fpm-alpine
WORKDIR /var/www/html
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
ENV PHP_MEMORY_LIMIT=2G
ENV PHP_UPLOAD_MAX_FILESIZE: 512M
ENV PHP_POST_MAX_SIZE: 512M
RUN docker-php-ext-install pdo pdo_mysql
# ...

如您所见,我为PHP内存限制设置了2GB

当我运行docker-compose run --rm phpunit时,我得到这个错误:

PHPUnit 9.5.16 by Sebastian Bergmann and contributors.
...............................................................  63 / 281 ( 22%)
......................
SymfonyComponentErrorHandlerErrorFatalError
Allowed memory size of 134217728 bytes exhausted (tried to allocate 5242880 bytes)
at database/migrations/2022_01_21_120600_create_tags_table.php:16
12▕      * @return void
13▕      */
14▕     public function up()
15▕     {
➜  16▕         Schema::create('tags', function (Blueprint $table) {
17▕             $table->id();
18▕             $table->string('name')->default('');
19▕             $table->string('type')->default('');
20▕             $table->string('color', 7)->default('');

这是引用的迁移文件。我觉得没什么特别的:

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name')->default('');
$table->string('type')->default('');
$table->string('color', 7)->default('');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
};

之前在不同的迁移文件上发生了相同的错误。

知道发生了什么吗?我该如何解决这个问题?

谢谢!

记录在案,我遇到了同样的问题。同样在Laravel 8->9升级。特别是在各种迁移中。

我会继续寻找解决方案,但由于我们在相同的情况下都遇到了相同的问题,这可能与Laravel 9升级有关。

EDIT:原来php8和/或php单元存在内存泄漏。这给我带来真正问题的原因是,我的代码中有一个手动内存覆盖:

ini_set('memory_limit', '256M');

删除后,我的测试运行时没有出现问题。当然,这不是内存泄漏的解决方案,但至少我的测试能够运行。

如果您有相同的问题,请检查您的代码是否存在类似的错误。

您可以在Dockerfile中使用来解决内存限制

RUN echo memory_limit = -1 >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini;

相关内容

最新更新