2

Using ini_get('memory_limit') will get what is specified in the php.ini file (i presume). But if i run ini_set('memory_limit', 'somethingInsanelyHigh') and then run ini_get(..) again, it will return what i previously set it to. This brings me to believe there's no real way to increase the memory limit and detect if it's actually increased, is there?

What does ini_set(..) actually do? Does it just update a variable somewhere or does it actually increase the limit? And if so, how do i know if it worked or not?

1 Answer 1

6

ini_set returns the old value on success, or FALSE on failure. So you might want to check it:

if (ini_set('memory_limit', 'somethingInsanelyHigh') === false) {
  // cannot set memory_limit
}

Also, take into account that this will set the maximum amount of memory that the current PHP process will take, so if you are going to have multiple concurrent requests to this script, you will want to have something reasonable, in case your script really eats so much memory.

Also setting it to -1 means no memory limit.

edited: disabling memory limit is setting the value to -1, not setting it to 0.

Sign up to request clarification or add additional context in comments.

8 Comments

See this example that i posted earlier: pastebin.com/haRC9BZW So it does indeed set the value correctly. this will set the maximum amount of memory that the current PHP process will take How do you know that? Basically, i just want to know what the absolute highest value i can set is. ini_get() isn't much help because it just returns whatever i put in.
The maximum value is probably PHP_INT_MAX (just guessing) but it's more likely that you eat your RAM before reaching it. memory_limit just limits the amount of memory dedicated to that script, but does not check that you have that amount of memory available.
Oh, okay, i get it! The problem i'm having is that a site keeps generating out of memory errors. Example: Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 30720 bytes)... That doesn't even make sense, does it? There's plenty of memory left. Am i missing something?
67108864 bytes is 64M, so you probably have this limit set in somewhere. Set it to 128M (that's the default since PHP 5.3 btw) or disable it setting it to -1: ini_set('memory_limit', -1). See the documentation about memory_limit
Yes, but i only tried to allocate 30720 bytes which is far from 64M. Should i not be able to allocate up to 64M?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.