Clean memory only if needed

This commit is contained in:
Andrea Cavalli 2021-03-12 19:41:27 +01:00
parent 062d1c2f5a
commit ce9a9ebbf9
8 changed files with 42 additions and 15 deletions

View File

@ -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 {

View File

@ -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() {

View File

@ -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() {

View File

@ -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,

View File

@ -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<MemoryStats> promise) const {

View File

@ -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() {

View File

@ -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;

View File

@ -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<string> &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