From b1e9774e62a5686afa4ceb0028d776df81020796 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Tue, 19 May 2020 01:12:11 +0200 Subject: [PATCH] Free memory --- td/telegram/ContactsManager.cpp | 23 +++++++++++++++++++++++ td/telegram/DocumentsManager.cpp | 1 + td/telegram/StickersManager.cpp | 13 +++++++++++++ td/telegram/Td.cpp | 2 ++ td/telegram/Td.h | 1 + td/telegram/VideoNotesManager.cpp | 1 + td/telegram/VideosManager.cpp | 1 + td/telegram/WebPagesManager.cpp | 15 ++++++++++----- td/telegram/files/FileManager.cpp | 13 ++++++++----- td/telegram/files/FileManager.h | 2 +- 10 files changed, 61 insertions(+), 11 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 4931dafe..f1d0b4c5 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -13611,27 +13611,50 @@ void ContactsManager::get_current_state(vectormemory_cleanup(); file_manager_->memory_cleanup(); + malloc_trim(0); + std::vector file_types; for (auto &file_type : request.file_types_) { if (file_type == nullptr) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 6b6873ac..6fd8cb71 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -30,6 +30,7 @@ #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include #include #include #include diff --git a/td/telegram/VideoNotesManager.cpp b/td/telegram/VideoNotesManager.cpp index 79aa0105..351aa2fc 100644 --- a/td/telegram/VideoNotesManager.cpp +++ b/td/telegram/VideoNotesManager.cpp @@ -248,6 +248,7 @@ tl_object_ptr VideoNotesManager::get_input_media( void VideoNotesManager::memory_cleanup() { video_notes_.clear(); + video_notes_.rehash(0); } } // namespace td diff --git a/td/telegram/VideosManager.cpp b/td/telegram/VideosManager.cpp index f20c1744..00c58809 100644 --- a/td/telegram/VideosManager.cpp +++ b/td/telegram/VideosManager.cpp @@ -302,6 +302,7 @@ string VideosManager::get_video_search_text(FileId file_id) const { } void VideosManager::memory_cleanup() { videos_.clear(); + videos_.rehash(0); } } // namespace td diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 56fa05b2..03df81f4 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -1598,10 +1598,15 @@ vector WebPagesManager::get_web_page_file_ids(const WebPage *web_page) c } void WebPagesManager::memory_cleanup() { -// web_pages_.clear(); -// web_page_messages_.clear(); -// got_web_page_previews_.clear(); -// url_to_web_page_id_.clear(); -// url_to_file_source_id_.clear(); + web_pages_.clear(); + web_pages_.rehash(0); + web_page_messages_.clear(); + web_page_messages_.rehash(0); + got_web_page_previews_.clear(); + got_web_page_previews_.rehash(0); + url_to_web_page_id_.clear(); + url_to_web_page_id_.rehash(0); + url_to_file_source_id_.clear(); + url_to_file_source_id_.rehash(0); } } // namespace td diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 6a2e5968..69528efa 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -3456,7 +3456,7 @@ void FileManager::hangup() { } void FileManager::memory_cleanup() { - if (file_id_info_.size() > 1000 && empty_file_ids_.size() < 200) { + if (file_id_info_.size() > 1000 || empty_file_ids_.size() < 200) { is_closed_ = true; auto time = std::time(nullptr); @@ -3477,7 +3477,7 @@ void FileManager::memory_cleanup() { /* DESTROY ASSOCIATED FILE */ empty_file_ids_.push_back(file_id.get()); - file_id_info_[file_id.get()] = FileIdInfo(); + file_id_info_.erase(file_id_info_.begin() + ((int32) file_id.get())); } /* DESTROY MAIN QUERY */ @@ -3489,9 +3489,9 @@ void FileManager::memory_cleanup() { } /* DESTROY MAIN FILE */ - file_id_insert_time_[i] = INT64_MAX; + file_id_insert_time_.erase(((int32) i)); empty_file_ids_.push_back(i); - file_id_info_[i] = FileIdInfo(); + file_id_info_.erase(file_id_info_.begin() + ((int32) i)); /* DESTROY MAIN NODE */ file_nodes_[main_node_id] = nullptr; @@ -3500,6 +3500,9 @@ void FileManager::memory_cleanup() { } } + vector(file_id_info_).swap(file_id_info_); + vector(empty_file_ids_).swap(empty_file_ids_); + LOG(INFO) << empty_file_ids_.size() << " empty ids and " << queries_container_.size() << " running queries"; is_closed_ = false; } @@ -3508,4 +3511,4 @@ void FileManager::memory_cleanup() { void FileManager::tear_down() { parent_.reset(); } -} // namespace tds +} // namespace td diff --git a/td/telegram/files/FileManager.h b/td/telegram/files/FileManager.h index e9b6c4b1..09893749 100644 --- a/td/telegram/files/FileManager.h +++ b/td/telegram/files/FileManager.h @@ -557,7 +557,7 @@ class FileManager : public FileLoadManager::Callback { std::map pmc_id_to_file_node_id_; std::unordered_map file_id_insert_time_; - vector file_id_info_; + std::vector file_id_info_; vector empty_file_ids_; vector> file_nodes_; ActorOwn file_load_manager_;