Created
June 3, 2015 19:56
-
-
Save sclarson/8ddff50042d5894fbb31 to your computer and use it in GitHub Desktop.
Unserialize link
This file contains 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
phinas@trogdor$ php unserialize-link-maybe.php 20 | |
Attach 8 MB shared memory segment | |
Memory after my_array creation: 524288 -> 25427968 (+24903680 +23.75 MB, initial +24903680 +23.75 MB) | |
Memory used by serialized my_array 1st element: 335 B, used by serialized my_array: 3.26 MB, 10000 elements | |
Memory after unset#001: 26214400 -> 26738688 ( +524288 +512 kB, initial +524288 +512 kB) | |
Memory after unset#003: 26738688 -> 27000832 ( +262144 +256 kB, initial +786432 +768 kB) | |
Memory after unset#004: 27000832 -> 26738688 ( -262144 -256 kB, initial +524288 +512 kB) | |
Memory after unset#005: 26738688 -> 27000832 ( +262144 +256 kB, initial +786432 +768 kB) | |
Memory after unset#006: 27000832 -> 27787264 ( +786432 +768 kB, initial +1572864 +1.5 MB) | |
Memory after unset#007: 27787264 -> 28573696 ( +786432 +768 kB, initial +2359296 +2.25 MB) | |
Memory after unset#008: 28573696 -> 29097984 ( +524288 +512 kB, initial +2883584 +2.75 MB) | |
Memory after unset#009: 29097984 -> 29884416 ( +786432 +768 kB, initial +3670016 +3.5 MB) | |
Memory after unset#010: 29884416 -> 30670848 ( +786432 +768 kB, initial +4456448 +4.25 MB) | |
Memory after unset#011: 30670848 -> 31457280 ( +786432 +768 kB, initial +5242880 +5 MB) | |
Memory after unset#013: 31457280 -> 31719424 ( +262144 +256 kB, initial +5505024 +5.25 MB) | |
Memory after unset#015: 31719424 -> 32243712 ( +524288 +512 kB, initial +6029312 +5.75 MB) | |
Memory after unset#017: 32243712 -> 32768000 ( +524288 +512 kB, initial +6553600 +6.25 MB) | |
Memory after unset#018: 32768000 -> 33030144 ( +262144 +256 kB, initial +6815744 +6.5 MB) | |
Memory after unset#019: 33030144 -> 33292288 ( +262144 +256 kB, initial +7077888 +6.75 MB) | |
Memory after unset#020: 33292288 -> 33554432 ( +262144 +256 kB, initial +7340032 +7 MB) | |
Memory after shm_remove: 33554432 -> 33292288 ( -262144 -256 kB, initial +7077888 +6.75 MB) | |
Defined variables: argc, argv, num_loops, shm_size, shm_key, shm_id, initial_memory_usage, prev_memory_usage, memory_usage, loop, k, i |
This file contains 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 | |
main($_SERVER['argc'], $_SERVER['argv']); | |
function main($argc, $argv) { | |
if ( $argc > 1 ) { | |
$num_loops = abs(intval($argv[1])); | |
} else { | |
$num_loops = 1; | |
} | |
$initial_memory_usage = 0; | |
$prev_memory_usage = 0; | |
$memory_usage = 0; | |
$loop = 0; | |
$initial_memory_usage = memory_get_usage(true); | |
$prev_memory_usage = $initial_memory_usage; | |
for ($k = 1 ; $k <= 10000 ; $k++) { | |
$my_array[$k] = array(); | |
for ($i = 0; $i < 12 ; $i++) { | |
$my_array[$k]["item_".$i] = "value_".$i; | |
} | |
} | |
print_memory_usage("my_array creation", $initial_memory_usage, $memory_usage, $prev_memory_usage); | |
print_array_size($my_array, "my_array"); | |
// Reset memory usage after my_array allocation | |
$initial_memory_usage = memory_get_usage(true); | |
$prev_memory_usage = $initial_memory_usage; | |
$memory_usage = $initial_memory_usage; | |
// This loop seems to leak memory... | |
for ($loop = 1 ; $loop <= $num_loops ; $loop++) { | |
unserialize_test($my_array, true); | |
print_memory_usage(sprintf('Loop#%03d', $loop), $initial_memory_usage, $memory_usage, $prev_memory_usage); | |
} | |
// debug_zval_dump($my_array); | |
unset($my_array); | |
print_memory_usage('unset', $initial_memory_usage, $memory_usage, $prev_memory_usage); | |
printf("Defined variables: %s\n", implode(", ", array_keys(get_defined_vars()))); | |
} | |
/* ------------------------------ END MAIN --------------------------------------------------------------*/ | |
function unserialize_test(&$my_array, $change_values) { | |
$new_array = unserialize(serialize($my_array)); | |
if ( $change_values ) { | |
/* | |
* Change values in new_array (to defeat copy-on-write) | |
*/ | |
for ($k = 1 ; $k <= 10000 ; $k++) { | |
for ($i = 0; $i < 12 ; $i++) { | |
$new_array[$k]["item_".$i] = "VALUE_".$i; | |
} | |
} | |
} | |
// print_memory_usage(sprintf('set#%03d', $loop), $initial_memory_usage, $memory_usage, $prev_memory_usage); | |
// debug_zval_dump($new_array); | |
unset($new_array); | |
} | |
function print_memory_usage($function_name, $initial_memory_usage, &$memory_usage, &$prev_memory_usage) { | |
usleep(500 * 1000); // Sleep to let GC run | |
$memory_usage = memory_get_usage(true); | |
if ( $memory_usage != $prev_memory_usage) { | |
printf("Memory after %15s: %8d -> %8d (%+8d %s, initial %+8d %s)\n" | |
, $function_name | |
, $prev_memory_usage | |
, $memory_usage | |
, ($memory_usage - $prev_memory_usage) | |
, format_delta($memory_usage - $prev_memory_usage) | |
, ($memory_usage - $initial_memory_usage) | |
, format_delta($memory_usage - $initial_memory_usage) | |
); | |
$prev_memory_usage = $memory_usage; | |
} | |
} | |
function print_array_size($array, $array_name) { | |
printf("Memory used by serialized %s 1st element: %s, used by serialized %s: %s, %d elements\n" | |
, $array_name | |
, (count($array) > 0)?format_size(php_sizeof(reset($array))):format_size(0) | |
, $array_name | |
, format_size(php_sizeof($array)) | |
, count($array)); | |
} | |
function php_sizeof($object) { | |
return (strlen(serialize($object))); | |
} | |
/** | |
* Return a string formatted size from a given size in bytes. | |
* | |
* @param int $bytes quantity to format | |
* @return string | |
*/ | |
function format_size($bytes, $precision = 2) { | |
$units = array(' B', ' kB', ' MB', ' GB', ' TB', ' PB'); | |
$bytes = max($bytes, 0); | |
$pow = floor(($bytes ? log($bytes) : 0) / log(1024)); | |
$pow = min($pow, count($units) - 1); | |
$bytes /= pow(1024, $pow); | |
return round($bytes, $precision) . $units[$pow]; | |
} | |
/** | |
* Return a string formatted delta from a given size in bytes. | |
* | |
* @param unknown $size | |
* @return string | |
*/ | |
function format_delta($bytes, $precision = 2) { | |
$sign = ($bytes < 0)?'-':'+'; | |
return $sign . format_size(abs($bytes), $precision); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment