Saturday, 16 June 2012

Allowed memory size of 33554432 bytes exhausted

There are instances when as a PHP developer one has to face the devil of "Allowed memory size of 33554432 bytes exhausted " problem specially while dealing with large block of data say large images, text files etc.









It is because the default memory size that PHP uses is 32 MB. Now few ideal ways could be like scaling down the images, paginating the bigger SQL queries etc. But to increase the default value of memory size the PHP's init_set function can be used. In the function where one has to deal with such big images or queries insert the below mentioned line above the lines where one has to deal with such big images or queries.

                               ini_set('memory_limit','256M'); 

It increases the memory limit to 256MB in this case. However, any desired value can be set for the same.

If -1 is specified in place of memory size then it implies there is no maximum limit for PHP to use memory.
                               ini_set('memory_limit',-1); 

This little modification usually does the trick.

No comments:

Post a Comment