Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am facing more memory consumption in Php 7 compare to PHP 5.6

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

  • OS: window 10
  • Server : IIS (although I used the CLI, not the server), with fast cgi
  • machine : 64 bit
  • php-5.6.23-nts-Win32-VC11-x64
  • php-7.0.9-nts-Win32-VC14-x64

Can anyone explain why I got this result?


Additional Tests

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();
like image 970
developerCK Avatar asked Sep 28 '16 07:09

developerCK


People also ask

Is PHP 5 faster than 7?

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.

What is the difference between PHP 5.6 and 7?

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.

What are the most important advantages of using PHP 7 over PHP 5?

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.

Which PHP version is fastest?

Benchmark Results. PHP 8.1 is the fastest with CodeIgniter, performing 8.48% more requests per second than PHP 8.0.


1 Answers

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"

  • On start it allocates large chunk of memory
  • On in-app allocation it allocates small chunk to avoid fragmentation

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:

PHP memory allocation for large programs PHP memory allocation for small programs

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");

Read more

  1. Official PHP7/PHP-NG presentation: https://drive.google.com/file/d/0B3UKOMH_4lgBUTdjUGxIZ3l1Ukk/view
  2. Official PHP7/PHP-NG internal changes description: https://wiki.php.net/phpng-int
  3. Official extension migration guide: https://wiki.php.net/phpng-upgrading
  4. Good articles from @NikiC: http://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html and http://nikic.github.io/2015/06/19/Internal-value-representation-in-PHP-7-part-2
  5. PHP5 internal details: http://www.phpinternalsbook.com/
  6. Badoo PHP5->PHP7 success story with details: https://techblog.badoo.com/blog/2016/03/14/how-badoo-saved-one-million-dollars-switching-to-php7/
like image 110
MobDev Avatar answered Sep 20 '22 14:09

MobDev