user3454396
user3454396

Reputation: 423

PHP memory vs OS allocated memory

I have always used normal tools to determine my PHP memory usage to try and detect memory leaks, memory consumption etc. Only recently did I look at the following method.

echo shell_exec(sprintf('grep "VmRSS:" /proc/%d/status', getmypid()))."\r\n";

It seems like something I can really use but it has raised a question. This code in different places yields the expected results, although bigger memory allocations are displayed than via PHP. Still it is constant and reflects what I expect.

The following code however confuses me. I replaced VmRSS with VmPEAK, and I get a huge memory count. 400 meg. This has no effect on my max_memory setting in the ini file, that limit still gets triggered only when my normal memory checks shows that the limit was exceeded, and that is normally low, around 50meg. I also cannot understand how the process can consume 400 Meg of data.

echo shell_exec(sprintf('grep "VmPEAK:" /proc/%d/status', getmypid()))."\r\n";

What exactly am I looking at with the above command? Is it pre-allocated memory space?

Upvotes: 4

Views: 244

Answers (1)

Sherif
Sherif

Reputation: 11942

Run man proc from your command line and you should get plenty of details about what's in /proc/[pid]/status. Specifically as it pertains to proc(5) from your question about VmPeak and VmRSS:

* VmPeak: Peak virtual memory size.
* VmRSS: Resident set size.

Virtual memory is tricky, because it's not all necessarily in physical memory. Some virtual memory might, other parts of that memory could be on disk. Mostly, VmRSS is the resident memory that you normally care about whereas VmPeak is just the peak virtual memory size (the maximum amount of virtual memory that's been allocated by this process so far). The resident memory size will grow and shrink as the process allocates and de-allocates memory, but the VmPeak will only stand to grow.

If you want to see detailed information about currently mapped memory regions for a process you'll need to look at /proc/[pid]/maps or use pmap from your command line on a *nix system.

So for example, to know exactly what parts of memory are mapped to this php process we get something like this from pmap.

0000000000400000   8468K r-x-- php5
0000000000e45000    644K r---- php5
0000000000ee6000     52K rw--- php5
0000000000ef3000    116K rw---   [ anon ]
00000000015b9000   6404K rw---   [ anon ]
00007f1b26796000     40K r-x-- libnss_files-2.21.so
00007f1b267a0000   2048K ----- libnss_files-2.21.so
00007f1b269a0000      4K r---- libnss_files-2.21.so
00007f1b269a1000      4K rw--- libnss_files-2.21.so
00007f1b269a2000     44K r-x-- libnss_nis-2.21.so
00007f1b269ad000   2044K ----- libnss_nis-2.21.so
00007f1b26bac000      4K r---- libnss_nis-2.21.so
00007f1b26bad000      4K rw--- libnss_nis-2.21.so
00007f1b26bae000     92K r-x-- libnsl-2.21.so
00007f1b26bc5000   2044K ----- libnsl-2.21.so
00007f1b26dc4000      4K r---- libnsl-2.21.so
00007f1b26dc5000      4K rw--- libnsl-2.21.so
00007f1b26dc6000      8K rw---   [ anon ]
00007f1b26dc8000     32K r-x-- libnss_compat-2.21.so
00007f1b26dd0000   2044K ----- libnss_compat-2.21.so
00007f1b26fcf000      4K r---- libnss_compat-2.21.so
00007f1b26fd0000      4K rw--- libnss_compat-2.21.so
00007f1b26fd1000     28K r-x-- librt-2.21.so
00007f1b26fd8000   2044K ----- librt-2.21.so
00007f1b271d7000      4K r---- librt-2.21.so
00007f1b271d8000      4K rw--- librt-2.21.so
00007f1b271d9000    508K r-x-- libzmq.so.5.0.0
00007f1b27258000   2044K ----- libzmq.so.5.0.0
00007f1b27457000     20K r---- libzmq.so.5.0.0
00007f1b2745c000      4K rw--- libzmq.so.5.0.0
00007f1b2745d000     72K r-x-- zmq.so
00007f1b2746f000   2044K ----- zmq.so
00007f1b2766e000      4K r---- zmq.so
00007f1b2766f000      4K rw--- zmq.so
00007f1b27670000      4K rw---   [ anon ]
00007f1b27671000     56K r-x-- libbsd.so.0.7.0
00007f1b2767f000   2044K ----- libbsd.so.0.7.0
00007f1b2787e000      4K r---- libbsd.so.0.7.0
00007f1b2787f000      4K rw--- libbsd.so.0.7.0
00007f1b27880000      4K rw---   [ anon ]
00007f1b27881000    148K r-x-- libtinfo.so.5.9
00007f1b278a6000   2044K ----- libtinfo.so.5.9
00007f1b27aa5000     16K r---- libtinfo.so.5.9
00007f1b27aa9000      4K rw--- libtinfo.so.5.9
00007f1b27aaa000    196K r-x-- libedit.so.2.0.53
00007f1b27adb000   2048K ----- libedit.so.2.0.53
00007f1b27cdb000      8K r---- libedit.so.2.0.53
00007f1b27cdd000      4K rw--- libedit.so.2.0.53
00007f1b27cde000     16K rw---   [ anon ]
00007f1b27ce2000     32K r-x-- readline.so
00007f1b27cea000   2044K ----- readline.so
00007f1b27ee9000      4K r---- readline.so
00007f1b27eea000      4K rw--- readline.so
00007f1b27eeb000     28K r-x-- pdo_mysql.so
00007f1b27ef2000   2044K ----- pdo_mysql.so
00007f1b280f1000      4K r---- pdo_mysql.so
00007f1b280f2000      4K rw--- pdo_mysql.so
00007f1b280f3000    120K r-x-- mysqli.so
00007f1b28111000   2048K ----- mysqli.so
00007f1b28311000     20K r---- mysqli.so
00007f1b28316000      4K rw--- mysqli.so
00007f1b28317000     44K r-x-- mysql.so
00007f1b28322000   2044K ----- mysql.so
00007f1b28521000      8K r---- mysql.so
00007f1b28523000      4K rw--- mysql.so
00007f1b28524000    196K r-x-- libmemcached.so.11.0.0
00007f1b28555000   2044K ----- libmemcached.so.11.0.0
00007f1b28754000      4K r---- libmemcached.so.11.0.0
00007f1b28755000      4K rw--- libmemcached.so.11.0.0
00007f1b28756000     84K r-x-- memcached.so
00007f1b2876b000   2048K ----- memcached.so
00007f1b2896b000     12K r---- memcached.so
00007f1b2896e000      4K rw--- memcached.so
00007f1b2896f000     92K r-x-- memcache.so
00007f1b28986000   2048K ----- memcache.so
00007f1b28b86000      4K r---- memcache.so
00007f1b28b87000      4K rw--- memcache.so
00007f1b28b88000     40K r-x-- libjson-c.so.2.0.0
00007f1b28b92000   2044K ----- libjson-c.so.2.0.0
00007f1b28d91000      4K r---- libjson-c.so.2.0.0
00007f1b28d92000      4K rw--- libjson-c.so.2.0.0
00007f1b28d93000     32K r-x-- json.so
00007f1b28d9b000   2048K ----- json.so
00007f1b28f9b000      4K r---- json.so
00007f1b28f9c000      4K rw--- json.so
00007f1b28f9d000     48K r-x-- libicuio.so.55.1
00007f1b28fa9000   2044K ----- libicuio.so.55.1
00007f1b291a8000      8K r---- libicuio.so.55.1
00007f1b291aa000      4K rw--- libicuio.so.55.1
00007f1b291ab000   2384K r-x-- libicui18n.so.55.1
00007f1b293ff000   2044K ----- libicui18n.so.55.1
00007f1b295fe000     60K r---- libicui18n.so.55.1
00007f1b2960d000      4K rw--- libicui18n.so.55.1
00007f1b2960e000    360K r-x-- intl.so
00007f1b29668000   2044K ----- intl.so
00007f1b29867000     24K r---- intl.so
00007f1b2986d000     16K rw--- intl.so
00007f1b29871000      4K rw---   [ anon ]
00007f1b29872000   1076K r-x-- libglib-2.0.so.0.4600.2
00007f1b2997f000   2044K ----- libglib-2.0.so.0.4600.2
00007f1b29b7e000      4K r---- libglib-2.0.so.0.4600.2
00007f1b29b7f000      4K rw--- libglib-2.0.so.0.4600.2
00007f1b29b80000      4K rw---   [ anon ]
00007f1b29b81000     36K r-x-- libltdl.so.7.3.0
00007f1b29b8a000   2044K ----- libltdl.so.7.3.0
00007f1b29d89000      4K r---- libltdl.so.7.3.0
00007f1b29d8a000      4K rw--- libltdl.so.7.3.0
00007f1b29d8b000     68K r-x-- libXext.so.6.4.0
00007f1b29d9c000   2044K ----- libXext.so.6.4.0
00007f1b29f9b000      4K r---- libXext.so.6.4.0
00007f1b29f9c000      4K rw--- libXext.so.6.4.0
00007f1b29f9d000   1960K r-x-- libfftw3.so.3.4.4
00007f1b2a187000   2044K ----- libfftw3.so.3.4.4
00007f1b2a386000     80K r---- libfftw3.so.3.4.4
00007f1b2a39a000      4K rw--- libfftw3.so.3.4.4
00007f1b2a39b000     92K r-x-- liblqr-1.so.0.3.2
00007f1b2a3b2000   2044K ----- liblqr-1.so.0.3.2
00007f1b2a5b1000      4K r---- liblqr-1.so.0.3.2
00007f1b2a5b2000      4K rw--- liblqr-1.so.0.3.2
00007f1b2a5b3000    328K r-x-- liblcms2.so.2.0.6
00007f1b2a605000   2044K ----- liblcms2.so.2.0.6
00007f1b2a804000      4K r---- liblcms2.so.2.0.6
00007f1b2a805000     16K rw--- liblcms2.so.2.0.6
00007f1b2a809000      4K rw---   [ anon ]
00007f1b2a80a000    132K r-x-- libgomp.so.1.0.0
00007f1b2a82b000   2044K ----- libgomp.so.1.0.0
00007f1b2aa2a000      4K r---- libgomp.so.1.0.0
00007f1b2aa2b000      4K rw--- libgomp.so.1.0.0
00007f1b2aa2c000   2316K r-x-- libMagickCore-6.Q16.so.2.0.0
00007f1b2ac6f000   2044K ----- libMagickCore-6.Q16.so.2.0.0
00007f1b2ae6e000     80K r---- libMagickCore-6.Q16.so.2.0.0
00007f1b2ae82000    252K rw--- libMagickCore-6.Q16.so.2.0.0
00007f1b2aec1000    124K rw---   [ anon ]
00007f1b2aee0000   1156K r-x-- libMagickWand-6.Q16.so.2.0.0
00007f1b2b001000   2048K ----- libMagickWand-6.Q16.so.2.0.0
00007f1b2b201000      4K r---- libMagickWand-6.Q16.so.2.0.0
00007f1b2b202000     16K rw--- libMagickWand-6.Q16.so.2.0.0
00007f1b2b206000    344K r-x-- imagick.so
00007f1b2b25c000   2044K ----- imagick.so
00007f1b2b45b000     40K r---- imagick.so
00007f1b2b465000     24K rw--- imagick.so
00007f1b2b46b000     64K r-x-- gmp.so
00007f1b2b47b000   2048K ----- gmp.so
00007f1b2b67b000      4K r---- gmp.so
00007f1b2b67c000      4K rw--- gmp.so
00007f1b2b67d000     20K r-x-- libXdmcp.so.6.0.0
00007f1b2b682000   2044K ----- libXdmcp.so.6.0.0
00007f1b2b881000      4K r---- libXdmcp.so.6.0.0
00007f1b2b882000      4K rw--- libXdmcp.so.6.0.0
00007f1b2b883000      8K r-x-- libXau.so.6.0.0
00007f1b2b885000   2048K ----- libXau.so.6.0.0
00007f1b2ba85000      4K r---- libXau.so.6.0.0
00007f1b2ba86000      4K rw--- libXau.so.6.0.0
00007f1b2ba87000    128K r-x-- libxcb.so.1.1.0
00007f1b2baa7000   2044K ----- libxcb.so.1.1.0
00007f1b2bca6000      4K r---- libxcb.so.1.1.0
00007f1b2bca7000      4K rw--- libxcb.so.1.1.0
00007f1b2bca8000     44K r-x-- libjbig.so.0
00007f1b2bcb3000   2044K ----- libjbig.so.0
00007f1b2beb2000      4K r---- libjbig.so.0
00007f1b2beb3000     12K rw--- libjbig.so.0
00007f1b2beb6000    132K r-x-- liblzma.so.5.0.0
00007f1b2bed7000   2044K ----- liblzma.so.5.0.0
00007f1b2c0d6000      4K r---- liblzma.so.5.0.0
00007f1b2c0d7000      4K rw--- liblzma.so.5.0.0
00007f1b2c0d8000    152K r-x-- libexpat.so.1.6.0
00007f1b2c0fe000   2048K ----- libexpat.so.1.6.0
00007f1b2c2fe000      8K r---- libexpat.so.1.6.0
00007f1b2c300000      4K rw--- libexpat.so.1.6.0
00007f1b2c301000   1236K r-x-- libX11.so.6.3.0
00007f1b2c436000   2048K ----- libX11.so.6.3.0
00007f1b2c636000      4K r---- libX11.so.6.3.0
00007f1b2c637000     16K rw--- libX11.so.6.3.0
00007f1b2c63b000    444K r-x-- libtiff.so.5.2.0
00007f1b2c6aa000   2048K ----- libtiff.so.5.2.0
00007f1b2c8aa000      4K r---- libtiff.so.5.2.0
00007f1b2c8ab000     12K rw--- libtiff.so.5.2.0
00007f1b2c8ae000   2036K r-x-- libvpx.so.2.0.0
00007f1b2caab000   2048K ----- libvpx.so.2.0.0
00007f1b2ccab000      8K r---- libvpx.so.2.0.0
00007f1b2ccad000      4K rw--- libvpx.so.2.0.0
00007f1b2ccae000     12K rw---   [ anon ]
00007f1b2ccb1000    240K r-x-- libfontconfig.so.1.8.0
00007f1b2cced000   2044K ----- libfontconfig.so.1.8.0
00007f1b2ceec000      8K r---- libfontconfig.so.1.8.0
00007f1b2ceee000      4K rw--- libfontconfig.so.1.8.0
00007f1b2ceef000    640K r-x-- libfreetype.so.6.11.1
00007f1b2cf8f000   2044K ----- libfreetype.so.6.11.1
00007f1b2d18e000     24K r---- libfreetype.so.6.11.1
00007f1b2d194000      4K rw--- libfreetype.so.6.11.1
00007f1b2d195000    148K r-x-- libpng12.so.0.51.0
00007f1b2d1ba000   2044K ----- libpng12.so.0.51.0
00007f1b2d3b9000      4K r---- libpng12.so.0.51.0
00007f1b2d3ba000      4K rw--- libpng12.so.0.51.0
00007f1b2d3bb000    268K r-x-- libjpeg.so.8.0.2
00007f1b2d3fe000   2048K ----- libjpeg.so.8.0.2
00007f1b2d5fe000      4K r---- libjpeg.so.8.0.2
00007f1b2d5ff000      4K rw--- libjpeg.so.8.0.2
00007f1b2d600000     64K rw---   [ anon ]
00007f1b2d610000     64K r-x-- libXpm.so.4.11.0
00007f1b2d620000   2044K ----- libXpm.so.4.11.0
00007f1b2d81f000      4K r---- libXpm.so.4.11.0
00007f1b2d820000      4K rw--- libXpm.so.4.11.0
00007f1b2d821000    268K r-x-- libgd.so.3.0.0
00007f1b2d864000   2044K ----- libgd.so.3.0.0
00007f1b2da63000     24K r---- libgd.so.3.0.0
00007f1b2da69000    124K rw--- libgd.so.3.0.0
00007f1b2da88000     20K rw---   [ anon ]
00007f1b2da8d000     88K r-x-- gd.so
00007f1b2daa3000   2048K ----- gd.so
00007f1b2dca3000     20K r---- gd.so
00007f1b2dca8000      4K rw--- gd.so
00007f1b2dca9000     36K r-x-- libcrypt-2.21.so
00007f1b2dcb2000   2044K ----- libcrypt-2.21.so
00007f1b2deb1000      4K r---- libcrypt-2.21.so
00007f1b2deb2000      4K rw--- libcrypt-2.21.so
00007f1b2deb3000    184K rw---   [ anon ]
00007f1b2dee1000    800K r-x-- libsqlite3.so.0.8.6
00007f1b2dfa9000   2048K ----- libsqlite3.so.0.8.6
00007f1b2e1a9000      8K r---- libsqlite3.so.0.8.6
00007f1b2e1ab000      8K rw--- libsqlite3.so.0.8.6
00007f1b2e1ad000      4K rw---   [ anon ]
00007f1b2e1ae000    284K r-x-- libhx509.so.5.0.0
00007f1b2e1f5000   2044K ----- libhx509.so.5.0.0
00007f1b2e3f4000      8K r---- libhx509.so.5.0.0
00007f1b2e3f6000      8K rw--- libhx509.so.5.0.0
00007f1b2e3f8000      4K rw---   [ anon ]
00007f1b2e3f9000     56K r-x-- libheimbase.so.1.0.0
00007f1b2e407000   2044K ----- libheimbase.so.1.0.0
00007f1b2e606000      4K r---- libheimbase.so.1.0.0
00007f1b2e607000      4K rw--- libheimbase.so.1.0.0
00007f1b2e608000    156K r-x-- libwind.so.0.0.0
00007f1b2e62f000   2048K ----- libwind.so.0.0.0
00007f1b2e82f000      4K r---- libwind.so.0.0.0
00007f1b2e830000      4K rw--- libwind.so.0.0.0
00007f1b2e831000     28K r-x-- libffi.so.6.0.4
00007f1b2e838000   2044K ----- libffi.so.6.0.4
00007f1b2ea37000      4K r---- libffi.so.6.0.4
00007f1b2ea38000      4K rw--- libffi.so.6.0.4
00007f1b2ea39000     80K r-x-- libroken.so.18.1.0
00007f1b2ea4d000   2044K ----- libroken.so.18.1.0
00007f1b2ec4c000      4K r---- libroken.so.18.1.0
00007f1b2ec4d000      4K rw--- libroken.so.18.1.0
00007f1b2ec4e000    192K r-x-- libhcrypto.so.4.1.0
00007f1b2ec7e000   2048K ----- libhcrypto.so.4.1.0
00007f1b2ee7e000      4K r---- libhcrypto.so.4.1.0
00007f1b2ee7f000      4K rw--- libhcrypto.so.4.1.0
00007f1b2ee80000      4K rw---   [ anon ]
00007f1b2ee81000    636K r-x-- libasn1.so.8.0.0
00007f1b2ef20000   2048K ----- libasn1.so.8.0.0
00007f1b2f120000      4K r---- libasn1.so.8.0.0
00007f1b2f121000     12K rw--- libasn1.so.8.0.0
00007f1b2f124000    528K r-x-- libkrb5.so.26.0.0
00007f1b2f1a8000   2044K ----- libkrb5.so.26.0.0
00007f1b2f3a7000     12K r---- libkrb5.so.26.0.0
00007f1b2f3aa000     12K rw--- libkrb5.so.26.0.0
00007f1b2f3ad000      4K rw---   [ anon ]
00007f1b2f3ae000     32K r-x-- libheimntlm.so.0.1.0
00007f1b2f3b6000   2044K ----- libheimntlm.so.0.1.0
00007f1b2f5b5000      4K r---- libheimntlm.so.0.1.0
00007f1b2f5b6000      4K rw--- libheimntlm.so.0.1.0
00007f1b2f5b7000     12K r-x-- libkeyutils.so.1.5
00007f1b2f5ba000   2044K ----- libkeyutils.so.1.5
00007f1b2f7b9000      4K r---- libkeyutils.so.1.5
00007f1b2f7ba000      4K rw--- libkeyutils.so.1.5
00007f1b2f7bb000     68K r-x-- libtasn1.so.6.4.2
00007f1b2f7cc000   2048K ----- libtasn1.so.6.4.2
00007f1b2f9cc000      4K r---- libtasn1.so.6.4.2
00007f1b2f9cd000      4K rw--- libtasn1.so.6.4.2
00007f1b2f9ce000    360K r-x-- libp11-kit.so.0.1.0
00007f1b2fa28000   2044K ----- libp11-kit.so.0.1.0
00007f1b2fc27000     40K r---- libp11-kit.so.0.1.0
00007f1b2fc31000      8K rw--- libp11-kit.so.0.1.0
00007f1b2fc33000    240K r-x-- libgssapi.so.3.0.0
00007f1b2fc6f000   2048K ----- libgssapi.so.3.0.0
00007f1b2fe6f000      4K r---- libgssapi.so.3.0.0
00007f1b2fe70000      8K rw--- libgssapi.so.3.0.0
00007f1b2fe72000      4K rw---   [ anon ]
00007f1b2fe73000    104K r-x-- libsasl2.so.2.0.25
00007f1b2fe8d000   2044K ----- libsasl2.so.2.0.25
00007f1b3008c000      4K r---- libsasl2.so.2.0.25
00007f1b3008d000      4K rw--- libsasl2.so.2.0.25
00007f1b3008e000     40K r-x-- libkrb5support.so.0.1
00007f1b30098000   2044K ----- libkrb5support.so.0.1
00007f1b30297000      4K r---- libkrb5support.so.0.1
00007f1b30298000      4K rw--- libkrb5support.so.0.1
00007f1b30299000     12K r-x-- libcom_err.so.2.1
00007f1b3029c000   2044K ----- libcom_err.so.2.1
00007f1b3049b000      4K r---- libcom_err.so.2.1
00007f1b3049c000      4K rw--- libcom_err.so.2.1
00007f1b3049d000    176K r-x-- libk5crypto.so.3.1
00007f1b304c9000   2044K ----- libk5crypto.so.3.1
00007f1b306c8000      8K r---- libk5crypto.so.3.1
00007f1b306ca000      4K rw--- libk5crypto.so.3.1
00007f1b306cb000      4K rw---   [ anon ]
00007f1b306cc000    780K r-x-- libkrb5.so.3.3
00007f1b3078f000   2048K ----- libkrb5.so.3.3
00007f1b3098f000     52K r---- libkrb5.so.3.3
00007f1b3099c000      8K rw--- libkrb5.so.3.3
00007f1b3099e000    504K r-x-- libgmp.so.10.2.0
00007f1b30a1c000   2048K ----- libgmp.so.10.2.0
00007f1b30c1c000      4K r---- libgmp.so.10.2.0
00007f1b30c1d000      4K rw--- libgmp.so.10.2.0
00007f1b30c1e000    208K r-x-- libnettle.so.6.1
00007f1b30c52000   2044K ----- libnettle.so.6.1
00007f1b30e51000      8K r---- libnettle.so.6.1
00007f1b30e53000      4K rw--- libnettle.so.6.1
00007f1b30e54000    196K r-x-- libhogweed.so.4.1
00007f1b30e85000   2048K ----- libhogweed.so.4.1
00007f1b31085000      4K r---- libhogweed.so.4.1
00007f1b31086000      4K rw--- libhogweed.so.4.1
00007f1b31087000   1092K r-x-- libgnutls-deb0.so.28.41.7
00007f1b31198000   2048K ----- libgnutls-deb0.so.28.41.7
00007f1b31398000     36K r---- libgnutls-deb0.so.28.41.7
00007f1b313a1000      8K rw--- libgnutls-deb0.so.28.41.7
00007f1b313a3000    312K r-x-- libldap_r-2.4.so.2.10.4
00007f1b313f1000   2044K ----- libldap_r-2.4.so.2.10.4
00007f1b315f0000      8K r---- libldap_r-2.4.so.2.10.4
00007f1b315f2000      4K rw--- libldap_r-2.4.so.2.10.4
00007f1b315f3000      8K rw---   [ anon ]
00007f1b315f5000     52K r-x-- liblber-2.4.so.2.10.4
00007f1b31602000   2048K ----- liblber-2.4.so.2.10.4
00007f1b31802000      4K r---- liblber-2.4.so.2.10.4
00007f1b31803000      4K rw--- liblber-2.4.so.2.10.4
00007f1b31804000    284K r-x-- libgssapi_krb5.so.2.2
00007f1b3184b000   2044K ----- libgssapi_krb5.so.2.2
00007f1b31a4a000      8K r---- libgssapi_krb5.so.2.2
00007f1b31a4c000      8K rw--- libgssapi_krb5.so.2.2
00007f1b31a4e000    108K r-x-- librtmp.so.1
00007f1b31a69000   2044K ----- librtmp.so.1
00007f1b31c68000      8K r---- librtmp.so.1
00007f1b31c6a000      4K rw--- librtmp.so.1
00007f1b31c6b000    196K r-x-- libidn.so.11.6.11
00007f1b31c9c000   2048K ----- libidn.so.11.6.11
00007f1b31e9c000      4K r---- libidn.so.11.6.11
00007f1b31e9d000      4K rw--- libidn.so.11.6.11
00007f1b31e9e000    428K r-x-- libcurl.so.4.3.0
00007f1b31f09000   2048K ----- libcurl.so.4.3.0
00007f1b32109000     12K r---- libcurl.so.4.3.0
00007f1b3210c000      4K rw--- libcurl.so.4.3.0
00007f1b3210d000     80K r-x-- curl.so
00007f1b32121000   2044K ----- curl.so
00007f1b32320000      8K r---- curl.so
00007f1b32322000      4K rw--- curl.so
00007f1b32323000    104K r-x-- pdo.so
00007f1b3233d000   2044K ----- pdo.so
00007f1b3253c000     12K r---- pdo.so
00007f1b3253f000      4K rw--- pdo.so
00007f1b32540000    248K r-x-- mysqlnd.so
00007f1b3257e000   2048K ----- mysqlnd.so
00007f1b3277e000     16K r---- mysqlnd.so
00007f1b32782000      8K rw--- mysqlnd.so
00007f1b32784000      4K rw---   [ anon ]
00007f1b32785000     12K r-x-- libfuzzy.so.2.1.0
00007f1b32788000   2048K ----- libfuzzy.so.2.1.0
00007f1b32988000      4K r---- libfuzzy.so.2.1.0
00007f1b32989000      4K rw--- libfuzzy.so.2.1.0
00007f1b3298a000      8K r-x-- ssdeep.so
00007f1b3298c000   2044K ----- ssdeep.so
00007f1b32b8b000      4K r---- ssdeep.so
00007f1b32b8c000      4K rw--- ssdeep.so
00007f1b32b8d000    144K r-x-- opcache.so
00007f1b32bb1000   2048K ----- opcache.so
00007f1b32db1000      4K r---- opcache.so
00007f1b32db2000      4K rw--- opcache.so
00007f1b32db3000     32K rw---   [ anon ]
00007f1b32dbb000   7048K r---- locale-archive
00007f1b3349d000     88K r-x-- libgcc_s.so.1
00007f1b334b3000   2044K ----- libgcc_s.so.1
00007f1b336b2000      4K r---- libgcc_s.so.1
00007f1b336b3000      4K rw--- libgcc_s.so.1
00007f1b336b4000   1484K r-x-- libstdc++.so.6.0.21
00007f1b33827000   2044K ----- libstdc++.so.6.0.21
00007f1b33a26000     40K r---- libstdc++.so.6.0.21
00007f1b33a30000      8K rw--- libstdc++.so.6.0.21
00007f1b33a32000     16K rw---   [ anon ]
00007f1b33a36000  25304K r-x-- libicudata.so.55.1
00007f1b352ec000   2044K ----- libicudata.so.55.1
00007f1b354eb000      4K r---- libicudata.so.55.1
00007f1b354ec000      4K rw--- libicudata.so.55.1
00007f1b354ed000   1532K r-x-- libicuuc.so.55.1
00007f1b3566c000   2048K ----- libicuuc.so.55.1
00007f1b3586c000     64K r---- libicuuc.so.55.1
00007f1b3587c000      4K rw--- libicuuc.so.55.1
00007f1b3587d000     16K rw---   [ anon ]
00007f1b35881000     96K r-x-- libpthread-2.21.so
00007f1b35899000   2048K ----- libpthread-2.21.so
00007f1b35a99000      4K r---- libpthread-2.21.so
00007f1b35a9a000      4K rw--- libpthread-2.21.so
00007f1b35a9b000     16K rw---   [ anon ]
00007f1b35a9f000   1792K r-x-- libc-2.21.so
00007f1b35c5f000   2048K ----- libc-2.21.so
00007f1b35e5f000     16K r---- libc-2.21.so
00007f1b35e63000      8K rw--- libc-2.21.so
00007f1b35e65000     16K rw---   [ anon ]
00007f1b35e69000   1716K r-x-- libxml2.so.2.9.2
00007f1b36016000   2048K ----- libxml2.so.2.9.2
00007f1b36216000     32K r---- libxml2.so.2.9.2
00007f1b3621e000      8K rw--- libxml2.so.2.9.2
00007f1b36220000      4K rw---   [ anon ]
00007f1b36221000     12K r-x-- libdl-2.21.so
00007f1b36224000   2044K ----- libdl-2.21.so
00007f1b36423000      4K r---- libdl-2.21.so
00007f1b36424000      4K rw--- libdl-2.21.so
00007f1b36425000   1052K r-x-- libm-2.21.so
00007f1b3652c000   2044K ----- libm-2.21.so
00007f1b3672b000      4K r---- libm-2.21.so
00007f1b3672c000      4K rw--- libm-2.21.so
00007f1b3672d000    432K r-x-- libpcre.so.3.13.1
00007f1b36799000   2048K ----- libpcre.so.3.13.1
00007f1b36999000      4K r---- libpcre.so.3.13.1
00007f1b3699a000      4K rw--- libpcre.so.3.13.1
00007f1b3699b000     60K r-x-- libbz2.so.1.0.4
00007f1b369aa000   2044K ----- libbz2.so.1.0.4
00007f1b36ba9000      4K r---- libbz2.so.1.0.4
00007f1b36baa000      4K rw--- libbz2.so.1.0.4
00007f1b36bab000   1688K r-x-- libdb-5.3.so
00007f1b36d51000   2044K ----- libdb-5.3.so
00007f1b36f50000     28K r---- libdb-5.3.so
00007f1b36f57000      4K rw--- libdb-5.3.so
00007f1b36f58000    376K r-x-- libssl.so.1.0.0
00007f1b36fb6000   2048K ----- libssl.so.1.0.0
00007f1b371b6000     16K r---- libssl.so.1.0.0
00007f1b371ba000     28K rw--- libssl.so.1.0.0
00007f1b371c1000   2152K r-x-- libcrypto.so.1.0.0
00007f1b373db000   2044K ----- libcrypto.so.1.0.0
00007f1b375da000    112K r---- libcrypto.so.1.0.0
00007f1b375f6000     48K rw--- libcrypto.so.1.0.0
00007f1b37602000     12K rw---   [ anon ]
00007f1b37605000     92K r-x-- libresolv-2.21.so
00007f1b3761c000   2048K ----- libresolv-2.21.so
00007f1b3781c000      4K r---- libresolv-2.21.so
00007f1b3781d000      4K rw--- libresolv-2.21.so
00007f1b3781e000      8K rw---   [ anon ]
00007f1b37820000    100K r-x-- libz.so.1.2.8
00007f1b37839000   2044K ----- libz.so.1.2.8
00007f1b37a38000      4K r---- libz.so.1.2.8
00007f1b37a39000      4K rw--- libz.so.1.2.8
00007f1b37a3a000    144K r-x-- ld-2.21.so
00007f1b37aa4000   1648K rw---   [ anon ]
00007f1b37c53000     28K r--s- gconv-modules.cache
00007f1b37c5a000     12K rw---   [ anon ]
00007f1b37c5d000      4K r---- ld-2.21.so
00007f1b37c5e000      4K rw--- ld-2.21.so
00007f1b37c5f000      4K rw---   [ anon ]
00007ffd41bf5000    132K rw---   [ stack ]
00007ffd41c29000      8K r----   [ anon ]
00007ffd41c2b000      8K r-x--   [ anon ]
ffffffffff600000      4K r-x--   [ anon ]
 total           298912K

As you can see from the total here this single process looks like it's consuming 298M of memory, but a lot of that memory can be shared among other PHP processes in most cases.

Let's take a look at a detailed breakdown of this output...

The first column is the virtual memory address allocated to the memory region (the offset of the first byte of memory for that region), in hexadecimal notation. The second column is the size of the memory segment. The third column is the permissions set for that memory segment (*just like typical nix filesystem permissions with Owner/Group/World -- read/write/execute permissions). Then there is the region name, which could be anything, like a shared object library, shared memory segment, or anonymously allocated memory segment. It's important to note that some of these memory segments can actually be shared by multiple processes.

The size you will get from PHP's memory_get_usage and memory_get_peak_usage functions, will only include the internal memory allocated for your PHP code and not necessarily any memory that the PHP process is allocating on the system.

The way PHP allocates and frees memory is tricky depending on the SAPI. In a web SAPI like mod_php or php-fpm, for example, there can be multiple PHP processes sharing loaded libraries (these are things like your pdo/curl/dom extensions loaded into PHP), or they could be statically compiled. So this varies from one system to another. PHP also retains the heap memory for each individual worker in a web SAPI from module initialization stage to the shutdown stage, which means multiple requests could keep memory tied up on the system, but inside PHP, to your code it looks like it's available. This is mostly done for performance reasons, because allocating memory is an expensive process.

What's great about PHP though is that you usually don't have to worry too much about memory management because it almost always has sane defaults and manages memory pretty well for you. Some extension code may manage its own memory, but if it goes through the PHP memory manager you can rest assured PHP will always be as efficient as possible with your memory.

Upvotes: 2

Related Questions