I have this PHP code (version 5.4) that reads in a 6MB text file and splits it into lines, then splits those lines by tabs:
echo "Initial memory used: ".memory_get_usage()." bytes.\n";
$data = file_get_contents(ENROLLMENTS_SRC_PATH);
echo "About to split into lines, current memory used: ".memory_get_usage()." bytes.\n";
$data = explode("\n", $data);
echo "About to split lines into columns, current memory used: ".memory_get_usage()." bytes.\n";
$line = explode("\t", $data[0]);
echo "Split one line, current memory used: ".memory_get_usage()." bytes.\n";
echo "Peak memory used so far: ".memory_get_peak_usage()." bytes.\n";
foreach($data as $key => $line) {
$data[$key] = explode("\t", $line);
}
echo "Final memory used: ".memory_get_usage()." bytes.\n";
echo "Peak memory used: ".memory_get_peak_usage()." bytes.\n";
I understand that PHP arrays have very high overhead, but what I was not expecting was for the peak usage during the foreach loop to be about 28MB larger than the final result, according to these results:
Initial memory used: 226384 bytes.
About to split into lines, current memory used: 6536952 bytes.
About to split lines into columns, current memory used: 18327712 bytes.
Split one line, current memory used: 18328352 bytes.
Peak memory used so far: 24639744 bytes.
Final memory used: 116898184 bytes.
Peak memory used: 135000584 bytes.
This is so large that the peak memory was higher than the memory used by $data
before the loop and after the loop combined, even though the size of a single split line appears to be under a kilobyte. I am trying to reduce the memory used by my script, so I am trying to understand how PHP uses memory.
Why does the foreach
loop use so much apparently excess memory, and is there anything I can do about it?