Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@firebus
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firebus/2e324b1261f0e2cbe1f9 to your computer and use it in GitHub Desktop.
Save firebus/2e324b1261f0e2cbe1f9 to your computer and use it in GitHub Desktop.
class MemoryMash {
public function mash() {
str_pad('', $this->getMaxMemory());
}
private function getMaxMemory() {
$memoryLimit = ini_get('memory_limit');
if ($memoryLimit != -1) {
$memoryLimit = $this->return_bytes($memoryLimit);
} else {
# linuxy only, meh
exec('vmstat -s', $vmstat);
preg_match('/(\d+) (\w)/', $vmstat[0], $matches);
$memoryLimit = $this->return_bytes($matches[1] . $matches[2]);
}
return $memoryLimit;
}
#http://php.net/manual/en/function.ini-get.php#refsect1-function.ini-get-examples
private function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
}
@firebus
Copy link
Author

firebus commented Aug 8, 2015

exhaust memory_limit (or actual memory on the server, maybe?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment