KJedi
(Konstantin Mirin)
November 2, 2009, 3:08pm
1
Here is the code, that gives memory leaks when deals with file with long lines (less than 1Kb):
protected function insertFile($file, $table, $offset)
{
$fl = fopen(self::IMPORT_DIR.$file,'r');
$headings = fgetcsv($fl, 1024, "\t");
$db = Yii::app()->db;
$sqlPart = 'INSERT INTO `'.$table.'` (`'.implode('`,`', $headings).'`) VALUES(\'';
$suc = 0;
$err = 0;
$i = 0;
while ($i < $offset)
{
fgets($fl);
$i++;
}
$i = 0;
while (!feof($fl) && $i < self::LINES_PER_PROCESS)
{
$row = fgetcsv($fl, 1024, "\t");
$sql = $sqlPart.implode('\',\'', $row).'\')';
unset($row);
try
{
$cmd = $db->createCommand($sql);
$res = $cmd->execute();
unset($cmd);
unset($res);
$suc++;
}
catch (CException $e)
{
echo $sql."\n";
$err++;
unset($e);
}
unset($row);
unset($sql);
$i++;
}
fclose($fl);
echo $suc." lines inserted; $err errors encountered\n";
}
Why does it give errors??? Where is memory leaks? I unset all variables I create, how this code can leak?
qiang
(Qiang Xue)
November 2, 2009, 3:16pm
2
I guess it’s because you set YII_DEBUG to be true and thus the logging messages (in CDbCommand) are accumulated as you execute many SQLs. You can set YII_DEBUG to be false.
Vince
(Vadimg88)
November 2, 2009, 3:30pm
3
P.S no need to unset every single variable you set.
KJedi
(Konstantin Mirin)
November 2, 2009, 6:07pm
4
I know there is no need for this, I was trying to find the way to make it work
thanks Qiang, will try it now.
KJedi
(Konstantin Mirin)
November 2, 2009, 8:10pm
5
I found out that it already had YIIDEBUG commented out. I also commented out logging in the config file.
But the problem persists. What else can be done here?
KJedi
(Konstantin Mirin)
November 2, 2009, 8:12pm
6
I also found out that memory is usually our on the various lines of YiiBase file where public static function t is defined.
It is very strange cause I don’t use i18n in the app, how can this method be called at all?
qiang
(Qiang Xue)
November 2, 2009, 8:18pm
7
Can you describe what you mean by "memory leak"? How much memory is consumed? Is the consumption piled up?