Finding Global Memory Hogs in WordPress

It is easy to accidentally pollute the global PHP scope with unneccessary vars, and in some cases these vars are classes or objects, and can be a huge waste of memory per php process.

PHP to Find Global Sizes

This php will loop through all global vars, then sort them by the strlen of their json_encoded value.

$glens = array();
foreach (
array_keys( $GLOBALS ) as $k ) {
$glens[ mb_strlen( json_encode( $GLOBALS[ $k ], JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_UNICODE ) ) ] = $k;
}
krsort( $glens );
var_export( $glens );

Sample Output

6788535 => 'GLOBALS',
5615030 => 'wp_object_cache',
2516754 => 'wpdb',
303154 => 'wp_filter',
51323 => 'wp_scripts',
36602 => 'wp_post_types',
14685 => 'wp_taxonomies',
14590 => 'submenu',
14576 => 'wp_registered_widgets',
13426 => 'wpseo_sitemaps',
13111 => 'wp_registered_widget_controls',
10468 => 'wp_rewrite',
10203 => 'AA_DEBUG',
9237 => 'wp_styles',
8861 => 'allowedposttags',
8568 => 'wp_roles',
7017 => 'wp_registered_widget_updates',
5727 => 'menu',
5383 => 'wp_widget_factory',
4355 => 'allowedentitynames',
4300 => 'wp_post_statuses',
3091 => 'userdata',
3003 => '_SERVER',
2769 => '_parent_pages',
2658 => '_wp_admin_css_colors',
2393 => '_registered_pages',
2283 => 'wp_registered_sidebars',
2106 => 'wp',
1861 => '_wp_post_type_features',
1477 => 'mb_news_links',
1468 => 'mb_the_brief',
1463 => 'mb_top_stories',
1461 => 'mb_project',
1383 => 'current_screen',
1363 => 'wp_file_descriptions',
1336 => 'wp_locale',
1322 => 'admin_page_hooks',
1137 => 'sidebars_widgets',
1131 => 'wp_query',
1071 => 'shortcode_tags',
1004 => 'BetterRelatedAdmin',
956 => 'merged_filters',
840 => 'wp_actions',
809 => 'relevanssi_variables',
775 => 'l10n',
764 => 'mwm_aalLoader',
754 => '_wp_deprecated_widgets_callbacks',
654 => 'new_whitelist_options',
591 => 'members',
543 => 'wp_embed',
509 => 'wp_admin_bar',
422 => 'dsq_api',
409 => '_wp_additional_image_sizes',
376 => 'acf',
344 => '_REQUEST',
341 => '_scb_data',
339 => 'allowedtags',
285 => 'month_abbrev',
284 => '_wp_real_parent_file',
261 => 'month',
226 => 'wp_meta_boxes',
224 => '_wp_registered_nav_menus',
175 => 'weekday_abbrev',
161 => 'weekday_initial',
133 => 'weekday',
119 => '_wp_theme_features',
112 => '_wp_submenu_nopriv',
96 => 'ISCLOG',
87 => 'count',
86 => 'wp_theme_directories',
78 => 'core_menu_positions',
42 => 'custom_post_type_order',
37 => 'akismet_api_host',
36 => 'timestart',
32 => 'wpseo_sitemaps_admin',
31 => 'user_email',
30 => 'args',
28 => 'PHP_SELF',
26 => 'akismet_nonce',
24 => 'members_load',
23 => 'wpseo_admin',
22 => 'hook_name',
20 => 'tinymce_version',
18 => 'hook_suffix',
16 => 'parent_file',
13 => 'title',
12 => 'time_format',
11 => 'is_configured',
10 => 'required_mysql_version',
8 => 'wp_db_version',
7 => 'taxnow',
6 => '_wp_menu_nopriv',
5 => '_wp_last_utility_menu',
4 => 'separator_found',
2 => 'show_admin_bar',

WordPress json_encode memory PHP wordpress

Comments