[wp-trac] [WordPress Trac] #55206: wp core api memory leaks

WordPress Trac noreply at wordpress.org
Wed Feb 23 05:40:46 UTC 2022

#55206: wp core api memory leaks
 Reporter:  sllimrovert   |       Owner:  (none)
     Type:  defect (bug)  |      Status:  assigned
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Database      |     Version:  trunk
 Severity:  normal        |  Resolution:
 Keywords:  2nd-opinion   |     Focuses:  performance

Comment (by sllimrovert):

 Hi Sergey,

 Thank you for the response.

 I agree `SAVEQUERIES` is working as intended, but it's not uncommon for it
 to be on by default in a development environment.  It could potentially
 lead to confusing time spent by inexperienced devs who've coded themselves
 into a memory limit corner.  For this one, I'd solve it using the constant
 to initialize a `$save_queries` property of `$wpdb`, same as how
 `$show_errors` is.  It would make it useful for devs to be able to grab
 just the queries from their set of calls.

 For the cache one, two days ago I learned of a function called
 `wp_suspend_cache_addition`.  Thank you for that.  Totally solves the
 scenario.  Still, it's conceivable someone doing generally best practices
 using the common api could find themselves in OOM territory.  For this one
 I'd solve it with garbage collection.  If a group in the cache exceeds
 some memory threshold, it gets pruned.

 I just looked at WP Background Processing, another one new to me.  That's
 a beautifully written piece of code by the folks at Delicious Brains, as
 it usually is.  And in fact, didn't they even come across memory issues as
 evidenced by - https://github.com/deliciousbrains/wp-background-

 Me, I have a code snippet that I use when I need it, which is usually when
 processing thousands of unique posts.

  * Caching is normally good.  However, when running a big batch process,
 it often isn't.  We might be
  * caching things for the whole request that we really only need for the
 current record.  That's a
  * memory leak.  Additionally, if SAVEQUERIES is defined as true, then
 $wpdb saves all queries throughout
  * the request.  That's another memory leak.  We prevent these here with
 the next two lines.
  * @return void

 function wp_prevent_memory_leak() {
         global $wpdb;
         $wpdb->queries = array();

Ticket URL: <https://core.trac.wordpress.org/ticket/55206#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list