Фича PHP:
php.net/manual/ru/features.gc.refcounting-basics.p...
Решения:
1. Вместо объекта использовать массив.
2. Сериализация/Десериализация объекта.
class TestMemory
{
protected $memory_start;
protected $memory_filled_array;
protected $memory_empty_array;
protected $memory_deleted_array;
protected $array;
public function __construct()
{
$this->memory_start = 0;
$this->memory_filled_array = 0;
$this->memory_empty_array = 0;
$this->memory_deleted_array = 0;
}
public function runTest()
{
$this->memory_start = memory_get_usage();
$this->array = [];
for($i = 0; $i < 10000; $i++)
{
$object = new stdClass();
$object->property1 = $i;
$object->property2 = 123;
$object->property3 = str_repeat('Str', 100);
$this->array[] = serialize($object);
unset($object);
}
$this->memory_filled_array = memory_get_usage();
for($i = 0; $i < 10000; $i++)
{
$object = unserialize($this->array[$i]);
//$object; //do some
unset($this->array[$i], $object);
}
$this->memory_empty_array = memory_get_usage();
unset($this->array, $i);
$this->memory_deleted_array = memory_get_usage();
}
public function printResult()
{
$result = '---Result---' . '<br>';
foreach($this as $name => $value)
{
if($name !== 'array') $result .= $name . ': ' . number_format($value / 1024 / 1024, 2) . 'MB.' . '<br>';
}
$result .= 'Memory leak: ' . ($this->memory_deleted_array - $this->memory_start) . 'Byte' . '<br>';
echo $result;
}
}
$test = new TestMemory();
for($i = 0; $i < 5; $i++)
{
$test->runTest();
$test->printResult();
}