13

SUMMARY


Short recomendations (from more datailed informations, see answers)

To avoid memory leaks you can:

  1. unset variables at once when they become useless
  2. you can use xdebug for detailed report of memory consumption by functions and find memory leaks
  3. you can set memory_limit (for example to 5Mb) to avoid dummy memory allocation

QUESTION

For what php can use memory, except libraries and variables? I monitor memory, used by variables and its ~ 3Mb with this code:

$vars = array_keys(get_defined_vars());
        $cnt_vars = count($vars);
        $allsize = 0;
        for ($j = 0; $j < $cnt_vars; $j++) {

            try
            {
                $size = @serialize($$vars[$j]);
                $size = strlen($size);
            }
            catch(Exception $e){
                $str = json_encode($$vars[$j]);
                $str = str_replace(array('{"','"}','":"','":'), '', $str);
                $size = strlen($str);
            }
            $vars[$j] = array(
                'size' => $size,
                'name' => $vars[$j]
            );
            $allsize += $size;
        }

and libraries takes ~ 18Mb (libcurl, etc.) So total its 21 Mb, but

pmap -x (process) shows, that total memory consumption is kB: 314028 RSS: 74704 Dirty: 59672

so, total real consumption is ~74Mb. Also i see some large blocks with [anon] mapping in my pmap For what PHP using this blocks?

php version: 5.5.9-1ubuntu4.14 php extensions:

root@webdep:~# php -m
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
json
libxml
mbstring
mcrypt
mhash
openssl
pcntl
pcre
PDO
pdo_pgsql
pgsql
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache
16
  • @Smar PHP Version => 5.5.9-1ubuntu4.14
    – fiction
    Commented Feb 23, 2016 at 11:05
  • 2
    You also should consider using Xdebug for profiling, it will give you function call level increasement of memory usage, and can produce callgrind files you can analyze with something useful like kcachegrind.
    – Smar
    Commented Feb 23, 2016 at 11:06
  • 1
    Do you execute this via Apache module or fastcgi/fpm or cli/cgi version? Different versions allocate and free memory differently. Keep in mind some memory is allocated but not used. Play arround with memory_get_usage() and memory_get_usage(true), if you can't analyze with cachegrinder.
    – Daniel W.
    Commented Feb 23, 2016 at 11:17
  • @DanFromGermany i using it via cli/cgi (command php <script>). About memory_get_usage - it gives me only size of variables (and equal to size, that i calculate with my code in question)
    – fiction
    Commented Feb 23, 2016 at 11:32
  • 1
    @Smar I spent some time with PHP 5.5, 5.6 and 7 regarding memory utilisation. Turns out PHP7 improves on CPU usages, Memory utilisation is still quite same. If you are concerned about higher memory usages try HHVM. Commented Feb 27, 2016 at 10:32

4 Answers 4

6

PHP is not same as C or CPP code that compiles to single binary. All your scripts are executed inside Zend Virtual Machine. And most of the memory is consumed by VM itself. That includes the memory used by loaded extensions the shared libraries (.so files) used by PHP process and any other shared resources.

I don't remember the exact source but somewhere I read that nearly 70% of total CPU cycles are consumed by PHP internals and only 30% get to your code (Please correct me if I am wrong here). This is not directly related with Memory consumption but should give an idea about how PHP works.

About anon blocks I found some details in another SO answer. The answer is about Java but same should apply to PHP as well.

Anon blocks are "large" blocks allocated via malloc or mmap -- see the manpages. As such, they have nothing to do with the Java heap (other than the fact that the entire heap should be stored in just such a block).

I would recommend to disable some extensions. That should save you some unused memory.

2
  • So one interesting thing to try would be to reduce memory limit to something like 5 MB; if that fixes excessive memory allocation in OP’s case, I guess it’s just dummy allocation logic of PHP then (like in that case with Java, they’ve tried to fix that in newest Java versions AFAIK :-).
    – Smar
    Commented Feb 26, 2016 at 18:25
  • 1
    @Smar checking it now.. seems to be right, but i need to test it for day ot two, i will reply
    – fiction
    Commented Feb 26, 2016 at 21:39
5
+300

NOTE: this is not exactly an answer but information requested by the OP, but the comment field is too short for this... These are more of tools how to debug this kind of problems.

Xdebug’s docs are pretty comprehensive, they should tell how to use it far better than I could by copying their docs to here. The script you gave is a bit fuzzy, so I did not do the trace myself, but it would give you line-by-line diffs of memory usage.

Basically set xdebug.show_mem_delta to 1 with Xdebug enabled to generate the function trace, which you can then open in a text editor to see what part exactly is the thing that leaks memory.

Then you can compare the initial (or middle position) total memory to see how much it differs from the real memory usage you are seeing.

TRACE START [2007-05-06 14:37:26]
    0.0003     114112  +114112   -> {main}() ../trace.php:0

Here the total memory would be the 114112.

If the difference is really big, you may want to use something like shell_exec() to get the real memory usage in between all lines, and output that, and then you can compare that output to Xdebug’s memory output to see where the difference happens.

If the difference is from the very first line of the script, the culprit could be an extension of PHP. See php -m if there is any fishy extensions.

6
  • How can i check fishy extensions? I add list of php extensions to my question
    – fiction
    Commented Feb 23, 2016 at 14:42
  • 1
    You could disable them all, then enable them one at a time to test. Commented Feb 23, 2016 at 14:46
  • 1
    One thing that comes to my mind is that OPCache, since it’s Zend’s new speedup technology, so it could save something one does not expect. But what @quickshiftin said.
    – Smar
    Commented Feb 23, 2016 at 14:57
  • @Smar some days of debugging.. and i steel have a question... xdebug in the end of trace shows me 24735064 bytes in memory (~23.9 Mb) BUT in pmap i can see 76 Mb used by process (60Mb used by heap). So, i cannot imagine for what this 30 Mb is used for :( And i detect, that process works and take ~23 Mb, and SUDDENLY it takes 76Mb
    – fiction
    Commented Feb 26, 2016 at 20:50
  • 1
    For more info, the memory allocator (at least in Java, but I assume in PHP too; I suspect PHP does it in bad way as PHP is useful in leaking memory in general...) sometimes pre-allocates bigger memory blocks it needs in advance, so it does not need to do allocation all the time. And Java used to never free that memory for the same reason.
    – Smar
    Commented Feb 26, 2016 at 21:43
2

First of all make an array, to investigate memory it is taking

$startMemory = memory_get_usage();
$array = range(1, 100000);
echo memory_get_usage() - $startMemory, ' bytes';

one integer is 8 bytes (on a 64 bit unix machine and using the long type) and here 100000 integers, so you obviously will need 800000 bytes. That’s something like 0.76 MB.

This array gives 14649024 bytes. That’s 13.97 MB - eighteen times more than estimated.

Here is a quick summary of the memory usage of the different components involved:

                             |  64 bit   | 32 bit
---------------------------------------------------
zval                         |  24 bytes | 16 bytes
+ cyclic GC info             |   8 bytes |  4 bytes
+ allocation header          |  16 bytes |  8 bytes
===================================================
zval (value) total           |  48 bytes | 28 bytes
===================================================
bucket                       |  72 bytes | 36 bytes
+ allocation header          |  16 bytes |  8 bytes
+ pointer                    |   8 bytes |  4 bytes
===================================================
bucket (array element) total |  96 bytes | 48 bytes
===================================================
total total                  | 144 bytes | 76 bytes

Again, for large, static arrays, if i call like:

$startMemory = memory_get_usage();
$array = new SplFixedArray(100000);
for ($i = 0; $i < 100000; ++$i) {
$array[$i] = $i;
}
echo memory_get_usage() - $startMemory, ' bytes';

It will result 5600640 bytes

That’s 56 bytes per element and thus much less than the 144 bytes per element a normal array uses. This is because a fixed array doesn’t need the bucket structure. So it only requires one zval (48 bytes) and one pointer (8 bytes) for each element, giving the observed 56 bytes.

Hope this will be helpful.

0

Nothing is wrong with the numbers you see, you shouldn't combine them, this is just "tripling", you are seeing different sections (read-only, executable, writable) for libraries listed separately, your number is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.