Memory Consumption In PHP5.6 vs PHP7

php

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 configuration is

* 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

I was confused and wanted to know , why such thing happened. I did another test, with code

$i=0;
while ($i++ < 100000) ;

Below were the result.

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();
I wanted to know, what has changed in PHP 7 and what would be the implication of that.
So ran another complicated  OOPS code, with two classes and manipulation of object.
Then PHP 7 was far better than PHP 5.6

To understand that one needs to understand, how PHP5 and PHP7 allocates 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”.

there are many good links here to read more internal about implementation

Leave a Reply

Your email address will not be published. Required fields are marked *