0

suppose PHP memory limit is set to 128M

now suppose you do

$v = "128 M worth of data";

will this cause a PHP out of memory error to occur?

pillarOfLight
  • 8,592
  • 15
  • 60
  • 90

2 Answers2

0

Yes it will fail with the following error, you can try it:

Fatal error: Allowed memory size of xxxxxx bytes exhausted (tried to allocate xxxxxxx bytes)

And that's the expected result, as the data for the string must be saved somewhere -> in memory ;)

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0

Although there are several limits, I suppose you mean the "memory_limit" value you can set with ini_set. Then the answers would actually be no.

If you set the value to the exact limit, you will be fine (if that is everything there is in your script). But the problem is when you use 1 byte more than that. So you can no longer do anything in your script. If you try to copy the value or do something with it, you will exceed the limit and it will break down. So its useless to have a string like that.

This question that was recently posted has some nice answers on it: memory-get-peak-usage-with-real-usage

Community
  • 1
  • 1
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • It should be noted that memory_limit is collective, and that one byte of memory can come from anywhere. Don't play around with data so close to the memory_limit unless you realize the risks and proceed with great caution. Even still, if the answer is "let's bring PHP to its knees by increasing the memory limit and risking my application closing" you're most-likely approaching the problem wrong. If you need to use-up so much memory at run-time, perhaps it's an indication you need to garbage collect, or find a more scalable solution. – Ultimater Nov 23 '15 at 20:36