允许的内存大小为 138412032 字节耗尽(试图分配 20480 字节)

Allowed memory size of 138412032 bytes exhausted (tried to allocate 20480 bytes)

我正在尝试使用 Slim PHP Framework 开发一个 PHP 作曲家应用程序,并尝试像这样对 migrations.php 文件进行更改

<?php

declare(strict_types=1);

require __DIR__ . '/../../src/App/App.php';

try {
    $settings = $app->getContainer()->get('settings');

    $hostname = $settings['db']['hostname'];
    $username = $settings['db']['username'];
    $password = $settings['db']['password'];
    $database = $settings['db']['database'];

    $pdo = new PDO("mysql:host=$hostname", $username, $password); //Possibly the connection
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "DROP DATABASE IF EXISTS $database";
    $pdo->exec($sql);
    echo "[OK] Database droped successfully" . PHP_EOL;

    $sql = "CREATE DATABASE $database";
    $pdo->exec($sql);
    echo "[OK] Database created successfully" . PHP_EOL;

    $sql = "USE $database";
    $pdo->exec($sql);
    echo "[OK] Database selected successfully" . PHP_EOL;

    $sql = file_get_contents(__DIR__ . '/../../database/database.sql');
    $pdo->exec($sql);
    echo "[OK] Tables created successfully" . PHP_EOL;
//    echo "[OK] Records inserted successfully" . PHP_EOL;
    $data = file_get_contents(__DIR__ . '/../../database/companies.json');
    $array = json_decode($data, true);
    foreach($array as $row) {
        $relationships = $row["relationships"];

        $pdo->exec($sql);

    }
    echo "[OK] Json inserted into tables successfully" . PHP_EOL;



} catch (PDOException $e) {
    echo "[ERROR] " . $e->getMessage() . PHP_EOL;
}

我尝试加载一个 companies.json 文件,该文件大小为 78.5MB,当我 运行 composer migration 时,我 运行 进入错误 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /.../extras/bin/migration.php on line 35.

我还在代码库中使用相同的composer.json

  {
    "name": "maurobonfietti/rest-api-slim-php",
    "description": "Example of REST API with Slim PHP Framework.",
    "keywords": [
        "php",
        "slim-micro-framework",
        "rest-api",
        "mysql",
        "slim3",
        "slim",
        "rest",
        "api"
    ],
    "homepage": "https://github.com/maurobonfietti/rest-api-slim-php",
    "license": "MIT",
    "authors": [
        {
            "name": "Mauro Bonfietti",
            "email": "mauro.bonfietti@gmail.com",
            "homepage": "https://github.com/maurobonfietti"
        }
    ],
    "require": {
        "firebase/php-jwt": "^5.0",
        "palanik/corsslim": "dev-slim3",
        "predis/predis": "^1.1",
        "respect/validation": "^1.1",
        "slim/slim": "^3.12.2",
        "vlucas/phpdotenv": "^2.4"
    },
    "require-dev": {
        "phpstan/phpstan": "^0.12",
        "phpunit/phpunit": "^9.0"
    },
    "config": {
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\": "src/",
            "Tests\": "tests/"
        }
    },
    "scripts": {
        "coverage": "phpunit --coverage-html=coverage --coverage-text",
        "database": "extras/bin/restart-api.sh",
        "restart": "extras/bin/restart-api.sh",
        "migration": "php extras/bin/migration.php",
        "start": "php -S 0.0.0.0:8080 -t public public/index.php",
        "test": "phpunit"
    }
}

并且我在 here.

/docker/php7/php.ini 的存储库中添加了一个 php.ini 文件

有人介意告诉我如何增加应用程序的内存限制吗?我对 PHP 很陌生。让我知道是否需要更多说明

我建议您可以通过以下方式设置:

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

但是,这是一个糟糕的解决方案,尽量不要将整个 json 文件加载到内存中并使用类似 jsonstreamingparser .

的东西

是128M,设置memory_limit = 256M为例,希望对你有帮助

(在 .ini 中)