3

This is just a general question. Is there a way to scale up the the memory limit of a script as that script starts hitting the already defined limit?

For example, if the memory_limit is set to "32M", is there a way to check when the memory usage approaches that, and if so, set the limit higher?

Thanks for the help!

jldavis76
  • 812
  • 2
  • 15
  • 42
  • 3
    see this answer http://stackoverflow.com/a/3858189/ and I suppose you could by adding `ini_set('memory_limit','256M');` or something similar in an `else{...}` conditional statement. But I can't guarantee it will work as it may require a restart. You can always "try it" ;-) – Funk Forty Niner Mar 08 '16 at 19:04
  • I saw that post earlier, and I tried both putting it into the else, and trying to catch the fatal error exception, and doing it there. Unfortunately, neither worked, so I was hoping there was a simpler solution. If not, I could probably revisit that approach. – jldavis76 Mar 08 '16 at 19:06
  • So I got a few upvotes for that ^ - Watch this, somebody's going to pop in an answer *lol* which may bite back. – Funk Forty Niner Mar 08 '16 at 19:06
  • I guess it requires a restart then. Probably why it didn't work. It is a PC after all ;-) like when you pop in some new RAM. The OS doesn't know how much it has to work with, till you press the "ON" button again. You're not going to or be able to add RAM when it's still running, right? ;-) So I think the answer could be "no". – Funk Forty Niner Mar 08 '16 at 19:07
  • 1
    Side note to this: `memory_limit` doesn't mean your script `will` use that much memory, that's the most it can use. So set it to where you want it to fail, dynamically upping it isn't really going to help you out except choke your server because you don't have a limit emposed – cmorrissey Mar 08 '16 at 19:07
  • 1
    and we have an answer v_v_v – Funk Forty Niner Mar 08 '16 at 19:10
  • Best I can say is.... use the maximum memory you have in the ini_set. `ini_set('memory_limit','Set_to_the_Gills_M');` – Funk Forty Niner Mar 08 '16 at 19:11
  • I was trying to avoid setting a high limit,since it may not be needed most of the time, but I guess there isn't really a downside to doing so, is there? – jldavis76 Mar 08 '16 at 19:12
  • @jldavis76 No, there's no downside. Only downside you could get is, if and when you exceed the amount of memory used up by your script in contrast to physical (and available) RAM. Then it might page to disk. PHP won't buck if you have that set high in ini_set and you don't use it all up. – Funk Forty Niner Mar 08 '16 at 19:13
  • It doesn't make much sense to scale the upper limit dynamically. The server doesn't reserve that amount of memory for the script, it already assigns only the required amount. So if the script uses 1MB of memory, it doesn't make any difference if the upper limit is 1MB or 100MB. – JJJ Mar 08 '16 at 19:15
  • @jldavis76 so, what do we do with your question? Has this been answered in comments? That answer below received an upvote, so... hard to say what you want to do with the question. – Funk Forty Niner Mar 08 '16 at 19:20
  • If someone wants to rephrase the comment solutions into a question, I would select that as the answer. The answer below only indicates how to find out how much memory was used, and does not fully address the question. – jldavis76 Mar 08 '16 at 19:27

2 Answers2

1

For those reaching this while searching:

From the comments, the answer appears to be that there is not really a need to do this. Just setting the memory limit high enough will allow the script to run, and there is no downside to overestimating the memory limit needed.

Please view the comments below the question.

jldavis76
  • 812
  • 2
  • 15
  • 42
0

You can also play with this to get an idea how much memory you are using.

 $start_memory = memory_get_usage();
 //do some stuff here.
 echo memory_get_usage() - $start_memory;