PHP,Joomla,WordPress & Linux based server management, error resolutions and fixing problems.

Wednesday, October 3, 2012

Fix PHP FATAL Memory Error, Allowed Maximum Size of X bytes Exhausted... [Solution]

How to fix PHP Fatal Memory Errors!

Today while processing PNG images in one of my script, i came across this error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 7680 bytes) in image.php on line 33.

So, i thought may be i need to set the memory to somehow really high, as i am using a fairly decent laptop as my development environment with 8 GB of RAM backed by Core i5 SandyBridge processor, so i didn't wasted another second and added the following line to the top of my script :

ini_set("memory_limit" , "1024M");
I know this could be insane,but i was a bit in the flow of coding so i didn't care, amazingly this DIDN'T SOLVED THE PROBLEM. I was totally blank, and i was like, how could this be possible? I mean i know i am processing fairly big enough PNG images which are direct from DSLR but that shouldn't matter, the same images which are same in resolution but in JPEG are working fine. I thought may be this limit isn't working through the script, so i corrected the limit in php.ini. Amazingly, this either didn't solved the problem itself.

After allot of struggle with this, i just tried to relax myself, and said to myself " I need to do something really tricky here!".

So finally i ended up with following line: 

ini_set("memory_limit" , "-1");

To my surprise, this solved the issue instantaneously and my script worked even better, so i thought it may have some draw back issues on my CPU usage, but i monitored the CPU and RAM usage and it didn't went that up as i expected. But who cares for these things when you actually achieve your goal.

So, what does this actually do? This actually tells PHP to override all the memory limits and use as much memory as possible, this won't eat that much memory but it will if you use fairly large images, i was actually processing 8MP.

Keep in mind that huge memory limits can crush your server, and those scripts which run frequently and poorly coded can be a serious pain in rear for daily uses as they may be slow/sluggish and can even cause server crash as well, so use this with caution, as i am on development platform so i didn't care about this, but if you are going to use this trick on other platform other than that of development,this might cause you severe problems as well. So code neat, be clear to yourself, avoid unnecessary code blocks and be simple and straight forward for it.
At last i would say that, everything you go onto, just give it a try,actually you learn only when you take risks, so its better to try many  things, "Practice makes a man perfect.".
Keep this motto in your mind, be positive for yourself and don't worry about what might other think about your coding standards because everyone lives for themselves, so do your code. Comments are welcome, let me know your solutions about this.

This isn't just for image processing but you can use it though at any stage, but first try to allocate the similar sizes of memory like , 32M , 64M, 96M to 256M or even 512M. But make sure to debug your script and know at which stage is it consuming the most of the memory, so you can actually know what is causing it to hog.

Happy Coding

, ,