MemoryManager.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Memory Manager class. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\Internal\ProductFeed |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\Internal\ProductFeed\Utils; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Helper class for managing memory. |
| 18 | * |
| 19 | * @since 10.5.0 |
| 20 | */ |
| 21 | class MemoryManager { |
| 22 | /** |
| 23 | * Get available memory as a percentage of the total memory limit. |
| 24 | * |
| 25 | * @since 10.5.0 |
| 26 | * |
| 27 | * @return int Available memory as a percentage of the total memory limit. |
| 28 | */ |
| 29 | public function get_available_memory(): int { |
| 30 | $memory_limit = wp_convert_hr_to_bytes( ini_get( 'memory_limit' ) ); |
| 31 | if ( 0 >= $memory_limit ) { |
| 32 | // Some systems have "unlimited" memory. |
| 33 | // We should treat that as if there is none left. |
| 34 | return 0; |
| 35 | } |
| 36 | return (int) round( 100 - ( memory_get_usage( true ) / $memory_limit ) * 100 ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Flush all caches. |
| 41 | * |
| 42 | * @since 10.5.0 |
| 43 | */ |
| 44 | public function flush_caches(): void { |
| 45 | global $wpdb, $wp_object_cache; |
| 46 | |
| 47 | $wpdb->queries = array(); |
| 48 | |
| 49 | wp_cache_flush(); |
| 50 | |
| 51 | if ( ! is_object( $wp_object_cache ) ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // These properties exist on various object cache implementations. |
| 56 | $wp_object_cache->group_ops = array(); // @phpstan-ignore property.notFound |
| 57 | $wp_object_cache->stats = array(); // @phpstan-ignore property.notFound |
| 58 | $wp_object_cache->memcache_debug = array(); // @phpstan-ignore property.notFound |
| 59 | $wp_object_cache->cache = array(); // @phpstan-ignore property.notFound |
| 60 | |
| 61 | // This method is specific to certain memcached implementations. |
| 62 | if ( method_exists( $wp_object_cache, '__remoteset' ) ) { |
| 63 | $wp_object_cache->__remoteset(); // important. |
| 64 | } |
| 65 | |
| 66 | $this->collect_garbage(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Collect garbage. |
| 71 | */ |
| 72 | private function collect_garbage(): void { |
| 73 | static $gc_threshold = 5000; |
| 74 | static $gc_too_low_in_a_row = 0; |
| 75 | static $gc_too_high_in_a_row = 0; |
| 76 | |
| 77 | $gc_threshold_step = 2_500; |
| 78 | $gc_status = gc_status(); |
| 79 | |
| 80 | if ( $gc_threshold > $gc_status['threshold'] ) { |
| 81 | // If PHP managed to collect memory in the meantime and established threshold lower than ours, just use theirs. |
| 82 | $gc_threshold = $gc_status['threshold']; |
| 83 | } |
| 84 | |
| 85 | if ( $gc_status['roots'] > $gc_threshold ) { |
| 86 | $collected = gc_collect_cycles(); |
| 87 | if ( $collected < 100 ) { |
| 88 | if ( $gc_too_low_in_a_row > 0 ) { |
| 89 | $gc_too_low_in_a_row = 0; |
| 90 | // Raise GC threshold if we collected too little twice in a row. |
| 91 | $gc_threshold += $gc_threshold_step; |
| 92 | $gc_threshold = min( $gc_threshold, 1_000_000_000, $gc_status['threshold'] ); |
| 93 | } else { |
| 94 | ++$gc_too_low_in_a_row; |
| 95 | } |
| 96 | $gc_too_high_in_a_row = 0; |
| 97 | } else { |
| 98 | if ( $gc_too_high_in_a_row > 0 ) { |
| 99 | $gc_too_high_in_a_row = 0; |
| 100 | // Lower GC threshold if we collected more than enough twice in a row. |
| 101 | $gc_threshold -= $gc_threshold_step; |
| 102 | $gc_threshold = max( $gc_threshold, 5_000 ); |
| 103 | } else { |
| 104 | ++$gc_too_high_in_a_row; |
| 105 | } |
| 106 | $gc_too_low_in_a_row = 0; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |