0

I have issues with symfony 1.4 a task running daily. To make simple the task download an xml then read it.

I have notice that the memory used increase between each "node" in the xml.

For instance, somewhere in the script I do that:

 foreach($prd->ArtistList->Artist as $art)
          {
  echo "start foreach artist  ", memory_get_peak_usage(1), " bytes of ram.\n";
            if(!$artist = ArtistTable::getInstance()->findOneByRoleAndName($art['role'], $art['name']))
            {
              $artist = new Artist();
              $artist->role = $art['role'];
              $artist->name = $art['name'];
              $artist->save();
            }

            if(!$pha = ProductArtistTable::getInstance()->findOneByProductIdAndArtistId($prd_id, $artist->id))
            {
              $pha = new ProductArtist();
              $pha->product_id = $prd_id;
              $pha->artist_id = $artist->id;
              $pha->save();
            }

            $pha->free(true);
            $artist->free(true);
            unset($pha);
            unset($artist);

            echo "end foreach artist  ", memory_get_peak_usage(1), " bytes of ram.\n";
          }
          unset($art);

I can see that between two memory_get_peak_usage(1) end and start, memory used increase... I don't know what else am I supose to do to keep it at a same level...

Do you have any idea how to solve that?

Thanks!

kind-mug
  • 77
  • 1
  • 8

1 Answers1

1

I've monitored slow performances in the past within a symfony application. Found out that those built in ORM's (Doctrine in my case) query methods are really a bad thing, especially within a foreach loop.

I'd suggest you to try using RAW SQL instead.

If you don't know how to use raw sql within symfony this should do the trick

$connection = Doctrine_Manager::connection();
$statement = $connection->prepare('your sql statement');
$statement->execute();
$result = $statement->fetchAll(PDO::FETCH_NUM); // or whatever fetch mode you want to use
Rommy
  • 497
  • 1
  • 4
  • 16
  • Thanks I ll give it a try and let you know :) – kind-mug Apr 08 '14 at 15:44
  • Memory is still increasing, for instance I notice that : start foreach artist 214695936 bytes of ram. end foreach artist 214958080 bytes of ram. How can it be increasing ... driving me crazy ;) – kind-mug Apr 08 '14 at 16:14
  • It's PHP so you don't have that control over memory as in e.g. C. There are probably some variables kept with the connection info, queries, framework vars, etc. You would have to skip using the framework at all if memory usage is such an issue. For me usually skipping Doctrine hydration was enough to save loads of RAM (either using native SQL as Rommy suggests or calling `execute(array(), Doctrine_Core::HYDRATE_NONE)`). – Michal Trojanowski Apr 08 '14 at 21:10