When I was doing a benchmark, I found that PHP 7 was using more memory than PHP 5.6.
So, I did a test. I ran a script containing only:
$a=10;
and below are the results for the memory used when I used PHP CLI without any modules (php -n
)
php 5.6 = 222600 Bytes
php 7.0 = 350448 Bytes
* PHP 5.6.23 (cli) (built: Jun 22 2016 12:13:15)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
* PHP 7.0.9 (cli) (built: Jul 20 2016 10:47:41) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
Environment is
Can anyone explain why I got this result?
Using this code, as suggested by @gordon,
$i=0;
while ($i++ < 100000) ;
php 5.6: 227408 bytes
php 7.0: 386640 bytes
I determined memory usage with this code:
echo PHP_EOL;
echo "Memory Usage :".memory_get_usage();
echo PHP_EOL;
echo "Real Memory Usage :".memory_get_usage(true);
echo PHP_EOL;
echo "Real Peak Memory Usage :".memory_get_peak_usage(true);
echo PHP_EOL;
echo "Peak Memory Usage :".memory_get_peak_usage();
PHP 7 is an improved version of PHP 5 that provides faster performance while using less storage. PHP5 coding is much simpler than traditional coding, and PHP 7 provides developers with a simple coding system. The upgraded PHP7 engine is regarded as a next-generation design.
In fact, it is estimated that PHP 7 offers a 100 percent improvement in performance speed over PHP 5.6. This major improvement in speed allows web developers to create sites that provide interesting and engaging interactive features that still respond to user input as quickly as modern web users have come to expect.
Unlike PHP5, PHP 7 supports 64-bit integer or large files. In PHP 5 supporting large files was considered an experiment previously. Whereas in PHP 5, you can run your application on 64-bit system architecture seamlessly.
Benchmark Results. PHP 8.1 is the fastest with CodeIgniter, performing 8.48% more requests per second than PHP 8.0.
To understand the answer to your question - you need understand how PHP5 and PHP7 allocs memory.
PHP5
allocating memory "By Request" assuming by it's Zend Engine structure.
In PHP7
it's some optimizations made at this side, so on memory allocating "by the chunks"
This differences makes very good increasing of performance (because of engine don't need allocate memory on runtime every time you need it and save some time on fragmentation), but it increases memory consumption for "very small" programs, which size is below than "chunk size".
And yes, PHP7 saves memory very much on large programs.
You may view all this differences in pictures below:
Graphics builded with benchmark: 1.php
<?php
ini_set('memory_limit', '5G');
$a=range(1,$argv[1]);
echo PHP_EOL;
echo "Memory Usage :".memory_get_usage();
echo PHP_EOL;
echo "Real Memory Usage :".memory_get_usage(true);
echo PHP_EOL;
echo "Real Peak Memory Usage :".memory_get_peak_usage(true);
echo PHP_EOL;
echo "Peak Memory Usage :".memory_get_peak_usage();
echo PHP_EOL;
bench.sh
// Small programs
(for i in $(seq 0 5 5000);do php5 dev/Tools/mem/1.php $i|cut -f 2 -d:|sed -r 's/^$/;/g'|sed -r 's/([0-9]+)$/\1,/g'|tr -d '\n'; echo $i; done)|tr -d '\n'|sed -r 's/$/]/g'|sed -r 's/^;/[/g'>php5.m
(for i in $(seq 0 5 5000);do php dev/Tools/mem/1.php $i|cut -f 2 -d:|sed -r 's/^$/;/g'|sed -r 's/([0-9]+)$/\1,/g'|tr -d '\n'; echo $i; done)|tr -d '\n'|sed -r 's/$/]/g'|sed -r 's/^;/[/g'>php7.m
//Large Programs
(for i in $(seq 0 50 100000);do php5 dev/Tools/mem/1.php $i|cut -f 2 -d:|sed -r 's/^$/;/g'|sed -r 's/([0-9]+)$/\1,/g'|tr -d '\n'; echo $i; done)|tr -d '\n'|sed -r 's/$/]/g'|sed -r 's/^;/[/g'>php5.m
(for i in $(seq 0 50 100000);do php dev/Tools/mem/1.php $i|cut -f 2 -d:|sed -r 's/^$/;/g'|sed -r 's/([0-9]+)$/\1,/g'|tr -d '\n'; echo $i; done)|tr -d '\n'|sed -r 's/$/]/g'|sed -r 's/^;/[/g'>php7.m
octave drawer
php7;php7=ans;
php5;php5=ans;
plot(php5(:,5)',[php5(:,1:4)';php7(:,1:4)']');
legend("PHP5 mgu", "PHP5 rmu", "PHP5 rpmu", "PHP5 pmu","PHP7 mgu", "PHP7 rmu", "PHP7 rpmu", "PHP7 pmu");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With