Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
Memory usage of php process

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
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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).

  • Here's the actual link for SO answer https://stackoverflow.com/a/1483482/1012809

  • Check this article for more details on anonymous memory pages (anon) https://techtalk.intersec.com/2013/07/memory-part-2-understanding-process-memory/

  • Also check this Slide-share for more details on PHP memory management http://www.slideshare.net/jpauli/understanding-php-memory

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

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!