前回(PHPでのメモリ管理(バージョンによるメモリ消費の違い) - かまてっく ブログ)の続きです。
PHPのver5系とver7系では、メモリ管理が大きく変化していました。 今回も、バージョンでの差異を見ていきます。
1000文字の文字列×100個の配列を作成します。
memory_test.php
<?php echo "文字列を直接指定して配列を作成する\n"; $before_memory = memory_get_usage(); for( $i = 0;$i < 100;$i++ ){ $a[] = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ; } $after_memory = memory_get_usage(); $used_memory = $after_memory-$before_memory; echo "使用メモリ before[{$before_memory}] after[{$after_memory}]:after - before = {$used_memory}\n";
実行結果がこちら
$ php -v PHP 5.3.29 (cli) (built: May 12 2015 22:42:19) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
$ php memory_test.php 文字列を直接指定して配列を作成する 使用メモリ before[627144] after[744784]:after - before = 117640
$ php -v PHP 7.1.27 (cli) (built: Mar 8 2019 18:22:16) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.1.27, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
$ php memory_test.php 文字列を直接指定して配列を作成する 使用メモリ before[354872] after[363120]:after - before = 8248
やはり、今回もver7系がメモリ効率がかなり良くなっていることがわかります。 10倍以上の差が出ています。 ver5系では、100kb(100000bytes)のデータに対して 約110kb(117640bytes)とデータに対してほぼ同量のメモリを消費しています。 ver7系では、データに対してメモリ消費が少ないです。 (この辺りは、スタックが関連しているのでは?と推測しています。)
今回は、変数を利用するパターンも実行してみました。
memory_test2.php
<?php echo "変数に文字列を代入して、それを配列にする\n"; $s = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" . "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" ; $before_memory = memory_get_usage(); for( $i = 0;$i < 100;$i++ ){ $a[] = $s; } $after_memory = memory_get_usage(); $used_memory = $after_memory-$before_memory; echo "使用メモリ before[{$before_memory}] after[{$after_memory}]:after - before = {$used_memory}\n";
実行結果がこちら
$ php -v PHP 5.3.29 (cli) (built: May 12 2015 22:42:19) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
$ php memory_test2.php 変数に文字列を代入して、それを配列にする 使用メモリ before[628544] after[638936]:after - before = 10392
$ php -v PHP 7.1.27 (cli) (built: Mar 8 2019 18:22:16) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.1.27, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
$ php memory_test2.php 変数に文字列を代入して、それを配列にする 使用メモリ before[355000] after[363248]:after - before = 8248
ver7系では変化がありませんでした。 ver5系では変数を利用した方がメモリ効率が良くなっています。 なんと、前回の空配列を100個作るよりメモリ消費は少ないです。
ver5系では、配列の作り方でメモリ消費が大きく変化するようです。