0

Description:

The test script below works fine for 10 iterations but crashes (Segmentation fault) for 400000 iterations while it shouldn't crash.

Uses Php 7.2 on docker (Version 17.09.0-ce-mac35 (19611)) with no extension.

Test script:

<?php
class Lim {
    public $id;
    public $inv;
    public $fi;
    function __construct($id) {
        $this->id = $id;
        $this->inv = new Inv($this);
    }
};

class Inv {
    public $inv;
    public $fi;
    function __construct($inv) { $this->inv = $inv; }
}

$max = 400000;
//$max = 10;

$lim0 = new Lim(0);
$limp = $lim0;
for ($i=1; $i<$max; $i++) {
    $lim = new Lim($i);
    $lim->fi = $limp->inv;
    $limp->inv->fi = $lim;
    $limp = $lim;
}

Does anyone have an idea why ? Thanks

1 Answer 1

2

You are creating a new $lim but not destroying the old one. So you are running out of memory.

add this

unset($lim);

after

$limp = $lim;
Sign up to request clarification or add additional context in comments.

4 Comments

I'm a little skeptical, but totally open to being proven wrong. I'd expect PHP's garbage collector to address this since $lim is being reassigned on the next iteration. I'd also expect PHP to raise a an "Out of memory" fatal error if memory we an issue - a seg fault is a pretty serious failure.
OP is using a docker container so PHP can behave a little differently in there.
The objective is effectively to create 400000 Lim. Php doesn't crash when it gets out of memory ; it just alerts.
PHP sure does crash when an OOM occurs. And docker doesn't normally allocate a lot of memory to it when it starts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.