0

I really need some help on this one. My PHP program (it's a bit large, so I can't show all of it here) errors out with

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 16777216 bytes) in /path/program.php on line 81

This sounds like a valid error at face value, but I'm trying to do

$error = new message();

on line 81, and message is defined as

class message {
  public $pos = -1;
  public $severity = 0;
  public $msg = "";
}

Why would this result in allocating 16777216 bytes? What am I doing wrong? How can I keep the memory use down to a sane amount?

I did try searching, but the only articles I found about huge memory allocations were from people actually trying to allocate such large chunks! So that wasn't much help.

Edit: found out I had a bug in my program that caused an infinite number of messages to be created. The question still hasn't been answered though, why does it say it tries to allocate 16777216 bytes, while one message takes up only 184 bytes? Even the many answers to the linked question don't address this.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • You already have the max-memory set at 64M, you might need to increase it to 128M. You can also do some things to reduce the amount you are using. Doing both would be best. – Alister Bulman Apr 18 '14 at 11:47
  • "some things" you say? – Mr Lister Apr 18 '14 at 11:48
  • @AlisterBulman Did you even bother to understand the question here? – arkascha Apr 18 '14 at 11:51
  • 3
    Is that _really_ the correct and full class definition? – arkascha Apr 18 '14 at 11:52
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Adam Apr 18 '14 at 12:03
  • @arkascha Yes, that is a copy and paste from the actual PHP source. – Mr Lister Apr 18 '14 at 13:40
  • Then I suggest you use a debugger to step through the code to see what is going on, if it _really_ is that command causing the problem or you have some unexpected code getting executed. – arkascha Apr 18 '14 at 13:42
  • @arkascha OK, I did, and one allocation is only 184 bytes. I also found the bug, that caused an infinite amount of allocations, which explains the error itself, but not the text of the error. – Mr Lister Apr 18 '14 at 15:39
  • Sorry, I can't follow you there. What bug? What infinite amount of allocations and what explanation? – arkascha Apr 18 '14 at 15:44
  • @arkascha I meant I found out I had a bug in my program, an infinite loop that caused an infinite number of objects to be created, so naturally the system would run out of memory eventually. So that explains why the error occurred; but it does not explain why the error would say "allocate 16777216 bytes". – Mr Lister Apr 18 '14 at 17:41
  • Ah, ok. Might be a shifting problem then: sometimes it makes sense not to allocate a small amount of memory for "the next object", but instead to allocate a huge amount of memory and shift all existing objects there. This can be much more efficient in certain cases. So maybe php tries to shift some big array to another location in memory (copying) to be able to increase that array with less costs. – arkascha Apr 18 '14 at 17:52
  • Does this answer your question? [Meaning of "Fatal error: Allowed memory size of x bytes exhausted (tried to allocate y bytes)"?](https://stackoverflow.com/questions/5292873/meaning-of-fatal-error-allowed-memory-size-of-x-bytes-exhausted-tried-to-allo) – jabbascript Jan 05 '22 at 17:28
  • @jabbascript No. – Mr Lister Jan 09 '22 at 17:58

2 Answers2

1

Why would this result in allocating 16777216 bytes?

If you're asking why a small bit of code would result in such a large allocation, you may be confused (as I have been before) about the contents of the allocation. It may sound like the amount of bytes shown as allocated is the amount the memory was exceeded by, but it's actually the amount of the last attempted memory chunk (which would include your message class, but also other stuff).

i.e. The message class itself was probably a small fraction of the attempted memory allocation.

Here is the only source I've seen that mentions how memory in php is chunked: zend_alloc allocates memory from OS by CHUNKs

Here are a couple similar questions with various answers that might help explain the error in general:

jabbascript
  • 345
  • 1
  • 6
  • 13
0

As you can imagine, this error message occurs when PHP tries to use more memory than is avialable. I'm assuming that changing code is not an option but you CAN increase the amount of memory available to PHP.

To change the memory limit for one specific script, include a line such as this at the top of the script:

ini_set("memory_limit","124M");

or in php.ini you can Use :

memory_limit = 124M

Hope this helps

jkumar
  • 32
  • 8
  • This is not an answer to the question. This might provide a workaround at most, but q very questionable one. The question of the OP was: _why_ is such a huge amount of memory allocated. So Id say this might make sense as a comment to the question, but it is _not_ an answer. – arkascha Apr 18 '14 at 15:49