WordPress Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 77 bytes) in wp-includes/wp-db.php on line 1832
<p>I noticed recently on my WordPress website I'm getting sometimes 500 Internal Server Error. I checked logs and I have many lines like:</p>

<blockquote>
  <p>[Mon Oct 03 01:25:24.357439 2016] [fcgid:warn] [pid 12840] [client
  83.27.211.107:36968] mod_fcgid: stderr: PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 77 bytes)
  in /var/www/vhosts/mywebsite/public_html/wp-includes/wp-db.php on line
  1832</p>
</blockquote>

<p>I tried to increase memory limit:</p>

<blockquote>
  <p>define( 'WP_MAX_MEMORY_LIMIT' , '512M' );</p>
  
  <p>define( 'WP_MEMORY_LIMIT' , '512M' );</p>
</blockquote>

<p>And even more, but it didn't work. No matter what I set it still exceeding memory limit by some bytes. I think there's a problem with some queries to a database, but how to check which?</p>

<p>Content of the includes/wp-db.php:</p>

<pre><code>} else {
    $num_rows = 0;
    if ( $this-&gt;use_mysqli &amp;&amp; $this-&gt;result instanceof mysqli_result ) {
        while ( $row = mysqli_fetch_object( $this-&gt;result ) ) {
            $this-&gt;last_result[$num_rows] = $row;
            $num_rows++;
        }
    } elseif ( is_resource( $this-&gt;result ) ) {
        // server crashing at line below
        while ( $row = mysql_fetch_object( $this-&gt;result ) ) {
            $this-&gt;last_result[$num_rows] = $row;
            $num_rows++;
        }
    }

    // Log number of rows the query returned
    // and return number of rows selected
    $this-&gt;num_rows = $num_rows;
    $return_val     = $num_rows;
}
</code></pre>