From ce9a9ebbf9eef82fdff9a77b63fdd99097c85bd6 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Fri, 12 Mar 2021 19:41:27 +0100 Subject: [PATCH] Clean memory only if needed --- td/telegram/AnimationsManager.cpp | 7 +++++-- td/telegram/ContactsManager.cpp | 7 +++++-- td/telegram/GroupCallManager.cpp | 7 +++++-- td/telegram/InlineQueriesManager.cpp | 7 +++++-- td/telegram/MemoryManager.cpp | 8 +++++++- td/telegram/MessagesManager.cpp | 7 +++++-- td/telegram/PollManager.cpp | 7 +++++-- td/telegram/files/FileManager.cpp | 7 +++++-- 8 files changed, 42 insertions(+), 15 deletions(-) diff --git a/td/telegram/AnimationsManager.cpp b/td/telegram/AnimationsManager.cpp index caa54c2cb..37d58702c 100644 --- a/td/telegram/AnimationsManager.cpp +++ b/td/telegram/AnimationsManager.cpp @@ -11,6 +11,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/Document.h" #include "td/telegram/DocumentsManager.h" +#include "td/telegram/MemoryManager.h" #include "td/telegram/FileReferenceManager.h" #include "td/telegram/files/FileManager.h" #include "td/telegram/files/FileType.h" @@ -146,8 +147,10 @@ AnimationsManager::AnimationsManager(Td *td, ActorShared<> parent) : td_(td), pa void AnimationsManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (td_->memory_manager_->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } int32 AnimationsManager::get_animation_duration(FileId file_id) const { diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 6d2d84be9..5c64e5234 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -26,6 +26,7 @@ #include "td/telegram/InputGroupCallId.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/logevent/LogEventHelper.h" +#include "td/telegram/MemoryManager.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/MessageTtlSetting.h" #include "td/telegram/misc.h" @@ -3450,8 +3451,10 @@ ContactsManager::~ContactsManager() = default; void ContactsManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (td_->memory_manager_->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } UserId ContactsManager::load_my_id() { diff --git a/td/telegram/GroupCallManager.cpp b/td/telegram/GroupCallManager.cpp index e092f64eb..18b909c81 100644 --- a/td/telegram/GroupCallManager.cpp +++ b/td/telegram/GroupCallManager.cpp @@ -13,6 +13,7 @@ #include "td/telegram/ContactsManager.h" #include "td/telegram/Global.h" #include "td/telegram/MessageId.h" +#include "td/telegram/MemoryManager.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/misc.h" #include "td/telegram/net/NetQuery.h" @@ -478,8 +479,10 @@ GroupCallManager::~GroupCallManager() = default; void GroupCallManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (td_->memory_manager_->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } void GroupCallManager::memory_cleanup() { diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index af2773d44..53c6e4915 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -18,6 +18,7 @@ #include "td/telegram/Contact.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/Document.h" +#include "td/telegram/MemoryManager.h" #include "td/telegram/DocumentsManager.h" #include "td/telegram/files/FileManager.h" #include "td/telegram/files/FileType.h" @@ -166,8 +167,10 @@ InlineQueriesManager::InlineQueriesManager(Td *td, ActorShared<> parent) : td_(t void InlineQueriesManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (td_->memory_manager_->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } void InlineQueriesManager::on_drop_inline_query_result_timeout_callback(void *inline_queries_manager_ptr, diff --git a/td/telegram/MemoryManager.cpp b/td/telegram/MemoryManager.cpp index 933bc15ea..5370164a5 100644 --- a/td/telegram/MemoryManager.cpp +++ b/td/telegram/MemoryManager.cpp @@ -85,7 +85,13 @@ void MemoryManager::tear_down() { } bool MemoryManager::can_manage_memory() const { - return td_->auth_manager_->is_authorized() && !G()->close_flag(); + if (!(td_->auth_manager_->is_authorized() && !G()->close_flag())) { + return false; + } + if (G()->parameters().use_message_db || G()->parameters().use_chat_info_db || G()->parameters().use_file_db) { + return false; + } + return true; } void MemoryManager::get_memory_stats(bool full, Promise promise) const { diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 2bc52ca18..9496c0344 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -29,6 +29,7 @@ #include "td/telegram/InputMessageText.h" #include "td/telegram/Location.h" #include "td/telegram/logevent/LogEvent.h" +#include "td/telegram/MemoryManager.h" #include "td/telegram/MessageContent.h" #include "td/telegram/MessageEntity.h" #include "td/telegram/MessageEntity.hpp" @@ -12441,8 +12442,10 @@ class MessagesManager::DialogFiltersLogEvent { void MessagesManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (td_->memory_manager_->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } void MessagesManager::start_up() { diff --git a/td/telegram/PollManager.cpp b/td/telegram/PollManager.cpp index 4081f7875..246139985 100644 --- a/td/telegram/PollManager.cpp +++ b/td/telegram/PollManager.cpp @@ -14,6 +14,7 @@ #include "td/telegram/Global.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/logevent/LogEventHelper.h" +#include "td/telegram/MemoryManager.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/misc.h" #include "td/telegram/net/NetActor.h" @@ -265,8 +266,10 @@ void PollManager::start_up() { void PollManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (td_->memory_manager_->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } PollManager::~PollManager() = default; diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 117509a8a..28091c851 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -15,6 +15,7 @@ #include "td/telegram/files/FileLoaderUtils.h" #include "td/telegram/files/FileLocation.h" #include "td/telegram/files/FileLocation.hpp" +#include "td/telegram/MemoryManager.h" #include "td/telegram/Global.h" #include "td/telegram/logevent/LogEvent.h" #include "td/telegram/misc.h" @@ -4196,8 +4197,10 @@ void FileManager::memory_stats(vector &output) { void FileManager::tear_down() { parent_.reset(); - // Completely clear memory when closing, to avoid memory leaks - memory_cleanup(true); + if (G()->memory_manager().get_actor_unsafe()->can_manage_memory()) { + // Completely clear memory when closing, to avoid memory leaks + memory_cleanup(true); + } } } // namespace td