[PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8]
memory_get_usage — Returns the amount of memory allocated to PHP
Description
memory_get_usage[bool $real_usage
= false
]: int
Parameters
real_usage
Set this to true
to get total memory allocated from system, including unused pages. If not set or false
only the used memory is reported.
Note:
PHP does not track memory that is not allocated by
emalloc[]
Return Values
Returns the memory amount in bytes.
Examples
Example #1 A memory_get_usage[] example
See Also
- memory_get_peak_usage[] - Returns the peak of memory allocated by PHP
- memory_limit
xelozz -at- gmail.com ¶
12 years ago
To get the memory usage in KB or MB
Alex Aulbach ¶
11 years ago
Note, that the official IEC-prefix for kilobyte, megabyte and so on are KiB, MiB, TiB and so on.
See //en.wikipedia.org/wiki/Tebibyte
At first glance this may sound like "What the hell? Everybody knows, that we mean 1024 not 1000 and the difference is not too big, so what?". But in about 10 years, the size of harddisks [and files on them] reaches the petabyte-limit and then the difference between PB and PiB is magnificent.
Better to get used to it now. :]
jeff dot peck at snet dot net ¶
13 years ago
To get the memory usage in KB or MB
stanislav dot eckert at vizson dot de ¶
5 years ago
memory_get_usage[] is used to retrieve the memory allocated to PHP only [or your running script]. But intuitively, many people expect to get the memory usage of the system, based on the name of the function.
So if you need the overall memory usage, following function might be helpful. If retrieves the memory usage either in percent [without the percent sign] or in bytes by returning an array with free and overall memory of your system. Tested with Windows [7] and Linux [on an Raspberry Pi 2]: