Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 26
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Gratulacje i dzięki! Trafiłem tu przypadkiem poszukując informacji na temat php+mysql. Wiele polskich stron powiela identyczne przykłady, klonuje te same kursy i lekcje... ten serwis okazał sie inny. Zasada "problem - rozwiazanie - wyjaśnienie" zdaje egzamin - zapewnia jasną, jednoznaczną i pewną pomoc w konkretnym przypadku. Porady są warte swojej ceny, przede wszystkim ze względu na przyjazną (także dla początkujących) formę i treść oraz bogate i stale powiększane zasoby. Polecam i pozdrawiam!

Kamil Dmowski
Polski Czerwony Krzyż

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

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.

Przykład 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

grey - greywyvern - com
17-Aug-2005 09:37

If nothing else in the user notes below works for you, you can get a very (VERY) rough estimate of PHP memory usage by outputting the $GLOBALS array, stripping it of indentation whitespace, and counting the characters in the resulting string.  This method has a very high overhead (to be expected), but works on all operating systems, regardless of whether or not they have the --enable-memory-limit config option set.  I find that the syntax overhead of the print_r() statement roughly accounts for the PHP runtime base memory usage.

The code below is set up to work on all arrays, not just the $GLOBALS array.  Keep in mind that outside data referenced by resource IDs, such as database results and open file data, is not included in this total.

<?php

function array_size($arr) {
 
ob_start();
 
print_r($arr);
 
$mem = ob_get_contents();
 
ob_end_clean();
 
$mem = preg_replace("/\n +/", "", $mem);
 
$mem = strlen($mem);
  return
$mem;
}

$memEstimate = array_size($GLOBALS);

?>

Use only if being off to either side by at least 20% is acceptible for your purposes.


vesa dot kivisto at nepton dot fi
14-Jul-2005 03:02

I was unable to get the previous examples working properly and created code which works at least for me. Enjoy!

// Please note that you'll need the pslist.exe utility from http://www.sysinternals.com/Utilities/PsTools.html
// This is because win/2000 itself does not provide a task list utility.
//
function getMemoryUsage() {

 // try to use PHP build in function
 if( function_exists('memory_get_usage') ) {
  return memory_get_usage();
 }

 // Try to get Windows memory usage via pslist command
 if ( substr(PHP_OS,0,3) == 'WIN') {
 
  $resultRow = 8;
  $resultRowItemStartPosition = 34;
  $resultRowItemLength = 8;
 
  $output = array();
  exec('pslist -m ' . getmypid() , $output);
  
  return trim(substr($output[$resultRow], $resultRowItemStartPosition, $resultRowItemLength)) . ' KB';
 
 }

 
 // No memory functionality available at all
 return '<b style="color: red;">no value</b>';
 
}


guenter_doege at web dot de
11-Jul-2005 08:28

The Win XP / 2003 workaround script will also work with windows 2000 with a few slight modifications.

Please note that you'll need the pslist.exe utility from http://www.sysinternals.com/Utilities/PsTools.html because win/2000 itself does not provide a task list utility.

<?php

function getMemUsage()
{
    
       if (
function_exists('memory_get_usage'))
       {
           return
memory_get_usage();
       }
       else if (
substr(PHP_OS,0,3) == 'WIN')
       {
          
// Windows 2000 workaround

          
$output = array();
          
exec('pslist ' . getmypid() , $output);
           return
trim(substr($output[8],38,10));
       }
       else
       {
           return
'<b style="color: red;">no value</b>';
       }
}
?>


MagicalTux at FF dot st
25-May-2005 09:08

When you need to get the OS, do not use $_SERVER['OS'] or $_ENV['OS'], better use PHP_OS constant !
<?php
if (substr(PHP_OS,0,3)=='WIN') {
 
// [...]
}
?>

You also have other values such as CYGWIN_NT-5.0, Linux, ... this is the best way to get system's os (anyone on linux can do an "export OS=windows")


markus at computino dot de
09-Jan-2005 11: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 04: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 10: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.


ad-rotator.com
14-May-2004 07: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 06: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!


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt