Skip to content

Instantly share code, notes, and snippets.

@cccaballero
Last active July 27, 2022 16:05
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 cccaballero/e8e501671c387e035f24ab76774b6e7d to your computer and use it in GitHub Desktop.
Save cccaballero/e8e501671c387e035f24ab76774b6e7d to your computer and use it in GitHub Desktop.
Yii2 console app memory leak
<?php
namespace console\controllers;
use yii\console\Controller;
class TestController extends Controller
{
public function actionTest(){
$db = \Yii::$app->db;
$chunkSize = 1000;
$chunkCount = 0;
$columns = [
'id',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'aa',
'ab',
'ac',
'ad',
'ah',
'ai',
'aj',
'ak',
'al',
'am',
'an',
'ao',
'ap',
'aq',
'ar',
'as',
'at',
'au',
'av',
'aw',
'ax',
'ay',
'az',
'ba',
'bb',
'bc',
'bd',
'be',
'bf',
'bg',
'bh',
'bi',
'bj',
'bk',
'bl',
'bm',
'bn',
'bo',
'bp',
'bq',
'br',
'bs',
];
$value = [
null,
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'aa',
'ab',
'ac',
'ad',
'ah',
'ai',
'aj',
'ak',
'al',
'am',
'an',
'ao',
'ap',
'aq',
'ar',
'as',
'at',
'au',
'av',
'aw',
'ax',
'ay',
'az',
'ba',
'bb',
'bc',
'bd',
'be',
'bf',
'bg',
'bh',
'bi',
'bj',
'bk',
'bl',
'bm',
'bn',
'bo',
'bp',
'bq',
'br',
'bs',
];
$values = [];
while (true) {
var_dump(memory_get_usage());
$values[] = $value;
if ($chunkCount >= $chunkSize) {
$db->createCommand()->batchInsert('test', $columns, $values)->execute();
$chunkCount = 0;
$values = [];
}
$chunkCount++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment