10

I'm getting this nasty error Fatal error: Out of memory (allocated 18087936) (tried to allocate 77824 bytes). Weird thing is, it's 17,25 mb (allocated) and it tried to allocate 76 kb. Memory limit is 128MB, and as you can see it's not even close to that. VPS got ~400mb of free ram at that moment. It only happens when I'm posting something and not all the time. I find it weird and don't really know what can cause it. Let me know if you need any additional info.

Igor Yavych
  • 4,166
  • 3
  • 21
  • 42
  • Does it says on which file it happens? – romainberger Apr 16 '13 at 14:45
  • Register the shutdown function (e.g. like [this](http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error)). Add some code to log the machine state (current memory consumption, available memory, etc.) Analyze the logs, most likely the available memory is really short at the moment of error. – J0HN Apr 16 '13 at 14:48
  • What WP modules are you using, and can you discern any pattern whatsoever regarding the files in which this is happening? Maybe they all belong to one module or are all related to one task? – Quinn Strahl Apr 16 '13 at 14:50
  • can you add ini_set('memory_limit', '128M'); inline in your code and check if that has an impact to the error message. phpinfo() and ini_get('memory_limit') sometimes report things that don't correspond to reality. you could also try with lower value, say ini_set('memory_limit', '30M');, and report your findings. – eis Apr 16 '13 at 14:50
  • _romainberger_, always different files. from either wordpress (like post.php) to files from different modules. _Quinn Strahl_, i didn't notice any pattern so far except it only happens when i add new posts (not always). _J0HN_, i will try – Igor Yavych Apr 16 '13 at 20:12
  • Try to put this line at the beginning of wp-config.php `ini_set("memory_limit",'-1');` – Toretto Apr 18 '13 at 14:05
  • Possible duplicate http://stackoverflow.com/questions/6887794/wordpress-fatal-error-out-of-memory – ChrisF Apr 18 '13 at 14:10
  • _J0HN_, http://pastebin.com/v3Vpepne (I modified shutdown function from example you suggested to be bit more readable) – Igor Yavych Apr 20 '13 at 09:06
  • @Flyer - I had this issue before and solved it, have posted an answer for you. Basically, you're running into a limit with WP, which handles its own memory, rather than with the php `memory_limit`. – Glitch Desire Apr 22 '13 at 10:10

9 Answers9

6

The error says that the memory limit is 18M and not 128M. This means that somewhere the memory_limit is set to something different than 128M (local php.ini, or the application itself, since PHP can override this setting at run-time).

I would suggest that you first create a file called (say) phpinfo.php with the following contents:

<?php
phpinfo();
?>

and place it where your script runs. Then access the file with your browser and look for the actual memory_limit value. If it's still showing 128M both for "global" and "local" value, then probably somewhere in your code, there's a "ini_set("memory_limit", $value);" call or something similar. Otherwise, if it says 18M, look for other places where this can be set:

  • Check your wp-config.php file
  • Check for any local php.ini file (look into the phpinfo.php page to see the location of the actual loaded php.ini file)
  • Check for any .htaccess files that contain such a directive
periklis
  • 10,102
  • 6
  • 60
  • 68
  • It shows 128mb memory limit. I went through my wp installation folder in search of memory limit change via ini_set. I found none that could really change it. – Igor Yavych Apr 14 '13 at 14:08
  • can you put a `echo ini_get('memory_limit');` inside the script that causes the error? (the closer to the error line, the better) – periklis Apr 14 '13 at 14:35
  • always different file and different line – Igor Yavych Apr 14 '13 at 15:05
  • still, if you put it in any file that at some point produced an error, what does it say? – periklis Apr 14 '13 at 18:04
  • is this a shared host or a dedicated server? Are you sure there are actually as many MB free memory at the time of the error?. Although I'm sure you've already checked, have a look at http://wordpress.org/support/topic/fatal-error-allowed-memory-size-6#post-1017842 – periklis Apr 14 '13 at 19:04
  • I got vps, and yes, I'm sure there as many free ram. And point of increasing memory limit, if it's way higher than one at which it fails? – Igor Yavych Apr 14 '13 at 21:45
  • User's issue isn't PHP's memory limit, it's WP's. – Glitch Desire Apr 24 '13 at 14:04
2

Hope this helps, as I had the same issue:

You're running into a limitation in WordPress's own memory limit, not your PHP memory_limit. Wordpress implemented this limit in order to prevent poorly written scripts from shutting down your whole PHP interpreter. Unfortunately, as you've noticed, it's rather barebones.

The easiest way I've found to fix this is to install the Change Memory Limit plugin from Wordpress plugin repository. It allows you to fix the issue without modifying any WP files manually.

The 64M default for the plugin will probably be fine for you.

Alternatively, if you don't want to trust a third party plugin, add the following line to wp-config.php:

define('WP_MEMORY_LIMIT', '64M');
Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
2

Got it from this http://wordpress.org/support/topic/fatal-error-out-of-memory-messages?replies=24#post-1929111, thought I'd share it with you guys :


THIS TO BE ENTERED IN THE WP-CONFIG FILE WHICH IS IN THE ROOT OF THE WORDPRESS SETUP: ENTER AFTER : 
define('ABSPATH', dirname(__FILE__).'/'); THE FOLLOWING:

define('WP_MEMORY_LIMIT', '64M');

ALSO CREATE A PHP.INI WHICH SHOULD BE UPLOADED TO THE PLUGIN FOLDER IN WP-CONTENT:

 `memory_limit = 128M; //Maximum amount of memory a script may consume (64MB)
`

max_execution_time = 45;


upload_max_filesize = 15M;


post_max_size = 30M;

Hope this helps!

Hiren Pandya
  • 989
  • 1
  • 7
  • 20
1

Just an option that helped me debugging these errors.

If you have access to the .htaccess, you could try this:

php_value upload_max_filesize 128M
php_value post_max_size 128M
php_value max_execution_time 300
php_value max_input_time 300

If that doesn't help, you could try this to check the settings:

echo 'Max upload size: ' . ini_get('upload_max_filesize') . '<br />';
echo 'Max post size: ' . ini_get('post_max_size') . '<br />';
echo 'Memory limit: ' . ini_get('memory_limit');

I hope this works, for me it did.

Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
1

I am not sure this causes the error, but it is worth checking for.

Many of the most untraceable pathetic out of memory errors are caused by :

1) User defined exception handlers, causing exceptions therefore recursing.

2) The above + trying to fetch class information of object originating the exception, and the class information testing causes an autoload attempt, in which autoload throws another exception and therefore recursing.

Have you done something conflicting with WP error handling or autoload?

Volkan
  • 2,212
  • 1
  • 14
  • 14
1

I install wp-memory-usage plugin by alexrabe on all my Wordpress sites.

This plugin helps you to see what Wordpress is seeing in terms of your PHP memory usage. Other approaches may show you the PHP settings at the server, however these settings are often overridden with various techniques the closer you get to a rendered page within the browser.

To set your max memory -> open your favorite text editor and create a file called php.ini; include in the file the following line:

memory_limit = 256M

Place a copy of the php.ini your Wordpress root directory. I also place copies in: /wp-admin; /wp-content; and /wp-includes.

TECH N
  • 11
  • 2
1

Add define('WP_MEMORY_LIMIT', '256M'); in your wp-config.php file. Its the easiest way.

aakash
  • 376
  • 1
  • 5
  • 18
1

It looks as through somewhere in the site, the memory limit is being overwritten, do a site wide search in the code for ini_set('memory_limit' and see what is returned.

duellsy
  • 8,497
  • 2
  • 36
  • 60
  • already did. i also registered shutdown function as suggested by J0HN. At the moment of out of memory error, it returns right memory limit – Igor Yavych Apr 21 '13 at 09:23
1

Given that your phpinfo() script returns the correct value for memory usage, this is obviously being set in Wordpress somewhere, and is therefore overriding the Apache provided values, so using a .htaccess to apply this still won't work.
This link (http://codex.wordpress.org/Editing_wp-config.php#Increasing_memory_allocated_to_PHP) suggests that Wordpress does its own memory management by default, so setting the ('WP_MEMORY_LIMIT', '256M') in the wp-config.php is logical.

If you have shell access to the server hosting your site I would suggest going to the DocumentRoot of your wordpress install and running the following commands:

grep -R "18M" *
grep -R -e '(WP_MAX_MEMORY_LIMIT|WP_MEMORY_LIMIT)' *

This will search through your files for the string 18M (the value your memory limit seems to be set to in human readable terms) and the configuration options for the Wordpress defined memory limit. I suspect that one of these commands will return positive output. You should then change the WP_MEMORY_LIMIT as suggested (although 256M may be a little high - try 64M initially, you don't want to cause yourself potential problems).

Chris
  • 46
  • 5
  • 1st didn't return anything. Second got typo i think – Igor Yavych Apr 22 '13 at 10:11
  • Sorry, not concentrating: grep -R -e '(WP_MAX_MEMORY_LIMIT|WP_MEMORY_LIMIT)' * Should remove the syntax issues ;) – Chris Apr 22 '13 at 10:24
  • didn't return anything either – Igor Yavych Apr 22 '13 at 10:36
  • That seems pretty unusual, given that in the Wordpress code it must check for the value being set, even if it is not. I would highly recommend that you update your wp-config.php to include the WP_MEMORY_LIMIT value. If it fixes your issue, I was right and you can leave it in. If it does not, remove it and there's no harm done. We know that it is not Apache doing this. – Chris Apr 22 '13 at 10:50