PHP Fatal error: Allowed memory size exhausted

Hello. I have been running into a problem, and after getting absolutely nowhere on my own I am desperately hoping that someone here can point me in the right direction.

I am running a Yii-based site ( version 1.1.8 ) on a CentOs server (5.6 x86_64). It is running Apache 2.2 with PHP 5.3.8 and MySQL 5.1.56

The problem is this: with every single page request that goes through Yii my server spits out the following (both onto the html page and into the error log):

I have increased the maximum allowed memory in my php file over and over again, but no matter how high I set the limit (went up to 512MB), the same error occurs (although the particular numbers obviously increase).

When I load a non-yii php page (simple php test files) I have no issues at all. When I run the same yii-site code on my local machine I have no issues at all. I installed a new, blank yii site onto the server (the default testdrive sample site) and that too gives me the memory error. When I turn on the debugger I see no problems with any of the page requests, memory usage is all normal. The problem appears to be happening before any of my own code is loaded, at some point during the yii framework bootstrap load. The fact that the error says “Unknown on line 0” doesn’t help to point me in any direction whatsoever.

The server is running APC and memcached - and I thought that might be the problem - but even when I remove them from my php.ini I still get the errors. It makes no difference whether I use yii.php or yiilite.php in my index file. The debugging lines in the index file are also commented out.

Does anyone have any suggestions as to what might be happening here? Any suggestions as to how I might even approach solving this problem?

Anyone who can help will immediately become my new best friend.



Have you checked the Yii requirements on the server?

Yep. All are passed and in green except for three warnings in yellow:

PDO PostgreSQL extension

SOAP extension

GD extension

Really strange… when you say "testdrive sample site" you mean the default code generated by yiic ?


some ideas to check:

  • check the phpinfo() to se the value of memory_limit (could be that there are more php.ini files)

  • try to set memory_limit from index.php with


This looks like misconfiguration, possibly .htaccess or hosting memory issue.

check out:

Eventually put up here your .htaccess content…


Sorry, when I say "testdrive site" I mean the base sample site generated by running yiic. I worried that perhaps it was an issue with the relations in my own models so I wanted to try a site with no attached database and no customized models.

My php.ini memory limit is listing the limit correctly.

The only solutions that I have seen suggested elsewhere are to increase the maximum memory size. What strikes me as odd is that whenever I increase the memory size, it merely raises the ceiling level that is ultimately crashed into anyways. If I set the limit to 200, the error says 200MB limit exceeded. If I raise the limit to 500, the error says 500MB limit exceeded. It seems like no matter how high I set the limit it will always be reached. Having a php memory limit of 256MB (as it’s set now, and as is shown in the error message from my op) strikes me as being excessively high anyways. Other threads suggest raising the limit to 56M or 96M, and I have more than doubled that already.

The server is a dedicated webserver being hosted by inmotionhosting. According to the linked thread it seems that inmotion in particular has problems. I will try following the advice in that thread and see what happens.

Thanks for the help.

hmmmm… the errors seem to have stopped.

I upped the memory limit in my php.ini to 512M and then added the line to index.php as you suggested


That appears to have done the trick. Hopefully it stays that way.

As a curiosity, isn’t 512M a ton of memory to allocate to a single PHP script? I thought yii in particular was meant to be very lightweight, but reserving half a gig per script strikes me as extremely non-lightweight.

Thanks for all of your help though! It’s great to have some new best friends.

Yii does not need so much memory… should be something with the server configuration, but difficult to say what without investigating…

After 24 hours with the above changes the error log contains five new memory error entries. That’s a big improvement over having an error on every request, but obviously still not great.

If it is in fact a server configuration problem like you’re guessing, do you have any suggestions on how I might go about figuring out the config problem? Any particular tools I could try or tips on places that I could look? This may be a bit beyond the scope of a Yii Bug, but I am still very new when it comes to server setups… particularly on a linux system.


Not really sure… you would need to profile the script to try to find where the memory is taken…

To begin try with Yii performance profiling -

Hello Yii-Fans,

I’ve had the same problem.

My Solution: I set the configuration-value of "display errors" in the "php.ini" to "on". And now, my problems went away.

For Server-Admins with Plesk-Administration:

Create a file "vhost.conf" in the "conf"-Directory of domain.

Edit the file and add following lines:

php_value memory_limit 128M

php_admin_flag display_errors on

Then save the file and return to the shell-prompt.

Execute the command: /usr/local/psa/admin/sbin/httpdmng --reconfigure-domain domain

Now: Feel again the wonderful Magic of this Framework. ;-}

Long time back I also having the same problem,but I contacted the In Motion technical support team and solved my problem

Memory limit errors in PHP are tricky to troubleshoot, remember that the OS and service(apache) may impose their own limits on PHP - like someone said earlier, try to set the memory limit in apache if the error returns, and for future reference :)

I’m experiencing the same problem here on my development system (windows 7, zend server 5.6, php 5.3 and memory_limit 128M)

I think something is wrong with the Yii framework (probably Menu widget?) because I’ve never had this error before using Yii

has anybody figured out how to fix this error without extending the memory limit?

128M is a ton of memory for Yii. Typical website uses no more than 8—9 MB per page run. Can be a bit more if AR is used heavily. CMenu was used in production projects by many-many people so there shouldn’t be any issue with it.

Either you have some server misconfiguration or using AR without limit with huge amount of data.

exactly, 128M is a lot of memory for a single user development machine. that’s actually what makes me nervous using Yii in a production server!

I don’t use ARs at all. last time I had this error was on a simple page with a static view (just printing an html form) and a couple of CMenu widgets. passing different values to the CMenu widgets raised some memory errors

the confusing part is that the error is not always persistent and reloading the webpage sometimes makes the error go away!

I think if this was a server misconfiguration I would have the error in my other projects too. I’m not much of a server admin though so if there’s any (mis)configuration capable of raising this error I would appreciate it if you point me into the right direction :)

Can you create a simple application that produces this error in your environment so you’ll send it to us and we’ll be able to try reproducing it?

Or you are surely editing the wrong PHP.INI file

Sometimes there are 2 ini files:

  • /etc/php5/apache2/php.ini

  • /etc/php5/cli/php.ini

I am just in the process of memory optimization in my very large application (each request can return more than 40mb of XML), and I can say that for a normal Yii application, each request should not use more than 15MB.

My application was using 100MB per request, now optmized it uses ~25MB.

These things helped immenselly:

  • Removing ioncube from Yii, and using APC: ioncube doubles the memory usage, and APC doesn’t work with it. APC alone on Yii lowered the memory about 30%;

  • Don’t use AR when too many record, like exporting XML. Making the queries by hand, the performance increased more that 30%, and memory usage lowered by 20%;

  • Use a newer PHP version. Old PHP versions have many memory leaks.

I did a large study of Yii memory usage, I can say for sure that the problem isn’t with Yii.

I haven’t been able to reproduce this error so far. all I get is random fatal errors which will be gone by refreshing the page!

this is the latest error message I got, trying to write a new module:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 541975884 bytes) in D:\WebServer\hosts\localhost\yii-1.1.10.r3566\framework\YiiBase.php on line 418

as you can see the script tries to allocate more than 516 MB while the memory limit is 128MB