diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-04-06 04:42:24 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-04-06 04:42:24 +0200 |
commit | 797a36228bdb3b77b7314585a31de6f27feab51d (patch) | |
tree | 211b9dfbfe1922ce20977618cc73c8960245b79a | |
parent | 8d8c01258a9414b90ecf3e1e374390543c29c4d9 (diff) |
Use malloc_trim to reduce memory usage after freeing memory
-rw-r--r-- | include/Body.hpp | 2 | ||||
-rw-r--r-- | src/Body.cpp | 20 | ||||
-rw-r--r-- | src/ImageViewer.cpp | 7 | ||||
-rw-r--r-- | src/QuickMedia.cpp | 24 | ||||
-rw-r--r-- | src/ResourceLoader.cpp | 4 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 2 |
6 files changed, 49 insertions, 10 deletions
diff --git a/include/Body.hpp b/include/Body.hpp index cc11664..eb74837 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -178,6 +178,7 @@ namespace QuickMedia { class Body { public: Body(Program *program, sf::Texture &loading_icon_texture); + ~Body(); // Select previous page, ignoring invisible items. Returns true if the item was changed. This can be used to check if the top was hit when wrap_around is set to false bool select_previous_page(); @@ -309,6 +310,7 @@ namespace QuickMedia { sf::Vector2f body_size; float selected_item_height = 0.0f; float selected_scrolled = 0.0f; + bool loaded_textures_changed = false; //float scroll_y = 0.0f; }; }
\ No newline at end of file diff --git a/src/Body.cpp b/src/Body.cpp index ced71fa..61d319a 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -10,6 +10,7 @@ #include <SFML/OpenGL.hpp> #include <assert.h> #include <cmath> +#include <malloc.h> static const sf::Color front_color(32, 36, 42); static const sf::Color back_color(33, 35, 37); @@ -140,6 +141,12 @@ namespace QuickMedia { loading_icon.setOrigin(loading_icon_size.x * 0.5f, loading_icon_size.y * 0.5f); } + Body::~Body() { + item_thumbnail_textures.clear(); + items.clear(); + malloc_trim(0); + } + // TODO: Make this work with wraparound enabled? // TODO: For plugins with different sized body items this can be weird, because after scrolling down thumbnails could load and they could move items up/down until we see items we haven't seen bool Body::select_previous_page() { @@ -326,6 +333,7 @@ namespace QuickMedia { void Body::clear_cache() { clear_text_cache(); clear_thumbnails(); + malloc_trim(0); } void Body::clear_text_cache() { @@ -749,10 +757,12 @@ namespace QuickMedia { mouse_left_clicked = false; for(auto it = item_thumbnail_textures.begin(); it != item_thumbnail_textures.end();) { - if(!it->second->referenced) + if(!it->second->referenced) { it = item_thumbnail_textures.erase(it); - else + loaded_textures_changed = true; + } else { ++it; + } } // TODO: Only do this for items that are not visible, do not loop all items. @@ -764,6 +774,11 @@ namespace QuickMedia { if(body_item->embedded_item && elapsed_time_sec - body_item->embedded_item->last_drawn_time >= 1.5) clear_body_item_cache(body_item->embedded_item.get()); } + + if(loaded_textures_changed) { + loaded_textures_changed = false; + malloc_trim(0); + } } void Body::update_dirty_state(BodyItem *body_item, float width) { @@ -1162,6 +1177,7 @@ namespace QuickMedia { item_thumbnail->image.reset(); item_thumbnail->loading_state = LoadingState::APPLIED_TO_TEXTURE; item_thumbnail->texture_applied_time.restart(); + loaded_textures_changed = true; } } diff --git a/src/ImageViewer.cpp b/src/ImageViewer.cpp index 6efc688..9297edf 100644 --- a/src/ImageViewer.cpp +++ b/src/ImageViewer.cpp @@ -5,6 +5,7 @@ #include "../include/ResourceLoader.hpp" #include "../plugins/Manga.hpp" #include <cmath> +#include <malloc.h> #include <SFML/Window/Event.hpp> #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RectangleShape.hpp> @@ -265,6 +266,7 @@ namespace QuickMedia { min_page_center_dist = 9999999.0; page_closest_to_center = -1; + bool loaded_textures_changed = false; int page_i = 0; for(auto &page_data : image_data) { if(page_data) { @@ -287,6 +289,7 @@ namespace QuickMedia { page_data->image_status = ImageStatus::FAILED_TO_LOAD; } page_data->image.reset(); + loaded_textures_changed = true; } page_data->visible_on_screen = false; } @@ -349,10 +352,14 @@ namespace QuickMedia { if(page_data && !page_data->visible_on_screen) { fprintf(stderr, "ImageViewer: Unloaded page %d\n", 1 + i); page_data.reset(); + loaded_textures_changed = true; } ++i; } + if(loaded_textures_changed) + malloc_trim(0); + return ImageViewerAction::NONE; } diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index ea1292d..8a500fe 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -32,6 +32,7 @@ #include <cmath> #include <string.h> #include <signal.h> +#include <malloc.h> #include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Window/Clipboard.hpp> @@ -1047,6 +1048,8 @@ namespace QuickMedia { } void Program::page_loop(std::vector<Tab> &tabs, int start_tab_index, PageLoopSubmitHandler after_submit_handler) { + malloc_trim(0); + if(tabs.empty()) { show_notification("QuickMedia", "No tabs provided!", Urgency::CRITICAL); return; @@ -1177,6 +1180,7 @@ namespace QuickMedia { } } window.setKeyRepeatEnabled(true); + malloc_trim(0); } else if(new_tabs.size() == 1 && new_tabs[0].page->get_type() == PageTypez::IMAGE_BOARD_THREAD) { current_page = PageType::IMAGE_BOARD_THREAD; image_board_thread_page(static_cast<ImageBoardThreadPage*>(new_tabs[0].page.get()), new_tabs[0].body.get()); @@ -1920,6 +1924,8 @@ namespace QuickMedia { } else if(event.type == sf::Event::KeyPressed && (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q || event.key.code == sf::Keyboard::Backspace)) { // To be able to close the video player while the video is loading current_page = previous_page; + } else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::C && event.key.control) { + save_video_url_to_clipboard(); } } @@ -2244,15 +2250,15 @@ namespace QuickMedia { if(try_backup_url) { image_content.clear(); if(download_to_string(new_url, image_content, extra_args, true) != DownloadResult::OK || image_content.size() <= 255) { - show_notification("QuickMedia", "Failed to download image: " + new_url, Urgency::CRITICAL); + if(!image_download_cancel) show_notification("QuickMedia", "Failed to download image: " + new_url, Urgency::CRITICAL); return true; } } else { - show_notification("QuickMedia", "Failed to download image: " + url, Urgency::CRITICAL); + if(!image_download_cancel) show_notification("QuickMedia", "Failed to download image: " + url, Urgency::CRITICAL); return true; } } else { - show_notification("QuickMedia", "Failed to download image: " + url, Urgency::CRITICAL); + if(!image_download_cancel) show_notification("QuickMedia", "Failed to download image: " + url, Urgency::CRITICAL); return true; } } @@ -2397,6 +2403,8 @@ namespace QuickMedia { // Consume events sent during above call to get_number_of_images which sends a request to server which may take a while. We dont want pages to be skipped when pressing arrow up/down while(window.pollEvent(event)) {} + malloc_trim(0); + // TODO: Show to user if a certain page is missing (by checking page name (number) and checking if some are skipped) while (current_page == PageType::IMAGES && window.isOpen()) { while(window.pollEvent(event)) { @@ -2459,11 +2467,11 @@ namespace QuickMedia { } const float font_height = chapter_text.getCharacterSize() + 8.0f; - const float background_height = font_height + 6.0f; + const float bottom_panel_height = font_height + 6.0f; sf::Vector2f content_size; content_size.x = window_size.x; - content_size.y = window_size.y - background_height; + content_size.y = window_size.y - bottom_panel_height; if(redraw) { redraw = false; @@ -2492,12 +2500,12 @@ namespace QuickMedia { window.draw(image); } - chapter_text_background.setSize(sf::Vector2f(window_size.x, background_height)); - chapter_text_background.setPosition(0.0f, std::floor(window_size.y - background_height)); + chapter_text_background.setSize(sf::Vector2f(window_size.x, bottom_panel_height)); + chapter_text_background.setPosition(0.0f, std::floor(window_size.y - bottom_panel_height)); window.draw(chapter_text_background); auto text_bounds = chapter_text.getLocalBounds(); - chapter_text.setPosition(std::floor(window_size.x * 0.5f - text_bounds.width * 0.5f), std::floor(window_size.y - background_height * 0.5f - font_height * 0.5f)); + chapter_text.setPosition(std::floor(window_size.x * 0.5f - text_bounds.width * 0.5f), std::floor(window_size.y - bottom_panel_height * 0.5f - font_height * 0.5f)); window.draw(chapter_text); window.display(); diff --git a/src/ResourceLoader.cpp b/src/ResourceLoader.cpp index 1665e4d..f6c8354 100644 --- a/src/ResourceLoader.cpp +++ b/src/ResourceLoader.cpp @@ -3,6 +3,7 @@ #include <SFML/Graphics/Texture.hpp> #include <array> #include <unordered_map> +#include <malloc.h> #include <assert.h> static std::string resource_root; @@ -56,6 +57,7 @@ namespace QuickMedia::FontLoader { font = new_font.get(); font_cache[(size_t)font_type] = std::move(new_font); + malloc_trim(0); } return font; } @@ -73,8 +75,10 @@ namespace QuickMedia::TextureLoader { sf::Texture *result = new_texture.get(); if(!new_texture->loadFromFile(resource_root + str)) fprintf(stderr, "Failed to load image: %s%s\n", resource_root.c_str(), filepath); + new_texture->setSmooth(true); texture_cache[str] = std::move(new_texture); + malloc_trim(0); return result; } }
\ No newline at end of file diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 583bad8..0c44130 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -16,6 +16,7 @@ #include <cmath> #include <fcntl.h> #include <unistd.h> +#include <malloc.h> #include "../../include/QuickMedia.hpp" // TODO: Use string assign with string length instead of assigning to c string (which calls strlen) @@ -1249,6 +1250,7 @@ namespace QuickMedia { filter_encoded = url_param_encode(CONTINUE_FILTER); additional_messages_queue.push(true); + malloc_trim(0); } #if 0 |