Memory errors with media upload, WordPress can’t use more than 96M (while there’s 512 available!)

I’m aware that this is a known problem but I was unable to address it using information found in WPAnswers and WordPress support forums.

In my Worpdress single site installation the media uploader is always throwing an error at me

Read More
    out of memory (allocated 99614720) (tried to allocate 3760 bytes) 

the amount of allocated memory is more or less always the same – that should be about 96M… but in my server the php memory limit was set to 512M, post size is 12M, max upload size 8M

it’s a VPN server and there’s 1.5GB memory on it!

WordPress throws the error also on images as small as 300-400K and under.

I tried using

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

or

   define('WP_MAX_MEMORY_LIMIT', '128M');

but neither of these (alone or together) worked, in fact the error thrown was always the same and I had the impression WordPress was disregarding both – I tried other settings too, the error was always the same and the amount of memory allocated more or less the same

I tried to change the php settings, like lowering the memory limit to 128M or bringing back again to 256M, restarting http service (apache) and restarting the whole server, but nothing changes

I proceeded to disable plugins, but the only way they don’t seem ultimately responsible for the problem.

I decided to edit line 257 of media.php inside wp-includes folder, since the errors were always coming from line 258 which was executed thereafter

   @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );

I’ve replaced WP_MAX_MEMORY_LIMIT with values above 96MB for instance 128M or 256M

but no matter what, the allocated memory never exceeded 96MB. I tried lowering the WP_MAX_MEMORY_LIMIT to values such as 16M, 32M… anything below 96. Well! In that case the allocated memory resulted lower as in fact I was setting a lower limit – but how come it can’t be more than 96? Still, I don’t understand why wordpress takes so much, but nevertheless, why I’m not able to set to 128 and see if the problems are finally fixed? I’m only experiencing issues with the image uploader.

I’ve even tried to use

    @ini_set( 'memory_limit', '128M' );

in my wp-config but again it doesn’t change the 96M limit… however it’s able to lower it if I set to anything below 96 (resulting memory errors will state corresponding values)

I’ve run phpinfo from where my WordPress installation is and the available memory is indeed that which is set on the server… 512M! so I don’t understand why WordPress seem to use less than that no matter what

any clues?

thank you

Related posts

Leave a Reply

1 comment

  1. You’re looking at the problem the wrong way. The error you’re seeing isn’t an error coming from WordPress, it’s a PHP error.

    Somehow, somewhere, something is limiting the memory limit to 96M, and it ain’t WordPress that’s doing it.

    Here’s the thing: WordPress can’t actually limit the memory on most servers. I know that it attempts to increase the limit using ini_set and such, but that’s more of a just-in-case sort of thing. Sometimes it can set the limit higher. Most of the time, PHP simply ignores that line.

    You’re also misunderstanding how memory works. This is made clear by the line “WordPress throws the error also on images as small as 300-400K and under”. The size of the compressed image has little or nothing to do with the size the uncompressed image takes in memory. That is all about the resolution. Megapixels matter. One pixel takes 4 bytes. 8 million pixels (8 megapixels) takes 32 megabytes of memory to represent, and the size of the resulting compressed JPG is irrelevant. WordPress needs to hold a couple copies of the image in memory too, the original one that it’s working from, and another that it is creating the modified versions of. So yeah, lots and lots of memory is needed.

    But again, WordPress can’t set the memory limit in most cases. So something is limiting it to 96M. Which will severely limit the maximum size of the images WP can work with. Until you find where that 96M limitation is coming from, looking at the WordPress core code and such is kinda a waste of your time here.