Is it possible to clear your AR objects from php memory?

Hello, i am using yii 1.1.14. I am pulling a gigantic dataset that kills PHP due to using too much memory. In order to avoid, this i decided to create a loop where i pull 100 rows at a time, do something with them, and pull another 100 rows. unfortunately, even though i am unsetting the variables that i pull, my memory keeps growing. form what i’ve read, this is possibly due to circular references in AR. Is there any way to get around this? how can i completely destroy my AR objects?

thanks!

It is not recommend to use AD classes when you are working on a sets having more than 10 rows. Instead you should use lower database abstraction layer. Try to write SQL queries directly and select only columns that you really need.

Hmmm. So, if i have a bunch of models, and they all interact with one another. each model contains its own business logic and some of that call methods on it’s associated models etc. Now, i want to have an export that pulls several thousand of these and outputs stuff based on the business logic etc i mentioned above. are you saying that the only way to do so is by using raw sql and then rewriting all the business methods from scratch to work on arrays? is there really no way whatsoever to clear an AR model from memory once it has been created?

As far as I know there’s no way to do it manually but PHP’s garbage collection should do it automatically.

  1. Make sure your code doesn’t hold any reference to the unneeded AR instances somewhere.

  2. Check your business logic. It might involve some operations on a has-many relation which causes lazy loading to kick in and you might have a few thousands AR instances instead of 100.

You mentioned circular references. It’s unlikely because Active Record in Yii1 doesn’t implement Identity Map or similar pattern so (for example)


$author

and


$author->posts[0]->author

are two different instances of Author model.