CActiveRecored memory consuming

Hi,

I’m currently working one an application which should import data from an XML file.

As this file is really big, I’m using XMLReader and ActiveRecords to “parse and load” the data in DB.

Everything is fine when the number of lines to import is below 10.000.

Right now, even with memory_limit set to 180M, I cannot import more thant 40.000 lines into the DB.

I get the following error :


Fatal error: Allowed memory size of 180355072 bytes exhausted (tried to allocate 39 bytes) in /var/www/yii-1.0.9.r1396/framework/db/CDbConnection.php on line 462

I removed the active record stuff to use DAO and I have no more problems (but I would prefer to keep ActiveRecord).

here is a sample of my code :


$xml = new XMLReader();

$xml->open(Yii::app()->params['xmlUrl']);

while($xml->read()) {

  if(($xml->nodeType == XMLREADER::ELEMENT) && ($xml->localName == "Date")) {

	$arData = new Data();

	$arData ->data_id = $xml->getAttribute('id');

	$arData ->data_date = self::convertDate($xml->getAttribute('date'));

	$arData ->data_datetime = self::convertDate($xml->getAttribute('date')).' '.self::convertTime($xml->getAttribute('heure'),':').':00';

	$res = $arData ->save();

	if(!$res) {

		echo "NOK\n";

	}

	unset($arData );

  }

}

$xml->close();

If anyone has an idea, that would be great.

Thank you

This should be caused by the internal logging. You may define YII_DEBUG to false in the entry script.

Big web-based applications usually import and export in multiple requests (i.e. reloading page and continue the process) in order to avoid the error you encountered.

Well,

I haven’t talk about web application ;-).

In fact this is a command line / console part of the application. This CLI is called through cron so … the idea of reloading page is not really OK ;-). When I’ll have more time, I’ll try to find out what’s going on.

Anyway, thank you for giving a try.

You should disable debug mode as Qiang suggested

I just verified this myself in a similar console app. In my case the memory consumption for 10000 records rose from approx. 2M to 18M with debug enabled. Without debug it stayed at 2M (verified with 50000 rec’s).

The reason for you having success with DAO might be because fewer trace messages are generated.

Check memory usage before and after the loop




echo "memory usage: " . memory_get_usage(true)."\n";



/Tommy

if u’r using MSSQL, then no way. PDO’s driver leaks.

Hi

i have the same problem, I’m migrating data

i use yiic.php shell

memory_limit=128M

what part of the code is the problem?

I’m starting with php… sorry for the code (and my english)

table datos: 13500 rows

others: < 1000 rows

index.php




// remove the following line when in production mode

//defined('YII_DEBUG') or define('YII_DEBUG',true);



here my code




    public function migra_prof() {

        $datos = Yii::app()->migra->createCommand("SELECT codigopro, apellido, 

                nombre, docutipo, docunume, sexo, nacionalid, fechainsc, 

                fechanaci, lugarnaci, domicreal, localreal, telefreal, email, 

                domiclab, locallab, teleflab, observ, baja, fechabaja, motivobaja

                FROM `datos`"

                //WHERE codigopro <=1003000"

        )->queryAll();




        //var_dump($datos);

        

        //echo "paso";

        //echo "memory usage: " . memory_get_usage(true)."\n";

        //ini_set("memory_limit","128M");

        foreach($datos as $dato) {


        try

        {

          //echo $dato['docunume'];

          //var_dump($dato);

          

          


          $per=Personas::model()->count('nro_documento='.$dato['docunume']);

          if ($per>0) continue;

          

          $nueva = new Rp_profesionales();

          $persona=new Personas();

          

          $dom_real=new Domicilios();

          $dom_real->origen='RP';

          $dom_real->tipo='GEN';


          $dom_lab =new Domicilios();

          $dom_lab->origen='RP';

          $dom_lab->tipo='LAB';


          $tel_real=new Telefonos();

          $tel_real->origen='RP';

          $tel_real->tipo='GEN';


          $tel_lab =new Telefonos();

          $tel_lab->origen='RP';

          $tel_lab->tipo='LAB';


          $ema=new Emails();

          $ema->origen='RP';


          $nueva->cod_profesional=$dato['codigopro'];

          $persona->apellido=$dato['apellido'];

          $persona->nombre=$dato['nombre'];

          $persona->tipo_documento=$dato['docutipo'];

          $persona->nro_documento=$dato['docunume'];

          $persona->sexo=$dato['sexo'];

          if (empty($persona->sexo)) $persona->sexo='I';

          

          $persona->pai_idPais_nacionalidad=

          Paises::model()->find('cod_pais=:cod_pais',array(':cod_pais'=>$dato['nacionalid']))->idPais;


          $nueva->fecha=Varios::dateconvert($dato['fechainsc'],2);

          //$persona->fecha_nacimiento=$dato['fechanaci'];

          $persona->fecha_nacimiento=Varios::dateconvert($dato['fechanaci'],2);


          $persona->lugar_nacimiento=$dato['lugarnaci'];

          

          $dom_real->domicilio=$dato['domicreal'];

          $dom_real->loc_idLocalidad=

          Localidades::model()->find('cod_localidad=:cod_localidad',array(':cod_localidad'=>$dato['localreal']))->idLocalidad;





          $tel_real->telefono=$dato['telefreal'];

          $ema->email=$dato['email'];


          $dom_lab->domicilio=$dato['domiclab'];


          $dom_lab->loc_idLocalidad=

          Localidades::model()->find('cod_localidad=:cod_localidad',array(':cod_localidad'=>$dato['locallab']))->idLocalidad;


          $tel_lab->telefono=$dato['teleflab'];


          if (empty($dato['motivobaja']))

            $nueva->obs=$dato['observ'];

          else

             $nueva->obs=$dato['observ']."     Motivo= ".$dato['motivobaja'];

          

          if ($dato['baja']=='0') {

            $nueva->estado='ALT';

            $persona->estado='ALT';

          }

          elseif ($dato['baja']=='1' and $dato['motivobaja']=='ANULACION') {

            $nueva->estado='BOR';

            $persona->estado='BAJ';

          }

          else {

            $nueva->estado='BAJ';

            $persona->estado='BAJ';


          }


          


          $nueva->fecha_estado=Varios::dateconvert($dato['fechabaja'],2);


          $motivo=$dato['motivobaja'];

          //echo ' motivo ' .$motivo;

          

          if (empty($motivo)) $nueva->motivo_estado=null;

elseif ($motivo=='MATRICULA INACTIVA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'MATRICULA INACTIVA'))->valor;

elseif ($motivo=='FALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='FALLECIDO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='SALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='FALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='POR FALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='FUERA DE LA PROVINCIA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='FALTA DOCUMENTACION') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='INVALIDEZ') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'INVALIDEZ'))->valor;

elseif ($motivo=='NO TERMINO TRAMITE- NO RET.CRE') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='PERSONAL') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='JUBILACION POR INVALIDEZ') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUBILACION'))->valor;

elseif ($motivo=='MOTIVOS PERSONALES') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='FUERA DE LA PCIA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='RADICADO EN CAP.FED.') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='INHABILITACION JUDICIAL') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUDICIAL'))->valor;

elseif ($motivo=='TRASLADO A OTRA PROVINCIA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='FUERA DE LA PROV.') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='BAJA POR FALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='FALTA DE TITULO Y DOCUMENTACIO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='A PEDIDO DEL INTERESADO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='A PEDIDO DE LA INTERESADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='NO FINALIZO TRAMITE') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='INFORMA FUERA DE LA PROVINCIA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='INFORMA IV ZONA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'SIN ESPECIFICAR'))->valor;

elseif ($motivo=='NO TERMINO TRAMITE MATRICULAC.') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='HOM CULPOSO STJ .RESOL 22415/07 6 ANOS') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUDICIAL'))->valor;

elseif ($motivo=='SENTENCIA JUDICIAL/RESOL 22415/07 STJ') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUDICIAL'))->valor;

elseif ($motivo=='NO HABER EJERCIDO EN LA PROV.') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='NO FINALIZO EL TRAMITE') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='A SOLICITUD DEL INTERESADO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='HOM CULPOSO RESOL 22397/07/ 5 ANOS') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUDICIAL'))->valor;

elseif ($motivo=='SNTENCIA JUDICIAL RESOL 22415/07 STJ') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUDICIAL'))->valor;

elseif ($motivo=='SOLICITUD INTERESADA 31/07/2007') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='SOLICITUD INTERESADO 31/07/2007') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='RETIRO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUBILACION'))->valor;

elseif ($motivo=='PRO FALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='PRESUNTO FALLECIMIENTO') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='A PEDIDO DE LA INTESADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='TRASLADO A OTRA PROV.') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='FUERA DEL PAIS') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='A PEDIDO DEL PROFESIONAL') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='FUERA PCIA-PTA.CESE ACTIVIDADE') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FUERA DE LA PROVINCIA'))->valor;

elseif ($motivo=='PRESUNTO CESE ACTIVIDADES') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'CESE ACTIVIDAD'))->valor;

elseif ($motivo=='NO FINALIZO TRAMITE DE MATRIC.') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'DOCUMENTACION INCOMPLETA'))->valor;

elseif ($motivo=='MOTIVOS PERSONALES Y LABORALES') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='BAJA A PEDIDO DE LA INTERESADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='FALLECIDA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'FALLECIDO'))->valor;

elseif ($motivo=='A PEDIDO  DE LA INTERESADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='SOLICITUD INTERESADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='MATRICULADA COMO LICENCIADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'CAMBIO MATRICULA'))->valor;

elseif ($motivo=='EXPTE. 2889/5 ABUSO DESHONESTO/') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'JUDICIAL'))->valor;

elseif ($motivo=='PERSINAL') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='INFORMA IVZONA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'SIN ESPECIFICAR'))->valor;

elseif ($motivo=='A SOLICITUD DE LA INTERESADA') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RAZONES PARTICULARES'))->valor;

elseif ($motivo=='RES.4774/76 SP') $nueva->motivo_estado= Dominios::model()->find('dominio=:dominio and descripcion=:descripcion',array(':dominio'=>'RP_MOTIVO_ESTADO',':descripcion'=>'RESOLUCION INTERNA'))->valor;


//echo '$nueva->motivo_estado  '.$nueva->motivo_estado;





          $errores= array();

          $grabo_persona=false;

          if (!$persona->save()) {

            //var_dump($persona->getErrors());

            $errores= array_merge($errores,$persona->getErrors());

            throw new Exception('Error validando datos persona');

         }

          

          $grabo_persona=true;


          $nueva->per_idPersona= $persona->idPersona;

          $tel_real->per_idPersona=$persona->idPersona;

          $dom_real->per_idPersona=$persona->idPersona;

          $tel_lab->per_idPersona=$persona->idPersona;

          $dom_lab->per_idPersona=$persona->idPersona;

          $ema->per_idPersona=$persona->idPersona;




          if (!$nueva->validate()) {

            //var_dump($persona->getErrors());

            $errores= array_merge($errores,$nueva->getErrors());

            throw new Exception('Error validando datos profesional');

          }




          if (empty($dom_real->domicilio)) $dom_real->domicilio='SIN ESPECIFICAR';

          if (!$dom_real->validate()) {

            //var_dump($persona->getErrors());

            $errores= array_merge($errores,$dom_real->getErrors());

            throw new Exception('Error validando domicilio real');

          }


          if (empty($dom_lab->domicilio)) $dom_lab->domicilio='SIN ESPECIFICAR';

          if (!$dom_lab->validate()) {

            //var_dump($persona->getErrors());

            $errores= array_merge($errores,$dom_lab->getErrors());

            throw new Exception('Error validando domicilio lab');

          }


          if (!empty($tel_real->telefono) and !$tel_real->validate()) {

            //var_dump($persona->getErrors());

            $errores= array_merge($errores,$tel_real->getErrors());

            throw new Exception('Error validando tel real');

          }


          if (!empty($tel_lab->telefono) and !$tel_lab->validate()) {

            //var_dump($persona->getErrors());

            $errores= array_merge($errores,$tel_lab->getErrors());

            throw new Exception('Error validando tel lab');

          }


          if (!empty($ema->email) and !$ema->validate()) {

            //var_dump($persona->getErrors());

            //$errores= array_merge($errores,$ema->getErrors());

            //throw new Exception('Error validando email');

            $nueva->obs=' email inválido: '.$ema->email;

            $ema->email=null;

          }


          $nueva->save(false);

          $dom_real->save(false);

          $dom_lab->save(false);


          if (!empty($tel_real->telefono)) $tel_real->save(false);

          if (!empty($tel_lab->telefono)) $tel_lab->save(false);


          if (!empty($ema->email))  $ema->save(false);




}

        catch(Exception $e)

        {

                    if ($grabo_persona) $persona->delete();

                            echo "-------------------------------------------------------";

                            echo "\n";

                            echo "Error migrando ";

                            echo $dato['codigopro'];

                            echo " ";

                            echo $dato['docunume'];

                            echo "\n";

                            echo get_class($e)."\n";

                            echo $e->getMessage()."\n";

                            echo "-------------------------------------------------------";

                            echo "\n";

                            print_r($errores);

                            //echo $dato['fechabaja'].'   -   '.

                            // $nueva->fecha_estado.'   -    '.

                            // Varios::dateconvert($dato['fechabaja'],2);


                             


        }

        }


    }




You may try




unset($dato, $per, $nueva, $persona, $dom_real, $dom_lab, $tel_real, $tel_lab, $ema, ..., ...);



at the end of the loop.

(not tested ;D )

/Tommy

hi, thanks, but not work

every iteration consumes approximately 262144 bytes




          echo 'Dif memory usage '.$n.' : '. (memory_get_usage(true)-$mem)."\n";

          $mem=memory_get_usage(true);




try with





for ($i = 1; ; $i++) {

    

    $limit= (($i-1)*$cant_loop);


        $datos = Yii::app()->migra->createCommand("SELECT codigopro, apellido,

                nombre, docutipo, docunume, sexo, nacionalid, fechainsc, 

                fechanaci, lugarnaci, domicreal, localreal, telefreal, email, 

                domiclab, locallab, teleflab, observ, baja, fechabaja, motivobaja

                FROM `datos` LIMIT $limit,$cant_loop" )->queryAll();

        

        foreach($datos as $n=>$dato) {

....

        unset($dato, $per, $nueva, $persona, $dom_real, $dom_lab, $tel_real, $tel_lab, $ema);


        }


    unset($datos);

    echo 'iter = '.$i;

    if ($i*$cant_loop > $total) {

        break;

    }




not work

does not release memory never!!!!

help me, the next table has more rows!

[EDIT]

try with

mysql_free_result




        mysql_free_result($dato);

        mysql_free_result($nueva);

        mysql_free_result($dom_real);

        mysql_free_result($dom_lab);

        mysql_free_result($tel_real);

        mysql_free_result($tel_lab);

        mysql_free_result($ema);

        

        mysql_free_result($datos);




but get

which is the correct use?

There’s some more AR models you can try unset e.g. $nueva->motivo_estado.

Do you attach behaviors in any of the models? That can be the problem.

I’m not sure about unset(), try with only one param per call, just to be sure.

Perhaps you can break up the code into functions to get out of scope (can’t say if it makes any difference in PHP).

Also try to locate where most memory is consumed. Just a thought: perhaps it’s related to the param binding?.

Edit:

Be aware that YII_DEBUG will test true if not defined. If defined before yii is included it will not be set by the framework. Make sure you didn’t define YII_DEBUG before the two lines commented out in index.php.

Edit2: Hmm, I’m a bit dizzy today, with slight fever. Should be obvious from the lack of logic in the above statement <_<

/Tommy

yes

why?

how?

sorry i newbie

Regarding behaviors, I think the problem would be the same as what was discussed

here. If I have got it right, not even an explicit detachBehavior() will help in the same script invocation.

I think you could add memory_get_usage printouts and possibly see which model would benefit most from some workaround (if possible). But perhaps blockwise memory "allocation" would make that a no-go (of course multiple memory increment counters over several iterations would help).

I’m not sure, but I think someone mentioned param binding somewhere, dicussing memory leaks.

Previously I was thinking about what would happen if you read just one dato each iteration, now I believe that’s irrelevant.

BTW. For readibility and ability to maintain (e.g workaround attempt) I believe the huge if/elseif block could be replaced with an PHP associative array plus a single line of AR call.

/Tommy

:angry:, the solution is PHP 5.3…

remove the behavior not an option

are there for a purpose

done.;D

this is an incremental migration, run several times and someday finish

thanks Tommy

Yes, qiang is right, my problem solved :rolleyes:

My problem was the behavior(http://www.yiiframework.com/doc/cookbook/9/ and http://www.yiiframework.com/extension/i18n-datetime-behavior/)

i18n-datetime-behavior: I delete them, putting the code in each model

audit: disable it for certain actions that are read only

now work fine

greetings

It took me all day to figure this out …

I opened an issue http://code.google.com/p/yii/issues/detail?id=879 but was closed "wont fix"

There is a circular reference between the ActiveRecord and the Behavior so that unsetting the ActiveRecord does not get garbage collected since there is still a reference from inside the object…

Simple solution is …

$netwPort = new NetWPort();

… Do your thang…

$netwPort->detachBehaviors();

unset($netwPort);

60,000 Records later … all is well…

Edward