WordPress.org

Make WordPress Core

#44523 closed defect (bug) (invalid)

Huge hemory consumption in get_comments()

Reported by: vijantis Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.6
Component: Comments Keywords:
Focuses: performance Cc:
PR Number:

Description

Hello,

we have some serious memory issues with the website http://test8.transinformation.net.
The site is a large community, and has plenty posts and comments etc.
With certain content we exceed the 256MB limit. This happens e.g. after a successfull login.
(This subdomain is for the new theme relaunch, but contains a copy of all data. The live site also has this issue from time to time)

By adding debug functions before and after database calls to detect significant memory increases (this is where the website "chrashed" first), there is a huge increase of the memory usage after certain database queries.

See attached file.

Disabling the _prime_comment_caches in _prime_comment_caches helps a bit, getting the page to render, but peak memory usage is high with 206 MB.

Disabling the get_comments call in custom_functions helps, getting the page to render with a peak memory usage of 145 MB.
The code here is pretty simple, and should not cause any memory leaks, except maybe for the "separate_comments" function:

function et_comment_count( $count, $post_id ) {
        $is_doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX ? true : false;

        if ( ! is_admin() || $is_doing_ajax ) {
                global $id;
                $post_id = $post_id ? $post_id : $id;
                $get_comments = get_comments( array('post_id' => $post_id, 'status' => 'approve') );
                $comments_by_type = separate_comments( $get_comments );
                return count( $comments_by_type['comment'] );
        } else {
                return $count;
        }
}

Best regards
Jan Vorel

Attachments (1)

debug1.html (137.9 KB) - added by vijantis 19 months ago.

Download all attachments as: .zip

Change History (3)

@vijantis
19 months ago

#1 @SergeyBiryukov
19 months ago

  • Focuses performance added
  • Summary changed from Huge hemory consumption to Huge hemory consumption in get_comments()

#2 @swissspidy
19 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Severity changed from major to normal
  • Status changed from new to closed

Hi there and welcome to WordPress Trac!

As far as I can see this is not an issue with WordPress, but your implementation.

In your et_comment_count() function (which is part of your theme, not WordPress) you're loading all comments into memory by using get_comments(). Of course that leads to high memory consumption.

WordPress already offers wp_count_comments() and get_comment_count() to get the comment count.

If you really need the comment count per comment type, I suggest you to copy and adapt these functions, including the caching part.

Note: See TracTickets for help on using tickets.