wp admin - Memory growth with new additions to WordPress Multisite

admin2025-01-07  3

I run a subfolder based WordPress Multisite with ~200 websites, recently through our host I have tried to decrease our memory to 128mb per thread at 12 threads, instead of 256mb at 6 threads, but when I did that I was not able to login to the admin panel at all.

On investigation I found that the admin panel is using 215mb / 256mb available, so of course I wasn't able to login on 128mb. The site itself (non admin pages) only uses around 20-30mb for each page load. Disabling all plugins takes memory utilization from 215mb to ~180mb.

On even further investigation, I found that this is because there are hundreds (386) calls to "switch_to_blog()" and "restore_current_blog()" in order to populate the "My Sites" dropdown on every admin screen. If I remove the admin bar / disable these core admin bar files WP Admin memory utilization drops to around 36mb! If I remove a single site from our multisite memory utilization drops by ~3mb, so it stands that adding sites increases the overhead.

Is there a reason for this overhead for the WP Admin bar? Maybe I'm being stupid? Is there a way I can disable the admin bar for admin without messing with core files, or any other solution to this. Unfortunately my host (Kinsta) is not able to give admin higher memory than normal users.

Thanks!

UPDATE: Doing more tests, I made a user that has no websites assigned to it (so the My Sites list will have 0) and here is the memory usage (it's a bit more on the admin page vs on website pages, but still a lot less):

And here is a user with something like 180 websites assigned:

I run a subfolder based WordPress Multisite with ~200 websites, recently through our host I have tried to decrease our memory to 128mb per thread at 12 threads, instead of 256mb at 6 threads, but when I did that I was not able to login to the admin panel at all.

On investigation I found that the admin panel is using 215mb / 256mb available, so of course I wasn't able to login on 128mb. The site itself (non admin pages) only uses around 20-30mb for each page load. Disabling all plugins takes memory utilization from 215mb to ~180mb.

On even further investigation, I found that this is because there are hundreds (386) calls to "switch_to_blog()" and "restore_current_blog()" in order to populate the "My Sites" dropdown on every admin screen. If I remove the admin bar / disable these core admin bar files WP Admin memory utilization drops to around 36mb! If I remove a single site from our multisite memory utilization drops by ~3mb, so it stands that adding sites increases the overhead.

Is there a reason for this overhead for the WP Admin bar? Maybe I'm being stupid? Is there a way I can disable the admin bar for admin without messing with core files, or any other solution to this. Unfortunately my host (Kinsta) is not able to give admin higher memory than normal users.

Thanks!

UPDATE: Doing more tests, I made a user that has no websites assigned to it (so the My Sites list will have 0) and here is the memory usage (it's a bit more on the admin page vs on website pages, but still a lot less):

And here is a user with something like 180 websites assigned:

Share Improve this question edited Dec 30, 2024 at 19:44 socooladmin asked Dec 30, 2024 at 14:13 socooladminsocooladmin 212 bronze badges 3
  • 1 Huh. If switch_to_blog() is consuming memory like that, then that's a bug worth reporting. It's probably building up an in-memory cache of all blogs, so if you had an external object cache or something that should save your memory at least, but that's still slighltly stupid if you end up making say 200+ memcached requests instead. – Rup Commented Dec 30, 2024 at 15:56
  • 1 disabling the entire admin toolbar seems a bit extreme, it's easier to drop just the my sites menu, though it's unusual that it would be having that much of a load, not unless additional plugins and code are hooking in to do extra work, e.g. a multilingual type plugin could impact that, but without looking at the code of your site it's not possible to tell remotely. Also I'm not seeing direct evidence that my sites specifically is the cause of the memory issue, just tha it's called a lot which isn't surprising if there's a lot of names to fetch, there's more in the toolbar that could do this – Tom J Nowell Commented Dec 30, 2024 at 18:01
  • If I drop the My Sites in the admin bar I still get the same performance because of this repeated call, our sites do not run any multilingual plugins, and like I said this problem persists even with all plugins disabled. From what I can tell it seems entirely dependent on any action that lists many sites at once using switch_to_blog(). By removing myself as a user from all sites this issue is entirely fixed because there are no calls to switch_to_blog() for the admin bar. This is not ideal though. – socooladmin Commented Dec 30, 2024 at 19:33
Add a comment  | 

1 Answer 1

Reset to default -1

Yes, you can drop the "My sites" menu from a plugin.

function admin_bar_menus_remove_my_sites() {
    remove_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
}
add_action( 'add_admin_bar_menus', 'admin_bar_menus_remove_my_sites' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254651a223.html

最新回复(0)