3v4l.org

run code in 500+ PHP versions simultaneously
<?php gc_enable(); $arr = []; getMemory(); for ($i = 0; $i < 5000; $i++) { $d = str_repeat("Hello, world!", 400); $arr[$i][] = $d; } // Clear the sub-arrays foreach (array_keys($arr) as $index) { unset($arr[$index]); } // Clear the sub-arrays foreach ($arr as &$subArray) { unset($subArray); } array_splice($arr, 0); // Remove all elements from the main array unset($arr); gc_collect_cycles(); getMemory(); function getMemory() { echo date("Y-m-d H:i:s") . ' :: ' . round( (memory_get_usage(true) / 1000000), 2 ) . PHP_EOL; }
Output for 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.1
2023-09-03 18:52:00 :: 2.1 2023-09-03 18:52:00 :: 44.04
Output for 8.1.2 - 8.1.23
2023-09-03 18:52:00 :: 2.1 Fatal error: Out of memory (allocated 31457280) (tried to allocate 20480 bytes) in /in/l6N9I on line 7 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.
Output for 8.2.0 - 8.2.10
2023-09-03 18:52:00 :: 2.1 Fatal error: Out of memory (allocated 31457280 bytes) (tried to allocate 8192 bytes) in /in/l6N9I on line 6 mmap() failed: [12] Cannot allocate memory mmap() failed: [12] Cannot allocate memory
Process exited with code 255.

preferences:
34.18 ms | 1159 KiB | 11 Q