0

Using with command in yii give memory error.When I use 3 tables it works fine. As I add 4th then it starts give an error on server. Locally it works fine

$criteria->with = array('users0','businessUnits','skills','questions');
$criteria->together = true;

$models = Company::model()->findAll($criteria);

It gives an error Allowed memory size of 100663296 bytes exhausted (tried to allocate 32 bytes) in

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Haseeb Ahmad
  • 7,914
  • 12
  • 55
  • 133
  • 1
    possible duplicate of: http://stackoverflow.com/questions/415801/allowed-memory-size-of-33554432-bytes-exhausted-tried-to-allocate-43148176-byte – Sagar Guhe Mar 29 '16 at 06:22
  • or this: http://stackoverflow.com/questions/561066/fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-codeigniter-xml – Sagar Guhe Mar 29 '16 at 06:23
  • yeah, it is a common issue. Maybe your querying huge database tables leading to that error? a simple trick would be to increase memory_limit... But still you need to investigate why your memory is exhausted... could be a lot of things, your query, loops, etc. or even server configurations. – Severino Lorilla Jr. Mar 29 '16 at 06:25
  • I don't know which version of `Yii` you are using but I assume that you are using latest version, and in `Yii2` we have method like `asArray()` so you should use this, as you might be querying huge database and fetching huge records consume lots of memory if you using `ORM`, so use `array`. – Sagar Guhe Mar 29 '16 at 06:31
  • 1
    Please read this http://stackoverflow.com/questions/32398518/php-allowed-memory-size-memory-size-exhausted/32399108#32399108 – SiZE Mar 29 '16 at 06:35
  • @SagarGuhe is it query issue? issue in it $criteria->with = array('users0','businessUnits','skills','questions'); – Haseeb Ahmad Mar 29 '16 at 06:48

1 Answers1

1

Problem - Fatal error: Allowed memory size of 33554432 bytes exhausted

Reason for Fatal error: Allowed memory size of X bytes exhausted

Recently I have encountered this error with one of my php application. Like any other programmer I thought of googling the issue.

After some research I came to find that for some reason my script is taking all the memory space. As I was on shared hosting & ini_set was disabled by the server admin.

I had no other option than finding the root cause of the issue.

Incorrect Solution: Solutions such as increase the memory is not the correct solution. By that you are allowing your bad script to consume all the memory.

So how to solve the issue?

Suppose you are creating a variable that is carrying so much data. In loop you are reassigning value to that.

So you reassign too much data to a same variable, though its values are getting updated but memory is not getting freed yet as variable is in use & garbage collector is not clearing that memory.

Correct Solution: To avoid Allowed memory size exhausted error you can set the value to null. By doing so you are telling garbage collector whatever kept in memory for that variable is not required & freed. Garbage collect will immediately clear the space.

Michael
  • 2,631
  • 2
  • 24
  • 40
Avinash Sinha
  • 534
  • 3
  • 16
  • But i dont't think so I use any variable which reassign – Haseeb Ahmad Mar 29 '16 at 06:49
  • What are you actually doing here? Can you select all data from table? – Avinash Sinha Mar 29 '16 at 06:52
  • 1
    I think the moment he does the findAll() call, the php interpreter runs out of memory due to the high number of models it has to store. @HaseebAhmad you will probably have to use something else other than active record if you want to loop through a large number of models. Using DAO plus the CDbCommand::query() method might work better as that one steps through every model as you work through your loop, so you don't need a large bulk of memory in 1 go. – georaldc Mar 29 '16 at 22:40
  • @georaldc you are right. This isssue is due to findAll() – Haseeb Ahmad Mar 30 '16 at 05:03