Skip to content

Instantly share code, notes, and snippets.

@iworker
Last active August 29, 2015 14:25
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 iworker/e595e83f4781d087e999 to your computer and use it in GitHub Desktop.
Save iworker/e595e83f4781d087e999 to your computer and use it in GitHub Desktop.
<?php
namespace console\controllers;
use yii\console\Controller;
use common\models\TestModel;
class LeakController extends Controller
{
private function createModels($detach = true, $count = 10000)
{
for ($i = 0; $i != $count; ++$i) {
$model = new TestModel();
if ($detach) {
$model->detachBehaviors();
}
}
}
public function actionTestModel()
{
$test = new TestModel(); // Create temporary object for loading TestModel.php etc.
$time = microtime(true);
$memory = -memory_get_usage();
$this->createModels(true);
echo "Usage of memory with detaching: " . (memory_get_usage() + $memory) . " bytes; Usage of time: " . (microtime(true) - $time) . " s" . PHP_EOL;
$time = microtime(true);
$memory = -memory_get_usage();
$this->createModels(false);
echo "Usage of memory without detaching: " . (memory_get_usage() + $memory) . " bytes; Usage of time: " . (microtime(true) - $time) . " s" . PHP_EOL;
gc_collect_cycles();
$time = microtime(true);
$memory = -memory_get_usage();
$this->createModels(false);
gc_collect_cycles();
echo "Usage of memory with gc_collect_cycles: " . (memory_get_usage() + $memory) . " bytes; Usage of time: " . (microtime(true) - $time) . " s" . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment