From 888ecaea2be6c1f9a967f2efae5996cfca5e0a31 Mon Sep 17 00:00:00 2001 From: Paul Kevan <2290623+pkevan@users.noreply.github.com> Date: Wed, 11 Oct 2023 10:56:17 +0100 Subject: [PATCH] Fix memory usage on t-shirt cache prime (#1085) * Fix memory usage on t-shirt cache prime Warning are being thrown during the current execution of the t-shirt cron: ``` E_USER_WARNING Peak memory usage at 92.2%. Current action/filter: switch_blog. Stack Trace do_action_ref_array('camptix_prime_tshirt_report') ``` In testing this reduced the memory issues. * Update field-tshirt.php Fix linting. --- .../plugins/camptix/addons/field-tshirt.php | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/public_html/wp-content/plugins/camptix/addons/field-tshirt.php b/public_html/wp-content/plugins/camptix/addons/field-tshirt.php index 723a04ac67..1bb6ced45a 100644 --- a/public_html/wp-content/plugins/camptix/addons/field-tshirt.php +++ b/public_html/wp-content/plugins/camptix/addons/field-tshirt.php @@ -74,9 +74,10 @@ public function prime_report_cache() { 'order' => 'DESC', ) ); + $i = 0; foreach ( $sites as $site_id ) { switch_to_blog( $site_id ); - + $i++; $sizes = $this->get_aggregated_sizes(); if ( ! empty( $sizes ) ) { @@ -86,8 +87,11 @@ public function prime_report_cache() { } restore_current_blog(); - // Reset DB query log each time, to reduce memory usage. - self::reset_db_query_log(); + // Reset DB query log & object cache every 10 loops, to reduce memory usage. + if ( 0 == $i % 10 ) { + self::reset_db_query_log(); + self::reset_local_object_cache(); + } } update_site_option( 'tix_aggregated_tshirt_sizes', $sizes_by_site ); @@ -103,6 +107,26 @@ public function reset_db_query_log() { $wpdb->queries = array(); } + /** + * Reset local WordPress object cache. + */ + public function reset_local_object_cache() { + global $wp_object_cache; + + if ( ! is_object( $wp_object_cache ) ) { + return; + } + + $wp_object_cache->group_ops = array(); + $wp_object_cache->memcache_debug = array(); + $wp_object_cache->cache = array(); + + if ( method_exists( $wp_object_cache, '__remoteset' ) ) { + $wp_object_cache->__remoteset(); + } + } + + /** * Get the counts for each shirt size that attendees selected *