SFML community forums

Help => Graphics => Topic started by: upseen on December 07, 2013, 10:22:39 am

Title: [Solved] Freeing memory and sf::Texture
Post by: upseen on December 07, 2013, 10:22:39 am
Hello,

Given the minimal code below, at first run, in the Windows' task manager, my application is getting around 7000K memory, and after pressing a key (loading a texture), it takes 9000K. The problem is that memory stays at 9000K forever after, even when i free the texture memory, and reload it. Is this normal?:

#include <SFML/Graphics.hpp>
int main(int argc, char* argv[])
{
    sf::RenderWindow window( { 800, 600 }, "Testwindow" );
    window.setFramerateLimit( 60 );

    sf::Texture* tex{ nullptr };
    sf::Sprite sprite;

    while( window.isOpen() ) {
        sf::Event event;
        while( window.pollEvent( event ) ) {
                if ( event.type == sf::Event::Closed ) {
                        window.close();
                }
                else if ( event.type == sf::Event::KeyReleased ) {
                        if ( tex ) {
                                delete tex;
                                tex = nullptr;
                                sprite = sf::Sprite();
                        }
                        else {
                                tex = new sf::Texture();
                                tex->loadFromFile( "test.png" );
                                sprite.setTexture( *tex );
                        }
                }
        }

        window.clear();
        if ( tex ) {
                window.draw( sprite );
        }
        window.display();
    }

    if ( tex ) delete tex;
    return 0;
}

I'm using SFML2.1. Thanks.
Title: AW: Freeing memory and sf::Texture
Post by: eXpl0it3r on December 07, 2013, 10:37:55 am
First you should not manage memory manually with new/delete. Use smart pointers instead, they make use of RAII.

Secondly the task managed is not the way to monitor memory consumption. Allocating memory via OS is an expensive operation and since one allocation often doesn't comd alone, the OS is "smart" and assignes more memory than requested to your application and "frees" the memory only when either needed by the OS/other applications or when the unused memory is big enough.

In general if you strictly follow RAII you should not have to worry about memory consumption too much if at all. ;)
Title: Re: Freeing memory and sf::Texture
Post by: upseen on December 07, 2013, 11:02:43 am
Secondly the task managed is not the way to monitor memory consumption. Allocating memory via OS is an expensive operation and since one allocation often doesn't comd alone, the OS is "smart" and assignes more memory than requested to your application and "frees" the memory only when either needed by the OS/other applications or when the unused memory is big enough.

Explains, thanks.

And actually I'm originally using the std smart pointers and co for the resource manager. Just had to make it as simple as possible. Thanks again ;)