Home > OS >  WP_Query, after wp update - Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to
WP_Query, after wp update - Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to

Time:01-26

After updating wordpress from 5 to wp 6.1.1 , wp_query (or query_posts) eat up absurd amount of memory, on very simple query which list 1000 custom posts. I have 16000 posts of such type in database. and a lot of custom ACF fields, but there has been no issues in previous wordpress version.

I have tried it all,

wp_suspend_cache_addition( true ); - no effect

'fields' => 'ids', - no effect

'cache_results' => false, - no effect

Always end up with - Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate * bytes) in /class-wpdb.php on line 2187

I leaved only query which has

$quotesToGetProcessedArgs = [
    'post_type' => 'QuoteRequest',
    'post_status' => 'publish',
    'offset' => '0',
    'posts_per_page' => 1000,
    'fields' => 'ids',
    'cache_results' => false,
];
$quotesToGetProcessed = new WP_Query($quotesToGetProcessedArgs);
while ($quotesToGetProcessed->have_posts()) : $quotesToGetProcessed->the_post();
//do nothing
endwhile;
wp_reset_postdata();

As You can see, even if I do nothing and ask just for ids , query eats up absurd amount of memory (about 1.5GB!!!!) , I was able to temporaly solve it by adding ini_set('memory_limit', '1500M'); , but that is no real solution, on develop where I have older wordpress everything works so I suspect some wp6 changes on background do some heavy additional stuff.

CodePudding user response:

6.1.1 puts more information into its cache about each post it reads than 5.x did, to improve performance. It looks like your 'posts_per_page' => 1000 query puts 1000 posts' worth of meta- and term- data into the cache, thereby blowing out your web host machine's RAM.

Keep in mind that the point of using a database is to allow your application to handle data that's much larger than your RAM. But you have to handle it batch-by-batch, and the batches have to fit in RAM.

How to fix this? The easiest choice is to use a smaller number of posts.

Another might be to add these lines to your query. Your query suppresses updating the posts cache. You can also suppress updating the metadata and term caches, using these query-arg lines to control caching.

'cache_results'          => false,  /* you have this already */
'update_post_meta_cache' => false,
'update_post_term_cache' => false,

EDIT If this is your code, your best bet is reduce the posts_per_page batch size to something < 100. WordPress core developers are adding more caching to future versions, which will make large batch sizes even less viable as a programming technique.

Or you can use a filter to suppress updating the meta cache. But do this carefully; you may slow things down if you do it everywhere.

/**
 * Short-circuits updating the metadata cache.
 *
 * @since 5.0.0
 *
 * @param mixed $check      Whether to allow updating the meta cache of the given type.
 * @param int[] $object_ids Array of object IDs to update the meta cache for.
 */
function jiro_skip_update_metadata_cache( $check, $posts ) {
  return array();
}
add_filter ( 'update_post_metadata_cache', 'jiro_skip_update_metadata_cache' 10, 2 );

(I used your name in the function name because these functions pollute the global namespace in WordPress.)

  • Related