A free, self-hostable news aggregator…
Attempt to reduce max memory usage during actualize (#2955)
Alexandre Alapetiteaea380659014
diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.phpindex a2dcd360f..de0e6f9a1 100755--- a/app/Controllers/feedController.php+++ b/app/Controllers/feedController.php@@ -333,9 +333,9 @@ class FreshRSS_feed_Controller extends Minz_ActionController {$needFeedCacheRefresh = false;- // We want chronological order and SimplePie uses reverse order.- $entries = array_reverse($feed->entries());- if (count($entries) > 0) {+ $entries = $feed->entries();+ $nbEntries = count($entries);+ if ($nbEntries > 0) {$newGuids = array();foreach ($entries as $entry) {$newGuids[] = safe_ascii($entry->guid());@@ -346,7 +346,8 @@ class FreshRSS_feed_Controller extends Minz_ActionController {$oldGuids = array();// Add entries in database if possible.- foreach ($entries as $entry) {+ for ($i = 0; $i < $nbEntries; $i++) {+ $entry = $entries[$i];if (isset($newGuids[$entry->guid()])) {continue; //Skip subsequent articles with same GUID}@@ -405,9 +406,12 @@ class FreshRSS_feed_Controller extends Minz_ActionController {$entryDAO->addEntry($entry->toArray());$nb_new_articles++;}+ unset($entry);+ unset($entries[$i]);}$entryDAO->updateLastSeen($feed->id(), $oldGuids, $mtime);}+ unset($entries);if (mt_rand(0, 30) === 1) { // Remove old entries once in 30.if (!$entryDAO->inTransaction()) {@@ -455,6 +459,7 @@ class FreshRSS_feed_Controller extends Minz_ActionController {$feed->unlock();$updated_feeds++;unset($feed);+ gc_collect_cycles();// No more than $maxFeeds feeds unless $force is true to avoid overloading// the server.diff --git a/app/Models/Feed.php b/app/Models/Feed.phpindex 34c036921..1b9bd6f9f 100644--- a/app/Models/Feed.php+++ b/app/Models/Feed.php@@ -333,7 +333,12 @@ class FreshRSS_Feed extends Minz_Model {$guids = array();$hasUniqueGuids = true;- foreach ($feed->get_items() as $item) {+ // We want chronological order and SimplePie uses reverse order.+ for ($i = $feed->get_item_quantity() - 1; $i >= 0; $i--) {+ $item = $feed->get_item($i);+ if ($item == null) {+ continue;+ }$title = html_only_entity_decode(strip_tags($item->get_title()));$authors = $item->get_authors();$link = $item->get_permalink();@@ -414,6 +419,7 @@ class FreshRSS_Feed extends Minz_Model {}$guid = $item->get_id(false, false);+ unset($item);$hasUniqueGuids &= empty($guids['_' . $guid]);$guids['_' . $guid] = true;$author_names = '';@@ -441,7 +447,6 @@ class FreshRSS_Feed extends Minz_Model {}$entries[] = $entry;- unset($item);}$hasBadGuids = $this->attributes('hasBadGuids');diff --git a/app/actualize_script.php b/app/actualize_script.phpindex 399f7b18e..e77d637cf 100755--- a/app/actualize_script.php+++ b/app/actualize_script.php@@ -69,6 +69,7 @@ foreach ($users as $user) {fwrite(STDERR, 'FreshRSS write access problem in ' . join_path(USERS_PATH, $user, 'log.txt') . "\n");}}+ gc_collect_cycles();}Minz_Log::notice('FreshRSS actualize done.', ADMIN_LOG);@@ -78,6 +79,7 @@ if (defined('STDOUT')) {$duration = date_diff($end_date, $begin_date);fwrite(STDOUT, 'Ending feed actualization at ' . $end_date->format('c') . "\n"); //Unbufferedfwrite(STDOUT, 'Feed actualizations took ' . $duration->format('%a day(s), %h hour(s), %i minute(s) and %s seconds') . ' for ' . count($users) . " users\n"); //Unbuffered+ fwrite(STDOUT, 'Memory usage: ' . format_bytes(memory_get_peak_usage(true)) . "\n"); //Unbuffered}echo 'End.', "\n";ob_end_flush();