Last active
March 28, 2024 21:44
-
-
Save 1234ru/5b0e7d4c1845ec40fbcfa76e2c7b8739 to your computer and use it in GitHub Desktop.
Memory usage is unaffected by using a symbolic link instead of cyclic direct assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* There is no point in using symbolic links instead of just assigning values in the case below. */ | |
// Run this on console or use ob_flush()! | |
echo "Before: " . round(memory_get_peak_usage(true)/1024/1024, 2) . " Mb\n"; | |
// Never less than 2 mb at start | |
$string = str_repeat('a', 1000000); // Still 2 Mb | |
$array = array_fill(0, 100000, [ 'id' => 1 ]); // 6 Mb | |
foreach ($array as $row) { // &$row increases it from 6 to 10 Mb | |
// No actions with $row increase usage. | |
// Any action with &$row increases to 46 Mb | |
// $row['rand'] = rand(); | |
// $row['value'] = &$string; | |
// $row['value'] = $string; | |
// No difference between $string and &$string | |
// $a2[] = $row; // 12 Mb (+6 Mb) | |
// $row['id']; // No effect | |
// $a2[] = []; // 12 Mb | |
// $a2[] = ['id' => 1]; // 12 Mb | |
// $a2[] = [ 'value' => $string ]; // 46 Mb | |
// $a2[] = [ 'id' => rand() ];// 46 Mb | |
} | |
echo "After: " . round(memory_get_peak_usage(true)/1024/1024, 2) . " Mb"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment