In my script I wanted to clear the array elements to free memory from no longer used data.
I found myself in strange situation where using unset() causes:
( ! ) Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 16777224 bytes) in .../models/Persons.php on line 60
This is code part which causes this problem:
$chunks_count = count($this->xml_records_chunk['fnames']) - 1;
for ($num = 0; $num <= $chunks_count; $num++) {
$chunks_count = count($this->xml_records_chunk['fnames']) - 1;
$not_last = ($num < $chunks_count ? ',' : '');
$new_records .= '(' . $this->xml_records_chunk['fnames'][$chunks_count] . ','
. $this->xml_records_chunk['lnames'][$chunks_count] . ' , '
. $this->xml_records_chunk['dobs'][$chunks_count] . ' , '
. $this->xml_records_chunk['phones'][$chunks_count] . ' )' . $not_last;
unset($this->xml_records_chunk['fnames'][$chunks_count]);
unset($this->xml_records_chunk['lnames'][$chunks_count]);
unset($this->xml_records_chunk['dobs'][$chunks_count]);
unset($this->xml_records_chunk['phones'][$chunks_count]);
}
Script works just fine without unset.
Now the questions are:
- Why unset causes memory exhaustion?
- What is the correct way to unset unsused array elements in this case?
I've already checked this for example:
Ok null indeed works a bit other way since with it script dies on line 61 - 3rd unset.