Skip to content

Instantly share code, notes, and snippets.

@chrisLeeTW
Created May 25, 2017 04:09
Show Gist options
  • Save chrisLeeTW/5f60cb0ea4c1de08605b381c8a4e47e3 to your computer and use it in GitHub Desktop.
Save chrisLeeTW/5f60cb0ea4c1de08605b381c8a4e47e3 to your computer and use it in GitHub Desktop.
php memory leak test.
<?php
class test
{
private $test = null;
public function testMethod()
{
$this->test = "abc";
$count = 0;
while ($count <= 10000) {
$this->test = "abc" . $this->test;
$count++;
}
//echo $this->test . PHP_EOL;
}
public function testMethod2()
{
$test = "abc";
$count = 0;
while ($count <= 100000) {
$test = "abc" . $test;
$count++;
}
}
}
echo "[setup_start] memory usage : " . number_format(memory_get_usage() / 1048576, 2) . " MB" . PHP_EOL;
$objArr = [];
for($i = 0; $i <= 100; $i++) {
$objArr[$i] = new test();
$objArr[$i]->testMethod2();
$objArr[$i]->testMethod();
echo "[exec_end] memory usage : " . number_format(memory_get_usage() / 1048576, 2) . " MB" . PHP_EOL;
$reflection = new \ReflectionObject($objArr[$i]);
foreach ($reflection->getProperties() as $property) {
$property->setAccessible(true);
$property->setValue($objArr[$i], null);
}
$objArr[$i] = null;
echo "[down_end] memory usage : " . number_format(memory_get_usage() / 1048576, 2) . " MB" . PHP_EOL;
}
$objArr = null;
echo "[down_end] memory usage : " . number_format(memory_get_usage() / 1048576, 2) . " MB" . PHP_EOL;
echo "[down_end] peak memory usage : " . number_format(memory_get_peak_usage() / 1048576, 2) . " MB" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment