[wp-hackers] WP_Query leaking absurd amounts of memory

William P. Davis will.davis at gmail.com
Fri Jun 1 03:39:06 UTC 2012


What plugins and dropins are you using? Also, I would avoid calling wp_cache_flush - that's just going to spawn more queries which won't help much. 

Will
Sent from my BlackBerry

-----Original Message-----
From: Rich Christiansen <warproof at warproof.com>
Sender: wp-hackers-bounces at lists.automattic.com
Date: Thu, 31 May 2012 21:29:26 
To: <wp-hackers at lists.automattic.com>
Reply-To: wp-hackers at lists.automattic.com
Subject: [wp-hackers] WP_Query leaking absurd amounts of memory

Hello, all!

I'm super boggled with a strange (or maybe not?) memory leak issue.

Every time I call WP_Query() in the function below, Wordpress leaks 8 
megs of memory. And since I call this function a lot, things get hairy 
pretty quickly... :( I've tried unsetting the resulting $queryObject as 
well as periodically calling wp_cache_flush(), but neither seems to have 
any effect. Any thoughts?

-Rich


function get_post_ids_in_taxonomies($taxonomies, &$terms=array()) {
     $post_ids = array();

     $query = gen_query_get_posts_in_taxonomies($taxonomies, $terms);
     // var_dump($query);

     //Perform the query
     $queryObject = new WP_Query($query); //*****THE 8 MEGABYTES IS 
LEAKED HERE*****

     //For all posts found...
     if($queryObject->have_posts()) {
         while($queryObject->have_posts()) {
             $queryObject->the_post();

             //Get the $post_id by capturing the output of the_ID()
             ob_start();
             the_ID();
             $post_id = (int) ob_get_contents();
             ob_end_clean();

             // echo $post_id."\n";
             $post_ids[] = $post_id;
         }
     }

     unset($queryObject);

     return $post_ids;
}



gen_query_get_posts_in_taxonomies() is:

function gen_query_get_posts_in_taxonomies($taxonomies, &$terms=array()) {
     //General query params
     $query = array(
         'posts_per_page'    => -1,  //Get all posts (no paging)
         'tax_query'             => array('relation' => 'OR'),
     );

     //Add the specific taxonomies and terms onto $query['tax_query']
     foreach($taxonomies as $tax) {
         //Get terms in the taxonomies if we haven't yet
         if(!array_key_exists($tax, $terms)) {
             $terms[$tax] = array();

             $terms_tmp = get_terms($tax);
             foreach($terms_tmp as $tt)
                 $terms[$tax][] = $tt->term_taxonomy_id;
         }

         $query['tax_query'][] = array(
             'taxonomy' => $tax,
             'terms' => $terms[$tax],
             'field' => 'term_taxonomy_id',
         );
     }

     return $query;
}
_______________________________________________
wp-hackers mailing list
wp-hackers at lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list