CakePHP error Allowed memory size exhausted

Deadly 提交于 2020-01-06 05:36:06

问题


On one of my PHP applications I get Allowed memory size exhausted error.

What did i done wrong to get that error?

It's showing on pages where i use more-less usual cakephp methods like 'find', to select record from database table, on index (list) pages.

How can i fix that? problem is that i can not set memory size on commercial servers....

Thank you in advance!


回答1:


Research the 'Containable' behavior for your models. It's a core behavior, and it sounds like you need to add it to your finds, to limit the amount of data they're pulling back. Also, start looking at replacing finds with custom queries to speed things up and further limit the amount of data being returned.




回答2:


As Stoic suggested, you can increase your memory limit in your .htaccess file but you should really try and figure out exactly why it is happening, because even the default memory limit should be enough for most small-to-medium apps and databases and the higher you increase your memory limit, the sooner you're going to have to upgrade your hosting plan.

Using a memory inspection tool as Alfred suggested would be one option, but if you app is reasonably basic and the action it's failing on isn't that complex, you can look for tell-tale signs within your code manually. To begin with do you have recursive set to 2 anywhere around your find ? If so, try changing it to -1 temporarily. If not, you can add $this->Model->recursive = -1; before your find to make sure it is set.

Of course, you query might fail if you do this, but at least then you'd know what was eating all of the memory. You could then look at a more efficient custom query in place of the Cake automagic one which often saves a huge amount of overhead, especially on complex queries based on multi-level model associations.




回答3:


There are other issues that can lead to this error.

An endless loop for example, like I just implemented unintentionally.

I wanted to the application to render an ajax-view for every json-call, and I ended up with this in the App controller:

function beforeRender() {

    parent::beforeRender();

     if ($this->params['ext'] == 'json') {

       $this->render('Elements/json');

    }

}

which seems to go repeatedly back to the beforeRender-method.

Maybe this helps someone who's going through the same issue.




回答4:


Maybe you can inspect memory usage using a tool like xdebug. Next you should try and optimize that part(and only that part => no premature optimization).




回答5:


You can use something like the following in your .htaccess file:

php_value memory_limit 16M

This should help you setup memory limit without fiddling with your PHP INI file. :))
Make sure this is the first line in your .htaccess file, generally..



来源:https://stackoverflow.com/questions/6483162/cakephp-error-allowed-memory-size-exhausted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!