Upfront I want to say that if you see higher reported memory usage in PHP 7 on real code, the most likely cause is that PHP 7 will report memory usage of mysqlnd buffered queries as part of the memory usage. In PHP 5 this memory usage was not reported (but of course the memory was still used). For large queries this can make a very substantial difference.
Now to your actual case, which is basically the memory usage of PHP immediately after request startup. The answer by MobDev already explains why there is a discrepancy in "real" memory usage, which is the memory usage metric which reports how much memory PHP's allocator has requested from the system allocator of kernel. As MobDev points out PHP 7 will allocate memory in much larger chunks (2MB) and is also more aggressive about caching allocated chunks.
However this does not explain the discrepancy in "non-real" memory usage, which does not take these allocator details into account. It is easy to check whether exactly the memory goes by using a memory profiler, e.g. by running PHP through USE_ZEND_ALLOC=0 valgrind --tool=massif
. The USE_ZEND_ALLOC=0
part instructs PHP to not use its own allocator.
First of all this will show you that the actual memory usage and the memory usage reported by PHP differ quite significantly. Massif will show 3.2MB usage for PHP 5.6 and 2.3MB for PHP 7. The reason is that PHP only reports memory that goes through it's own allocator (ZMM), while many structures that survive across multiple request are not allocated using it.
The largest allocations going through the system allocator (thus not reported in the memory usage) are:
| PHP 5.6 | PHP 7
interned string buffer | 1 MB | 150 KB + strings
GC buffer | 320 KB | 320 KB
internal classes/funcs | >1.3 MB | >0.5 MB
The "internal classes/funcs" number is a crude lower bound, because there are many small allocations involved here which are hard to count. One main difference is visible, which is that PHP 7 does not use a fixed interned string buffer (the listed size is the for the hashtable buffer I'm seeing, which does not include the size of the strings themselves).
However, this still doesn't answer the question of the actually reported memory usage. In this case the largest allocations are:
| PHP 5.6 | PHP 7
VM stack | 130 KB | 256 KB
Object store | 64 KB | (8 KB)
CG arena | --- | 64 KB
There are a couple of differences here. The main one is that PHP 7 uses a larger VM page size (about twice as large). Additionally PHP 7 uses an arena to store certain structures (like user functions), which starts off with a default size of 64KB. On the other hand the size of the object store buffer is significantly smaller in PHP 7.
So essentially the TL;DR answer is that PHP 7 uses a larger VM stack page size.
memory_get_usage()
(no "true" param, no "peak"). Otherwise you're either measuring details of the allocator or measuring the memory usage of the compiler. – Ethelstan$a=10;
. You're engaging in premature optimization. – Harmony