Skip to content

Instantly share code, notes, and snippets.

@drewsonne
Forked from craiga/LogMemoryUsage.php
Last active August 29, 2015 13:56
Show Gist options
  • Save drewsonne/8854671 to your computer and use it in GitHub Desktop.
Save drewsonne/8854671 to your computer and use it in GitHub Desktop.
<?php
/**
* Log memory usage.
*
* Makes use of {@link https://gist.github.com/1849563 _log} and {@link https://gist.github.com/2880386 formatMemory}.
*
* @author Craig Anderson <[email protected]>
* @link https://gist.github.com/3336985
*/
protected function _logMemoryUsage()
{
$memoryUsage = memory_get_usage();
if(is_null($this->_lastMemoryUsage))
{
$this->_log("Memory usage: %s", formatMemory($memoryUsage));
}
else
{
$change = $memoryUsage - $this->_lastMemoryUsage;
$change_time = microtime(true) - $this->_lastMemoryUsageTime;
$rate = $change_time == 0 ? 0 : $change / $change_time;
$this->_log("Memory usage: %s; change since last time: %s; rate: %s per second", formatMemory($memoryUsage), formatMemory($change), formatMemory($rate));
}
$this->_lastMemoryUsage = $memoryUsage;
$this->_lastMemoryUsageTime = microtime(true);
}
/**
* Last recorded memory usage.
*/
protected $_lastMemoryUsage = null;
/**
* Last recorded memory usgae time.
*/
protected $_lastMemoryUsageTime = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment