WordPress variables and memory

When checking get_defined_vars() on an empty template file my theme loads over 70000 lines of output. Before even including elements on a page all of this is loaded.

  1. Is this correct?
  2. Is there any way to reduce it or remove unnecessary variables?
  3. Do all wp_rewrite rules have to be available for every page load?

Related posts

Leave a Reply

2 comments

  1. Your installation might be … less than optimal.

    Using my Mini Theme, no plugins and the following code on the front page right after the opening body tag …

    print count( $GLOBALS ) . ' $GLOBALS<br>';
    print @count( get_defined_vars(), 1 ) . ' variables<br>';
    print count( get_defined_constants( TRUE )['user'] ) . ' constants';
    

    … I get:

     158 $GLOBALS
    9759 variables
      85 constants
    

    Still too much for my taste. Keep in mind many plugin and theme developers forget to keep the global namespace clean. Depending on the quality of the addons you have installed your results might be much higher.

    And yes, you need those rewrite rules. Some slugs are translated, and translation can change on each request. So they have to be always available.

    1. Yes, a lot of information gets loaded. I just put print_r in my header.php on 3.5.1 and got nowhere near 70000 lines though. I got about 2110 and that includes some post body content. Also, print_r or var_dump are formatted representations, remember. Things are not actually stored in memory that way.
    2. I doubt you could remove much this stuff without breaking something or causing other negative effects. You are talking about Core hacks to do it. Some autoloaded items could probably be removed but if you did that you’d get extra database queries when those items are needed. I doubt it would be worth it.
    3. WordPress calculates rewrites on every page load. How is it to find the right page unless all of the rewrite rules are present? Of everything, these would be the most difficult to get rid of, unless you want to use the default permalinks.

    Are you actually experiencing a problem? Or is this preemptive?

    counts using toscho’s code, for the sake of comparison

    160 $GLOBALS
    79  variables
    29  constants