Last active
August 29, 2015 14:25
-
-
Save iworker/e595e83f4781d087e999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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