search for in the  
<mainphp_ini_scanned_files>
Last updated: Thu, 19 May 2005

memory_get_usage

(PHP 4 >= 4.3.2, PHP 5)

memory_get_usage -- Returns the amount of memory allocated to PHP

Description

int memory_get_usage ( void )

Returns the amount of memory, in bytes, that's currently being allocated to your PHP script.

memory_get_usage() will only be defined if your PHP is compiled with the --enable-memory-limit configuration option.

Example 1. A memory_get_usage() example

<?php
// This is only an example, the numbers below will
// differ depending on your system

echo memory_get_usage() . "\n"; // 36640

$a = str_repeat("Hello", 4242);

echo
memory_get_usage() . "\n"; // 57960

unset($a);

echo
memory_get_usage() . "\n"; // 36744

?>

See also memory_limit.



User Contributed Notes
memory_get_usage
markus at computino dot de
09-Jan-2005 04:38
on PHP Version 5.0.2 (with WinXP pro, Apache 2) it's not
<?php $_SERVER["OS"] ?> but
<?php $_ENV["OS"] ?>
webNOSPAMsjwans at hotmail dot com
01-Dec-2004 09:09
A quick and dirty Windows XP / 2003 wordaround:

<?php

function getMemUsage()
{
      
       if (
function_exists('memory_get_usage'))
       {
           return
memory_get_usage();
       }
       else if (
strpos( strtolower($_SERVER["OS"]), 'windows') !== false)
       {
          
// Windows workaround
          
$output = array();
          
          
exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);           
           return
substr($output[5], strpos($output[5], ':') + 1);
       }
       else
       {
           return
'<b style="color: red;">no value</b>';
       }
}

?>
randolphothegreat at yahoo dot com
29-Nov-2004 03:37
I'd just like to point out that although sandeepc at myrealbox dot com's idea for displaying the current memory usage is a good one, it's perhaps a bad idea to pipe the entire process list through grep. A better performing method would be to select only the process we're interested in:

<?
$pid
= getmypid();
error_log('MEMORY USAGE (% KB PID ): ' . `ps --pid $pid --no-headers -o%mem,rss,pid`);
?>

True, it's not much of a performance boost, but every bit helps.
24-Oct-2004 06:59
Unfortunately, this function is not available under Windows
ad-rotator.com
14-May-2004 12:31
The method sandeepc at myrealbox dot com posted yields larger memory usage, my guess is that it includes all the PHP interpreter/internal code and not just the script being run.

1) Use ps command
MEMORY USAGE (% KB PID ):  0.8 12588 25087 -> about 12MB
2) Use memory_get_usage()
int(6041952) -> about 6MB
sandeepc at myrealbox dot com
11-Nov-2003 11:15
To get this in pre 4.2.3 do a (works on unix like systems only):

$my_pid = getmypid();
error_log("MEMORY USAGE (% KB PID ): ".`ps -eo%mem,rss,pid | grep $my_pid`);

found this tip somewhere in bugs.php.net!

<mainphp_ini_scanned_files>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT