PHP memory leak and fork

I am trying to avoid memory leak in PHP. When I create an object and disconnect it at the end, it is still in memory. The reset is as follows:

$obj = NULL;
unset($obj);

However, this does not help.

My question is what will happen when I redo the proccess and the object will be created and destroyed in the child stream? Will it be the same? Or is there another way to free memory?

This is an import script that will consume several gigabytes.

+3
source share
3 answers

In PHP 5.3 there is a garbage collector that can collect circular references. It might be worth a try:

gc_enable();

class A {
  public function __construct() {
    $this->data = str_repeat("A", 1024000);
  }
}

$mem = memory_get_usage();
$a = new A();
$mem2 = memory_get_usage();
$a->a = $a;
$a->a->mydata =  $a->data . 'test';
$mem3 = memory_get_usage();
unset($a);
gc_collect_cycles();
$mem4 = memory_get_usage();      

printf("MEM 1 at start %0.2f Mb\n", ($mem / 1024) / 1024);
printf("MEM 2 after first instantiation %0.2f Mb\n", ($mem2 / 1024) / 1024);
printf("MEM 3 after self-ref: %0.2f Mb\n", ($mem3 / 1024) / 1024);
printf("MEM 4 after unset(\$a): %0.2f Mb\n", ($mem4 / 1024) / 1024);      

Output:

MEM 1 at start: 0.31 Mb
MEM 2 after first instantiation: 1.29 Mb
MEM 3 after self-ref: 2.26 Mb
MEM 4 after unset($a): 0.31 Mb   
+12
source

NULL unset(). , , unset() . , , , , , , .

+5

, script . ( ), . , , , .

, , , , . MathieuK, PHP5.3 gc *, . PHP - , , ( , , -, "" script..).

+1

Source: https://habr.com/ru/post/1716765/


All Articles