Understanding memory_get_usage() and pmap

橙三吉。 提交于 2019-12-10 11:46:44

问题


I'm trying to understand memory usage of some php processes. I've tried using both get_memory_usage() and pmap, but the results seem to be off by about one order of magnitude. I've tried with both memory_get_usage() and memory_get_usage(true), as well as memory_get_peak_usage(true), but even with memory_get_peak_usage(true) (the largest of all three varieties) there is still a huge with what is reported via pmap.

More specifically, calling memory_get_peak_usage(true) every minute within my php script returns values ranging between 1.75MB and 3.5MB, whereas a typical result of pmap -d PID gives something like:

...

b7839000       4 r---- 0000000000008000 0ca:00060 libcrypt-2.11.1.so
b783a000       4 rw--- 0000000000009000 0ca:00060 libcrypt-2.11.1.so
b783b000     156 rw--- 0000000000000000 000:00000   [ anon ]
b7864000       8 rw--- 0000000000000000 000:00000   [ anon ]
b7867000      12 r-x-- 0000000000000000 0ca:00060 libgpg-error.so.0.4.0
b786a000       4 r---- 0000000000002000 0ca:00060 libgpg-error.so.0.4.0
b786b000       4 rw--- 0000000000003000 0ca:00060 libgpg-error.so.0.4.0
b786c000       4 r---- 0000000000000000 000:00000   [ anon ]
b786d000      16 rw--- 0000000000000000 000:00000   [ anon ]
b7871000     108 r-x-- 0000000000000000 0ca:00060 ld-2.11.1.so
b788c000       4 r---- 000000000001a000 0ca:00060 ld-2.11.1.so
b788d000       4 rw--- 000000000001b000 0ca:00060 ld-2.11.1.so
bffc7000     136 rw--- 0000000000000000 000:00000   [ stack ]
f57fe000       4 r-x-- 0000000000000000 000:00000   [ anon ]
mapped: 32740K    writeable/private: 13116K    shared: 28K

If I understand correctly, the writeable/private figure is the most relevant figure because it is the memory used exclusively by the process. Nearly 13MB is a far cry from the amount reported by memory_get_peak_usage(true). Can someone please explain the discrepancy?


回答1:


My understanding is that memory_get_peak_usage() will return the amount of memory being used by your script. So that's not including the overhead of PHP.

Returns the peak of memory, in bytes, that's been allocated to your PHP script.

You can reduce the memory that PHP uses by compiling in fewer extensions.

PHP itself is a large application, especially with the default extensions. We (as php developers) get to write simple code like simplexml_load_string() and watch the magic happen, but the code powering that magic is kicking around somewhere in memory. Looking at the output of phpinfo() will reveal just how many extensions are installed. Inside PHP sits the code that will take your PHP and convert it into OPCODEs, then the code that executes those opcodes. Each instance of PHP as your executing it will carry that overhead.

This memory usage is clearly non-trivial, but to be expected. If you had to write all the code to process incoming GET/POST/FILES manage XML, file, and stream magic, etc. The memory usage would add up quickly.

As a result a common performance technique is to remove all unneeded extensions to shrink the executable size. For memory constrained servers (which is most of the loaded ones) removing a few extensions and dropping memory usage from 9 to 7 megs results in a bunch more apache workers running.

This overhead isn't shared because each execution really is a separate copy of PHP executing. There alternates available using the thread safe builds, but not all PHP extensions are thread safe.

Removing Extensions Take a look at the functions you're using. mysqli_*? xml_*? etc. Also take a look at how PHP is being built, here's my configure line:

Configure Command => './configure' '--with-apxs2=/usr/local/apache2/bin/apxs' '--with-mysql=mysqlnd' '--with-gd' '--enable-soap' '--with-libxml-dir=/usr/lib/' '--with-mysql-sock=/tmp' '--with-tidy' '--with-jpeg-dir=/usr/lib/' '--with-xsl' '--with-curl' '--with-zlib' '--enable-gd-native-ttf' '--with-openssl' '--with-mcrypt' '--with-pdo-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-bz2' '--enable-bcmath'

If I was shaving memory down I would start by building PHP with ./configure --disable-all (to remove default extensions), then explicitly adding only the extensions I new I was using. I'm not using libxml for my code anymore, nor soap so I can drop those bits. I can outsource my tidy work to a gearman worker and command line, so I'll pull that as well. Then running though my code (having unit testing would be fantastic here) and seeing what broke. Re-building PHP with the required extension, rinse and repeat. Clearly don't do this in production or you're going to have a Bad Time.




回答2:


Bytes vs Bits are a factor of 10 out. Hence 1.3 MBytes = 13MBits



来源:https://stackoverflow.com/questions/14877054/understanding-memory-get-usage-and-pmap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!