anon
anon

Reputation:

php memory_limit issue

one of my script required large memory. and probably because of this I am getting following error

Fatal error: Out of memory (allocated 42729472) (tried to allocate 32 bytes)

I tried to change memory_limit by using ini_set('memory_limit', '256M'); on the top of my page but still I am getting same issues. I also tried ini_set('memory_limit', '512M'); but no luck.

Upvotes: 1

Views: 1908

Answers (3)

teemitzitrone
teemitzitrone

Reputation: 2260

Do you have access to the php.ini file? Then change the memory_limit value there.

You should also analyze your script. Lots of database transactions? Many of them in loops? Lots of loops? Lots of temporary variables? A clever usage of unset can make a huge difference.

Also nice to know: Performance Considerations and Garbage Collection

Edit (a possible answer to your question why memory_limit doesn't work with ini_set)

memory_limit integer This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.

Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using --enable-memory-limit in the configure line. This compile-time flag was also required to define the functions memory_get_usage() and memory_get_peak_usage() prior to 5.2.1.

When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.

source: php.net

Upvotes: 4

soulmerge
soulmerge

Reputation: 75704

I was having a similar issue earlier. In my case it was the suhosin patch preventing my script from acquiring more memory.

Upvotes: 0

John Franklin
John Franklin

Reputation: 4912

That's crashing after about 42M, which suggests to me that the PHP interpreter didn't get the memo. Try the same ini_set() in a test page with the phpinfo() output and look for the memory limit.

Can you set it in the php.ini file instead?

Upvotes: 3

Related Questions