PHP Fatal error: Out of memory

Sometimes when writing complex scripts or scripts that need to use allot of memory you may end up receiving this error message “Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/your/script on line x”.

Firstly it is a common misunderstanding to think this is related to storage space, it has nothing to do with storage space and is actually referring to the amount of memory (RAM) your php script may consume, this is intentionally set quite low to avoid abnormal script behaviour or cases where an infinite loop may occur in your scripts.

However obviously sometimes you will need to go above this limit, especially when working with large amounts of data – in those cases you may increase the memory higher using the php ini_set function, on WebDesires hosting this should work out of the box, however on some other hosting services this may be disabled and you may need to contact your hosting provider.

So to increase the memory allowed for your PHP script to operate you simply need to run this code (usually at the top of the script but actually you can do it at any point you see fit).

[php]ini_set(‘memory_limit’, ’25M’);[/php]

Please be responsible when increasing a scripts maximum allowed memory limit, revise your code if necessary to clear any unused variables during or after loops so that you are as memory efficient as possible.

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.

Leave a Reply

Your email address will not be published. Required fields are marked *