0

In one of application that I maintenance (PHP based) I got the E_ERROR which is connected to the memory - "Allowed memory size of 134217728 bytes exhausted...".

I am familiar with temporary solutions like setting up higher amount of memory for the script etc. but it is just temporary solution and here I need to solve it from the roots.

I have checked amount of memory which is used after each part of code in my script by simply writing it to the log file like:

addMsg('1 - Memory Usage: ' . (memory_get_usage()/1048576) . ' MB \n');

By this method I have found out that only one simple line of code increase drastically memory usage and it is: session_start() function, which then causes this error.

Interesting thing is that this error does not occur every time scripts loads. It occurs from time to time (based on high traffic on that app, it occurs every few minutes). Also, I have found out about this error from New Relic which I use for monitoring and from logs. Although, I have never experienced myself.

Does anybody have any theoretical idea how this simple function can cause such memory usage that it cause E_ERROR?

Also, all of my errors look something like this:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 10556354 bytes) in Unknown on line 0
Allowed memory size of 134217728 bytes exhausted (tried to allocate 10399243 bytes) in Unknown on line 0
Allowed memory size of 134217728 bytes exhausted (tried to allocate 8112988 bytes) in Unknown on line 0

From my point of view, it seems that script wanted to allocate less space than it was allowed and even that, error occurred. Am I right or something else is going on?

user3100193
  • 531
  • 4
  • 6
  • 16
  • The first thing I would try is to increase max memory - probably to 256M – user2182349 Apr 02 '17 at 01:00
  • I have done that to make app running, but it wont solve the problem. It is just temporary solution. I am terrified if it goes up again, so increasing memory is not long term solution – user3100193 Apr 02 '17 at 01:03
  • session_start() probably takes a lot of memory because there's a lot of session data (or a lot of data is being created as a result of some class instantiating automatically as the session is unserialized). Check what you have stored in the session. – obe Apr 02 '17 at 01:05
  • " in Unknown on line 0 " seems wierd to me. I would check above php. Is PHP making a good Garabge collection? http://php.net/manual/en/sessionhandler.gc.php Maybe your memory is already high and you need to check for memory leaks. Are you on any framwork? http://stackoverflow.com/questions/15521206/session-start-exhausts-memory, http://stackoverflow.com/questions/561066/fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-codeigniter-xml, http://stackoverflow.com/questions/654310/cleanup-php-session-files, – Louis Loudog Trottier Apr 02 '17 at 01:06
  • Might want to take a look at garabge collection http://php.net/manual/en/features.gc.php but i definitively think you need to review your code for some leaks. Try to unset your variables in the global scope when you are done with them. Unset is a tiny function Overhead to trade of some Memory. get_defined_vars() will give you all the variabe still stored in memory. – Louis Loudog Trottier Apr 02 '17 at 01:14
  • Thank you for your answers! I am not using any framework. It is custom system build on PHP. I am going to take a close look at everything you guys advised to me and then I will get back to you in case I have additional questions or with my solution. – user3100193 Apr 02 '17 at 01:19
  • Is this an open source application like WordPress or Drupal? – user2182349 Apr 02 '17 at 01:19
  • @LouisLoudogTrottier Just one question about reviewing my code. Is there any possibility that there is an error in code if one line before session_start(), everything seems fine (maybe 2MB allocated) and one line after session_start() scripts stops with fatal error – user3100193 Apr 02 '17 at 01:21
  • @user2182349 no, it is custom site on PHP, nothing like Drupal or WP etc. – user3100193 Apr 02 '17 at 01:23
  • In that case i would check how much info you are storing in your session. I know serialization can be a heavy boat but not to stack 126mb. – Louis Loudog Trottier Apr 02 '17 at 01:48
  • Have you check how long it takes to start_session? a little microtime() before and after maybe? what does session_status() tells you before session_start(); are you passing any configuration array? When you talk about that 'APP', is it an http based request? Does the same happen when you call it from a browser? and last but not least, what happen if someone flush their cache/cookie? does it reduce the memory load on the server? I feel i might confuse more with all those 'could-be this or that' but those are what comes to mind. – Louis Loudog Trottier Apr 02 '17 at 04:01
  • ok, last spam : http://stackoverflow.com/questions/32027037/does-a-session-variable-stay-in-memory-in-php, answer refering to http://php.net/manual/en/book.memcached.php – Louis Loudog Trottier Apr 02 '17 at 04:09
  • I haven't tried with sesson_status() I will give it a try. Maybe one thing, that I have forgotten to mention is that E_ERROR only occurs when user access one specific url. File which includes session_start() is included in every page of site, but memory error occurs only at one specific url/page. Do you maybe have some ideas about this? Thanks in advance. – user3100193 Apr 02 '17 at 16:53

0 Answers0