Skip to content

Instantly share code, notes, and snippets.

@mix5003
Created November 10, 2017 04:34
Show Gist options
  • Select an option

  • Save mix5003/3d27a3a401f0f1415f57828c3bef9295 to your computer and use it in GitHub Desktop.

Select an option

Save mix5003/3d27a3a401f0f1415f57828c3bef9295 to your computer and use it in GitHub Desktop.
fread make a var use memory equals to buffer length. even if it has only few bytes
<?php
ini_set('memory_limit', '256M');
// create file
@mkdir('tmp_file');
for($i=0;$i<300;$i++){
file_put_contents("tmp_file/{$i}.txt", $i);
}
// read file
$data = [];
for($i=0;$i<300;$i++){
$fp = fopen("tmp_file/{$i}.txt", "r");
$data[$i] = fread($fp, 1024 * 1024);
fclose($fp);
echo strlen($data[$i])." ".memory_get_usage()."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment