Skip to content

memory_limit is ignored #21862

@mvorisek

Description

@mvorisek

Description

The following code:

<?php

$logFx = function ($v, $clear = false) {
    $p = __DIR__ . '/../../log';

    ob_start();
    var_dump($v);
    $str = ob_get_clean();
    file_put_contents($p, $str, $clear ? 0 : FILE_APPEND);
};
$logFx('', true);

$logFx(ini_get('memory_limit'));
$logFx('x');

$logFx(memory_get_usage(true));
$logFx(memory_get_usage(false));

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

$str = '';
for ($i = 0; $i < 4 * 1024; ++$i) {
    $str .= random_bytes(16 * 1024);
    $logFx(memory_get_usage(true));
}

$logFx(memory_get_usage(true));
$logFx(memory_get_usage(false));
$logFx(ini_get('memory_limit'));
$logFx('y');

repro repo: https://github.com/atk4/ui/tree/refs/heads/php_memory_limit_ignored

CI output: https://github.com/atk4/ui/actions/runs/24887405656/job/72870734637#step:10:4112

Resulted in this output:

int(200)
string(57473) "string(0) ""
string(4) "128M"
string(1) "x"
int(18874368)
int(1950048)
...
int(83824640)
int(83841024)
int(83857408)
int(83873792)
int(83890176)
int(83890176)
int(69063032)
string(3) "16M"
string(1) "y"

But I expected this output instead:

int(200)
string(57473) "string(0) ""
string(4) "128M"
string(1) "x"
int(18874368)
int(1950048)
...

must terminate before this is output:
string(1) "y"

PHP, randomly ignores memory_limit and it seems it allocates a new memory page even when real memory usage is already over the limit.

PHP Version

PHP 8.5.5 (cli) (built: Apr 22 2026 11:22:58) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.5, Copyright (c) Zend Technologies
    with Xdebug v3.5.1, Copyright (c) 2002-2026, by Derick Rethans
    with Zend OPcache v8.5.5, Copyright (c), by Zend Technologies

Operating System

linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions