Strange out of memory errors

Hello to all,

On the error log of my VPS i have the following errors of out of memory. The memory_limit is set at 128 MB.

Because it gives errors on different files, i don’t think is nothing to do with bad memory usage, even because the server is doing fine for the rest or the day, but sometimes it just gives out of memory errors. Could it be that the VPS has a resource usages peak?

Also, why it says allocated 262144 bytes when the memory limit is 128 MB?

Thanks!

[04-Jun-2013 02:18:46 Europe/Moscow] PHP Fatal error: Out of memory (allocated 262144) (tried to allocate 79 bytes) in Unknown on line 0 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 3407872) (tried to allocate 30720 bytes) in /home/melive/public_html/application/models/character.php on line 100 [04-Jun-2013 00:19:21 Europe/Rome] PHP Fatal error: Out of memory (allocated 4456448) (tried to allocate 101 bytes) in /home/melive/public_html/system/libraries/drivers/Cache/Memcache.php on line 47 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 5242880) (tried to allocate 30720 bytes) in /home/melive/public_html/application/i18n/en_US/global.php on line 63 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 4456448) (tried to allocate 105 bytes) in /home/melive/public_html/system/libraries/drivers/Cache/Memcache.php on line 47 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 4194304) (tried to allocate 30720 bytes) in /home/melive/public_html/application/models/character.php on line 3197 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 3670016) (tried to allocate 122880 bytes) in /home/melive/public_html/application/models/character.php on line 1012 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 4456448) (tried to allocate 99 bytes) in /home/melive/public_html/system/libraries/drivers/Cache/Memcache.php on line 47 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 4194304) (tried to allocate 122880 bytes) in /home/melive/public_html/application/models/character.php on line 1012 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 3670016) (tried to allocate 122880 bytes) in /home/melive/public_html/application/models/character.php on line 1012 [04-Jun-2013 00:19:19 Europe/Rome] PHP Fatal error: Out of memory (allocated 6029312) (tried to allocate 122880 bytes) in /home/melive/public_html/application/models/utility.php on line 898

Carefully check the logic of your code around the lines your log file states. Chances are you are allocating a lot of memory for a process of some sort.

It may be worth using a custom error handler and custom logging so you can get the full stack trace of errors, by the way.

Create a new php page, run this code:

[php]<?php echo phpinfo();?>[/php]

Find this section: memory_limit

Tell us what you see.

memory_limit	128M

what is strange to me is:

  1. Why it gives an error on a lot of files, even the framework code code, that should be optimized?
  2. Why in the message is written Out of memory (allocated 4456448, it should not write (allocated 128M)?

The error doesn’t come out very often. The webapp is a game web that runs 24x7. If it is a code related problem it should appear very frequently.

The 128M is the reserved memory related to a php thread? the thread is invoked by apache?

memory errors are not based on one spot. If you are familiar with how a daemon process works, you can skip to the next paragraph. If you don’t, a daemon process can allocate memory from RAM (or swap if RAM is filled up) to store temporary, volatile data. Every time you assign a variable, this is what happens below the hood. You can check this with the get_memory_usage function.
To limit abuse of this (i.e. a script causing the host to run out of volatile memory), PHP has a limit in place like this. This limit is the total maximum amount of memory that a single PHP process can hog up.

My guess is that previous circular references, object non-destruction and other shenanigans caused your PHP daemon to slowly crawl up towards the limit, at which point a variable 12k in size made it overflow.

A great tool to check this is xdebug, by the way. It will allow you to get a memory profile of anything that runs, and allows you to find memory leaks (this is what your problem’s name actually is).

Sponsor our Newsletter | Privacy Policy | Terms of Service