11

I'm building a site for a photographer who uploads pictures that are normally large for todays digital cameras. Images are quite memory intensive, especially the image operations like creating thumbnails.

So I wanted to raise the memory limit in the admin above 256MB - how can this be done? I allowed CGI and PHP to use up to 1 Gigabyte but wordpress always decreases the memory to 256MB. Any idea how to fix that on a client side that needs to be able to auto-update?

UPDATE: From Wordpress 3.2 ongoing the maximum memory limit in Wordpress will be configure-able again.

2
  • hey, haven't seen you in a while. Guess you've been busy with clients; good for you! Have you considered maybe offloading the image processing, maybe using Amazon S3 and EC2? aws.amazon.com/articles/1602 Or Google App Engine? code.google.com/appengine/docs/python/images/usingimages.html Or using urlimg.com? Commented Oct 26, 2010 at 10:02
  • @MikeSchnikel - Hej! I'm looking to solve this with wordpress. If I need to setup a thumbnail server I wouldn't use wordpress anyway so. But thanks for the links, very interesting at least :).
    – hakre
    Commented Oct 26, 2010 at 10:20

6 Answers 6

13

Theoretically, editing your config.php and add this line before wp-settings.php inclusion.

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

should raise your memory limit for WordPress to 256MB or whatever value you set. And this will work sitewide. However, as sorich87 pointed out, there are few functions that will alter this setting with hard coded 256 MB limit.

To Hack or Not To Hack

A little concern about this, WP_MEMORY_LIMIT is one of the most strange WP setting I've encountered. if you check /wp-includes/default-constants.php you'll find this setting:

// set memory limits
if ( !defined('WP_MEMORY_LIMIT') ) {
    if( is_multisite() ) {
        define('WP_MEMORY_LIMIT', '64M');
    } else {
        define('WP_MEMORY_LIMIT', '32M');
    }
}

I never realize that WP will set it's default memory usage so low, until I find this in WP codex:

WordPress will automatically check if PHP has been allocated less memory than the entered value before utilizing this function. For example, if PHP has been allocated 64MB, there is no need to set this value to 64M as WordPress will automatically use all 64MB if need be.(source)

That explanation was relieving. However, the usage of hard coded @ini_set('memory_limit', '256M'); everytime WP execute function that need more memory is never mentioned. In fact, I find no explanation about this vague behavior from WP codex. Since most of non user-related functions are either not documented or not clearly explained in codex.

While this setting work nicely on most case, it will make those functions useless on server with lower max memory setting or on your case, higher memory usage.

Until WP guys fix this, I think your only solution is to modify the core. You may find this post written by hakre interesting to read. He also submit a patch recommendation in Trac. Previous link to patch file may help you to find list of function that use this setting.

edit:

this is the most stupid answer I've ever give because I give a link to your own post (just realize that OP name was hakre after 2 days) :D

edit 2:

as mentioned on comment, this has been fixed by 3.2 release

2
  • your answer is far away from being stupid. To be precise it was kinda stupid of me posting this question while I already knew the answer, and your comment totally shows that you factually ran over that. Really, my bigest apologies, posting the link here was so right as far as I knew myself while posting the question, so what to say? Thanks for being that open to admit that! I like your style and linked my blog to your answer (not only linked back to the blog).
    – hakre
    Commented Feb 13, 2011 at 1:32
  • In 3.2 this should be configurable: Changeset 17749
    – hakre
    Commented May 4, 2011 at 23:29
1

Swill - I had just same problems. Damn wordpress with some of the "features" A rather more sophisticated approach is to change all the @ini_set 's to set it from WP_MEMORY_LIMIT - problem solved

Also, changing php values at runtime ain't that smart, not sure why it is implemented :(

0

By your description I think this code in admin.php is the issue:

if ( current_user_can( 'manage_options' ) )
    @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', '256M' ) );

Somewhat strange that it treats admin memory usage separately, but at least it is filterable.

1
  • This does not work for the image operations in the admin. They get reduced to 256MB again regardless if I use that filter or not. :/
    – hakre
    Commented Oct 26, 2010 at 10:17
0

There are @ini_set('memory_limit', '256M'); before images are loaded in memory by GD functions.

imagecreatefromstring() (line 253, wp-includes/media.php) is used by wp_load_image() which is used by image_resize (the function were the images processing is done for thumbnails creation). The memory limit is at line 252, so, as far as I can see, there is no way to do modify it without killing a kitten* >:).

There are also GD functions in load_image_to_edit() (line 200, wp-admin/includes/image-edit.php). This one is used by the image editor (I neved used it, since I know WordPress, didn't even know were to find it :)). If your client use it, you may want to set the memory limit there also. There is the filter load_image_to_edit_path.

Hope my answer is helpful.

*hacking core

1
  • Yup, these are hardcoded into a few admin-side functions relating to images. There is a WP_MEMORY_LIMIT constant and even an admin_memory_limit filter, but neither are consulted in these handful of cases.
    – user66
    Commented Oct 26, 2010 at 12:56
0

I just wanted to say thanks to bangbambang. This is the first post I found that was able to give me some insight into why the entry into wp-config.php of define('WP_MEMORY_LIMIT', '512M'); was not actually being used. I spent hours trying to figure this out...

My import was failing with the 256M exhausted issue regardless of how I set the memory limit.

Here are the places where the memory limit is hardcoded to 256M (in version 3.0.5 anyway).

wp-admin/includes/file.php (line 532)

wp-admin/includes/image-edit.php (line 393 & 498)

wp-includes/media.php (line 252)
1
  • Ticket #13847 has a patch attached that makes those places configure-able with an additional constant called WP_MAX_MEMORY_LIMIT.
    – hakre
    Commented Feb 9, 2011 at 22:29
-4
/* Memory Limit */
define( 'WP_MEMORY_LIMIT', '1G' );
define( 'WP_MAX_MEMORY_LIMIT', '1G');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.