Allowed Memory size exhausted
Created: Last updated:
Somewhere in you application you are pulling data and storing the data in a variable which exceeds the limit set in the PHP configuration. The data set you pull is either too big or you are causing a loop adding data to memory until the limit is reached.
But please, read on.
What memory size?
Don't we like errors just like this one? You get a Fatal Error and you have no clue what it is about. Everything works fine with your web server, PHP or Zend application and suddenly you run into this error telling you
- "Fatal Error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 384 bytes) in file/something.php on line 245".
You think: "What the heck, I have 4GB of RAM in my server or PC." You also know that the operating system handles virtual memory and an application usually gets about 2GB of virtual memory. So, what is the real problem here?
One thing is clear, it is neither a problem with physical memory nor the virtual memory allocated by the OS. It is a limitation set in the configuration by PHP. Now the big question is this: "Do we have enough memory set in the configuration?" The answer is "probably yes" because the real problem is something else! Lets think about what is happening with memory in general.
Cause and Reaction
Every application uses memory to store data, temporarily, at run time. The application in our case is PHP and we should not worry about it. It handles everything but it has set its own limit in the configuration, that is where exhausted is coming from. Check your php.ini file or phpinfo and search for memory_limit. Within phpinfo it is in the PHP Core section. In my case it is set to 32M (MegaBytes) which in most cases is enough.
First thing to know, memory stores data from all the files we load and with them all the variables, functions and classes. Keep in mind that properties in classes store data. Although the Zend Framework (or other frameworks and CMS like Wordpress, Joomla, Drupal for that matter) opens and stores a lot of files the sum of all this data is far from that limit. Imagine, the whole Zend library is only 22.3MBytes and never ever is the whole library loaded. The bigger question is what other data makes its way into memory.
Data stored in memory
Basically every framework has a database in the background. So, our biggest concern here are the databases. When we pull data from a database we store the data in local variables or the properties in our classes.
Lets take a look at the php.net website and the page about the resource limit. There is these nice little sentence and what the resource limit is for: "This helps prevent poorly written scripts for eating up all available memory on a server." Well, there we have it! If we hit this fatal error we probably have a problem in our script.
If you run into this fatal error on a regular basis you should really look at how you are handling data that you pull from a database. Ask yourself some of the following questions. Do you pull data with fetchAll or do use use filters? Do you pull the same data over and over again and do you stored it in different variables?
I occasionally run into this fatal error when I made some change in a script and this forced the execution for some—I have to admit it—stupid reason into a loop. The loop is pulling data repeatedly from the database and stores the result at every cycle into memory until the memory, as the error says, is exhausted. If it was not for the data we would eventually run into the next error which is the max_execution_time limit for the scripts, i.e. the execution time runs out and we get the error for that.
Hopefully we know when, what or where we made the change that forced the loop and can fix it immediately. If not, good lucky debugging your scripts and find the change that causes the trouble.
The fatal error has a file mentioned. Note that this file is probably not the source of your problem! It is only the point where the latest request for memory was coming from. However, if it is some file related to databases you can bet your life that the problem is exactly what I have mentioned above. You are pulling and storing to much data in what ever way from the database.