2

I use symfony 1.4 (from a task) and Doctrine 1.2 to import data from a lot of files.

It works well, but after a few minutes I get an "Allowed memory size exhausted" fatal error from PHP, not in my code but in Doctrine…

Call Stack:
    0.0004     325216   1. {main}() C:\wamp\www\XPower\symfony:0
    0.0125     587704   2. include('C:\wamp\www\XPower\lib\vendor\symfony\lib\command\cli.php') C:\wamp\www\XPower\symfony:14
    1.9202    6981872   3. sfSymfonyCommandApplication->run() C:\wamp\www\XPower\lib\vendor\symfony\lib\command\cli.php:20
    1.9233    6983528   4. sfTask->runFromCLI() C:\wamp\www\XPower\lib\vendor\symfony\lib\command\sfSymfonyCommandApplication.class.php:76
    1.9234    6983688   5. sfBaseTask->doRun() C:\wamp\www\XPower\lib\vendor\symfony\lib\task\sfTask.class.php:97
    3.0794    7047088   6. dataImportTask->execute() C:\wamp\www\XPower\lib\vendor\symfony\lib\task\sfBaseTask.class.php:68
    3.2731    8663752   7. dataImportTask->traiterDossier() C:\wamp\www\XPower\lib\task\dataImportTask.class.php:36
    7.7762   17906824   8. dataImportTask->traiterDossier() C:\wamp\www\XPower\lib\task\dataImportTask.class.php:55
  264.4390  133557080   9. xPower::importerXml() C:\wamp\www\XPower\lib\task\dataImportTask.class.php:64
  264.5919  133609328  10. xPower::importerFichierXml() C:\wamp\www\XPower\lib\XPower.class.php:312
  265.4313  134048568  11. InverterTable->findOneBySerialNumber() C:\wamp\www\XPower\lib\XPower.class.php:445
  265.4313  134048776  12. Doctrine_Table->__call() C:\wamp\www\XPower\lib\XPower.class.php:445

i've tried adding some "->free()" to Doctrine calls, but now it seems to fail on InverterTable->findOneBySerialNumber(), a "magic" Doctrine method.

How can I fix my problem ? Do I need to write my own "findOneBySerialNumber" method, adding a call to "free()" ?

Manu
  • 4,410
  • 6
  • 43
  • 77

1 Answers1

4

It's such a common problem with doctrine. You have to use pcntl_fork.

I always use this gist as an example.

pcntl_fork doesn't work on Windows because it requires an OS that follows the POSIX standards.

It might also be enought to just help doctrine free objects its objects, here's a great answer about that.

If you got some The MySQL server has gone away:

When using Doctrine and MySQL, it might be necessary to add the following of the child thread code:

Doctrine_Manager::connection()->close();

This will close the database connection, and Doctrine will automatically recreate it at the first query.

Community
  • 1
  • 1
j0k
  • 22,600
  • 28
  • 79
  • 90
  • that's too bad. Now that I think about it, I have a lot of files now because it is the first import, if I run my task regularly I shouldn't have any problems. – Manu Apr 18 '12 at 14:54
  • I've update my answer with an other answer about freeing doctrine object, maybe it will be much easier this way. – j0k Apr 18 '12 at 15:17
  • Well ok, I'll try profiler: false and OH MY GOD everything is so fast now ! O.O lol nice tip, thanks :) – Manu Apr 18 '12 at 15:34
  • Great answer but I'm getting "mysql server has gone away". The accepted solution to this is to use mysql_close followed by mysql_connect... now need to figure out how to do taht with symfony. – cmc Feb 14 '13 at 14:56
  • ... got it. Edited answer accordingly. – cmc Feb 14 '13 at 15:17