Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Why is this taking up so much memory  (Read 2271 times)

0 Members and 1 Guest are viewing this topic.

stati30241

  • Newbie
  • *
  • Posts: 2
    • View Profile
Why is this taking up so much memory
« on: November 14, 2020, 08:09:25 pm »
I was having a problem when I tried to render all the pixels in the window separately, so here a simpler version of my problem. Whenever I try to run this code, the memory usage starts growing very fast and reaches up to 365MB before stopping. Why is this taking up so much memory and how can I prevent this?
#include <SFML/Graphics.hpp>

int main() {
        sf::RenderWindow window{ sf::VideoMode{ 1024, 960 }, "Pixels", sf::Style::Close };

        while (window.isOpen()) {
                sf::Event sfmlEvent;
                while (window.pollEvent(sfmlEvent)) {
                        if (sfmlEvent.type == sf::Event::Closed) {
                                window.close();
                        }
                }

                window.clear();

                for (int i = 0; i < window.getSize().x; ++i) {
                        for (int j = 0; j < window.getSize().y; ++j) {
                                sf::RectangleShape rect{ { 1.0f, 1.0f } };
                                rect.setPosition(i, j);
                                window.draw(rect);
                        }
                }

                window.display();
        }

        return 0;
}

 

Paul

  • Jr. Member
  • **
  • Posts: 78
    • View Profile
Re: Why is this taking up so much memory
« Reply #1 on: November 15, 2020, 12:27:30 pm »
This code doesn't fill memory but burden CPU. If you want draw 1 000 000 pixels use vertex array and initialize them before clearing the screen.

https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-particle-system

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Why is this taking up so much memory
« Reply #2 on: November 15, 2020, 03:18:16 pm »
Take a memory profiler and figure it out if you're that interested. ;)

The OS can do a lot of things when it comes to memory allocations that isn't directly obvious from a source code point of view. For example it can just assign some memory to an application, even if the application isn't actively using, but so for the next memory allocation request, the OS doesn't have to assign some more memory, but it's already reserved.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything