Skip to content

Commit

Permalink
Avoid running out of memory and clear object cache between pages in i…
Browse files Browse the repository at this point in the history
…ndex_helper

Experiencing fatal out of memory messages, thought this would be a useful addition. Kept dying at about 11k posts, now able to consume all 21201 posts.
  • Loading branch information
sc0ttkclark committed Oct 20, 2014
1 parent 7bc2e45 commit 29c4b0a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion bin/wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ public function index( $args, $assoc_args ) {
* @return array
*/
private function _index_helper( $no_bulk = false, $posts_per_page) {
global $wpdb, $wp_object_cache;
$synced = 0;
$errors = array();
$offset = 0;
Expand Down Expand Up @@ -297,6 +298,20 @@ private function _index_helper( $no_bulk = false, $posts_per_page) {
$offset += $posts_per_page;

usleep( 500 );

// Avoid running out of memory
$wpdb->queries = array();

if ( is_object( $wp_object_cache ) ) {
$wp_object_cache->group_ops = array();
$wp_object_cache->stats = array();
$wp_object_cache->memcache_debug = array();
$wp_object_cache->cache = array();

if ( is_callable( $wp_object_cache, '__remoteset' ) ) {
call_user_func( array( $wp_object_cache, '__remoteset' ) ); // important
}
}
}

if ( ! $no_bulk ) {
Expand Down Expand Up @@ -557,4 +572,4 @@ private function _connect_check() {
WP_CLI::error( __( 'Unable to reach Elasticsearch Server! Check that service is running.', 'elasticpress' ) );
}
}
}
}

0 comments on commit 29c4b0a

Please sign in to comment.