Jump to content

[SOLVED] memory_get_peak_usage alternative?


phil88

Recommended Posts

I need to use memory_get_peak_usage(), but it's only available in PHP versions > 5.2.0 and I have 5.0.4.

 

I can't use memory_get_usage() either because PHP wasn't compiled with --enable-memory-limit.

 

So, because of this...I'm stuck. Does anybody know of an alternative implementation of either function that would work on my set up? There's no way for me to upgrade PHP or recompile it, I'm stuck with this environment unfortunately.

 

 

Bit of useful info;

PHP 5.0.4, not compiled with --enable-memory-limit

Windows XP

 

I'm guessing that I'm going to need to use exec(), but the examples in the comments on php.net don't seem to be working.

Link to comment
Share on other sites

yes you will. the following works (from php.net)

<?php 
if( !function_exists('memory_get_usage') ) 
{ 
    function memory_get_usage() { 
        if ( substr(PHP_OS,0,3) == 'WIN') 
        { 
               if ( substr( PHP_OS, 0, 3 ) == 'WIN' ) 
                { 
                    $output = array(); 
                    exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
                    return preg_replace( '/[\D]/', '', $output[5] ) * 1024; 
                } 
        }
        else  { 
            $pid = getmypid(); 
            exec("ps -eo%mem,rss,pid | grep $pid", $output); 
            $output = explode("  ", $output[0]); 
            //rss is given in 1024 byte units 
            return $output[1] * 1024; 
        } 
    } 
} 
?>

Link to comment
Share on other sites

Either I have magical scripts, or it doesn't work on my set up. It always returns 0.

 

Edit:

I tried running the exec(...) stuff from a batch file, and it said:

'tasklist' is not recognised as an internal or external command, operable program or batch file.

 

Is this because I have XP Home as opposed to Pro or a server edition?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.