RSS | Archive | Random | E-mail

About

Hi, I'm Christian Castelli, a 28 years old italian programmer located in Pisa (Italy). Here I post small snippets of code which can be useful in my work.

Links

Codepuzzling main site
Development site
ByteStrike italian blog
Follow me on Twitter

My Life Style

while(passion) {
  try {
    myLife.run();
  }catch(LifeExceptions) {  
    stronger++;
    continue;
   }
}

Following

7 July 09

[PHP] How much memory does your app consume?

Here it is a simple function to display how much memory your PHP script is consuming.

// debug function with time, memory consumption (MB) and optional custom message
public function getMemoryUsage($message="", $echo=1) 
{
 $mem_used = memory_get_usage(true)/ 1024 / 1024;
 $mem_peak = memory_get_peak_usage(true) / 1024 / 1024;
 preg_match("/(\d)+/",ini_get('memory_limit'),$matches);  // regexp, takes only digits
 $mem_limit = $matches[0];
 $percentual = round(($mem_used * 100) / $mem_limit,2);

 $res = date("H:i:s",time())." ".$message." ";		
 $res .= "[MEM_USED: $mem_used MB ($percentual%) - Peak: $mem_peak MB]\n";
 if($echo) echo $res;
 else return $res;
}

Tags: php debugging
Comments (View)
  1. andreaolivato reblogged this from codepuzzling
  2. codepuzzling posted this
blog comments powered by Disqus
Themed by Hunson. Originally by Josh