Support » Developing with WordPress » Differentiate in memory allocation per user

  • Resolved tezalsec

    (@tezalsec)


    Hi,

    I notice that when I log in as an admin user, my WP website needs at least 200 MB of memory, but when non-logged in anonymous users visit my website, 128 MB is enough for them to serve the pages. To save on simultaneous memory usage on my server, I find it a waste to just allocate 200+ MB to all users, so I thought of making a difference between logged-in users and non-logged in users.

    Is this in any way a bad idea? Am I overlooking things? At first sight it seems to work. Below is the code I use, I can not use WP core functions during wp-config.php (like is_admin()), so I do this instead in wp.config.php:

    /* experimental memory differentiation */
    
    if( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' )) {  // limit for admin
       define( 'WP_MEMORY_LIMIT', '256M' );
       define( 'WP_MAX_MEMORY_LIMIT', '256M' );
    
    } else { // limit for anon users
       define( 'WP_MEMORY_LIMIT', '128M' );
       define( 'WP_MAX_MEMORY_LIMIT', '128M' ); 
    }

    Love to hear your opinions on this.

    Cheers.

    • This topic was modified 2 weeks, 2 days ago by tezalsec.
    • This topic was modified 2 weeks, 2 days ago by tezalsec.
    • This topic was modified 2 weeks, 2 days ago by tezalsec.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Dion

    (@diondesigns)

    It’s a bad idea. 🙂

    As their names suggest, those constants set a limit for how much memory PHP can use for the request. So you’re better off setting one value and letting PHP do its (usually good) job of dynamic memory allocation.

    If you want to reduce memory usage, make sure you’re using Zend Opcache (a PHP extension), and also remove any PHP extensions you don’t need. Number one on that list is fileinfo, but keep in mind that Site Health will needlessly complain if it’s removed, even though WordPress will work perfectly fine without it.

    Another one to consider removing is the zip extension, though in that case, you should be careful because some poorly-written plugins/themes ignore the internal WP zip/unzip functions and try to use the extension’s functions directly. WP ships with PCLZip, which is used if the zip extension is not available, and it’s more memory-efficient than the zip extension. (The zip extension requires enough memory to load the entire .zip archive, PCLZip only requires enough memory to load the largest file in the .zip archive.)

    Thread Starter tezalsec

    (@tezalsec)

    Thanks @diondesigns for your input.

    Reading your sentence “So you’re better off setting one value and letting PHP do its (usually good) job of dynamic memory allocation.” , maybe I have a misconception in thinking it is a static allocation, and not a dynamic one.

    I assumed the whole memory limit of 256MB is statically reserved in advance per visitor request and not partly given back when not used, but you seem to be saying that that is not how it works, that it will not reserve and only take what is used?

    From the PHP documentation:

    This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server.

    So it is per script, and, no, the memory is NOT reserved. It’s merely a max safeguard.

    Also, from the WordPress documentation:

    Administration tasks may require more memory than usual operation. When in the administration area, the memory can be increased or decreased from the WP_MEMORY_LIMIT by defining WP_MAX_MEMORY_LIMIT.

    So it seems WordPress already enforces WP_MAX_MEMORY_LIMIT for the admin area ONLY, meaning there’s no need at all for your conditional function: you only have to define the two constants if you really wanted admin scripts to be able to use more memory.

    Thread Starter tezalsec

    (@tezalsec)

    Yes, I concluded that as well and removed the code.

    Glad to have learned about dynamic memory allocation.

    Thanks 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.