New function of PHP v7.3 gc_status() for garbage collector

N

Looking For A Job?

Relax - you're at the right place!

Interview Walkins

New function of PHP gc_status() fetch the data about garbage collector. However, Java doesn’t clearly specify memory and remove this from the program code.

PARAMETERS

Parameters are ‘Null’ into this function.

RETURN VALUES

Following data would be the output:

  • ‘threshold’ It is for ‘num allocs’ when other automatic gc run has been scheduled
  • ‘runs’
  • ‘roots’
  • ‘collected’

Example:

<?php

// create object tree that requires gc collection

$s = new stdClass();

$s->b = [];

for ($i = 0; $i < 100000; $i++) {

    $d = new stdClass();

    $d->a = $s;

    $s->b[] = $d;

}

unset($s);

unset($d);

gc_collect_cycles();

var_dump(gc_status());

As a result below will be output.

array(4) {

  [“runs”]=>

  int(5)

  [“collected”]=>

  int(100002)

  [“threshold”]=>

  int(50001)

  [“roots”]=>

  int(0)

}

TIP

As has been noted. Coder doesn’t certainly remove the memory into the program. Moreover, garbage collector searches the unnecessary garbage objects and remove them. Basically, this garbage collector was built based on two hypotheses. 

  • As can be seen majorly all the objects soon become not reachable.
  • References from the oldest objects to the latest objects only exist in small numbers.

Similar interest:

About the author

Deepak Maurya

1 comment

nineteen + 13 =

Looking For A Job?

Relax - you're at the right place!

Interview Walkins

Most Viewed Posts