Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ORM Memory leak
  • I have a task that creates and saves ORM objects into database. I noticed that every $object-save(); php script consumes more & more memory even tho the variable used to create the object in the loop is the same. 

    I made sure that app profiling and database profiling is disabled. Tried with Fuel 1.7 and 1.7.1 - same results. 

  • The ORM caches all objects it sees for re-use, it has never been designed for batch operations. Perhaps this is the issue?
  • Thanks for quick response. That is batch processing script. Is there any ways to clear cache? I added \Cache::delete_all(); but it does not help.
  • Harro VertonHarro Verton
    Accepted Answer
    The ORM caches in memory, on a per-request basis, which is why memory consumption grows with every object created.

    There is no method to reset the cache, but you can easily add one to your model, or to a base model class if you have one.

    All you need to do is

    static::$_cached_objects[$class] = array();

    where $class is a string with the exact name of the Model class name who's cache you want to delete.
  • perfect! thanks a lot. That saves me some time ;) Implemented - works like a charm.

    public static function flush_cache()
    {
    static::$_cached_objects[get_class()] = [];
    }

  • WanWizard

    Why do we add the method to ORM package?
    This kind of question comes some times.

  • Because I think this is a workaround for something that is implemented wrong. Because the ORM was never designed for batch operations. Because any effort put into v1 is not put into v2.Because ....

    You know how it works, create a PR, Steve can look at it, and if he accepts it's in. :-)
  • I respect your thought, and I have never met a case which I need the clear method.
    If you must clear cache, you are doing something wrong. I got it.

    Thank you for your explanation.

  • That is not what I wrote. I said that the way the ORM is written makes it not very suitable for batch operations. It was written to back interactive CRUD operations, and in that scenario, there is no need for clearing the cache, it will never contain a lot of objects.

    I did not say you are doing something wrong.
  • To talk about ORM batch processing. I searched website and see this.

    http://docs.doctrine-project.org/en/2.0.x/reference/batch-processing.html

    It is interesting that to clearing may help a little (or a lot) to free memory.
    Yes ORM is not good for batch processing as @Harro Verton said but, Do you think FuelPHP may have this clear option in the future?
  • That link doesn't work here, it says "You don't have access to this version.".
  • I could see the page.

    Bulk inserts sample like this:
    <?php
    $batchSize = 20;
    for ($i = 1; $i <= 10000; ++$i) {
    $user = new CmsUser;
    $user->setStatus('user');
    $user->setUsername('user' . $i);
    $user->setName('Mr.Smith-' . $i);
    $em->persist($user);
    if (($i % $batchSize) == 0) {
    $em->flush();
    $em->clear(); // Detaches all objects from Doctrine!
    }
    }


  • Ok, so just a method to clear cached objects?

    Should be easy to add, I already gave a pointer on how to do it earlier up. It could have been in already. ;-)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion