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

Author Topic: Weird memory usage  (Read 1975 times)

0 Members and 1 Guest are viewing this topic.

lowarago

  • Newbie
  • *
  • Posts: 10
    • View Profile
Weird memory usage
« on: April 02, 2017, 12:56:16 am »
I am curious why does my game use 80 MB of memory.

I am using VS 2015 and when I launch my game I see that my process memory is at 88 MB min.
Its weird. I am using like 30 textures 30 sprites.
In those 30 textures I load images from my folder called "images". These images all together use 80 KB.
All these textures are then loaded in my 30 sprites.

I create textures as pointers. This way :
sf::Texture* tx_wall = new sf::Texture();
tx_wall->loadFromFile("images/wall.bmp");

//MAIN GAME LOOP HERE
//.
//.
//.
// END OF GAME LOOP

delete tx_wall;

 

I also noticed that sometimes when I start game process memory jumps to 110 MB and stays at that point. If I launch my game again 3 times (sometimes only once) it goes back to 88 MB.

If I remove 4 images (They take 16 KB) from my folder so textures are not able to open them, it reduces my process memory by 20 MB so now my memory usage is 60 MB.

These 4 specific images only. If I remove any other image it Memory usage stays the same.
These images are only used by my custom class which look like this :

my_button.h
#pragma once
#ifndef MY_BUTTON_H
#define MY_BUTTON_H
       
#include <SFML\Graphics.hpp>


        class Button {
                private:
                        float x, y;
                        sf::RectangleShape size;
                        sf::Sprite *my_sprite;
                        sf::Texture *my_texture;
                        sf::Texture *my_press_texture;
                        bool me_pressed = false;
                public:
                       
                        Button(const sf::Texture& txr, const float& x_spawn, const float& y_spawn);
                        Button(const sf::Texture& txr,const sf::Texture& txr_p,  const float& x_spawn, const float& y_spawn);
                        ~Button();
                        sf::RectangleShape getSize();
                        sf::Vector2f getPosition();

                        void setPosition(const float& x_spawn, const float& y_spawn);

                        void setTexture(const sf::Texture& txr);
                        void setPressTexture(const sf::Texture& txr);
                       
                        void pressed(bool p);
                        bool is_pressed();
                        bool is_hovered(const sf::Vector2i& mouse_pos);

                        sf::Sprite getSprite();
        };


#endif
 

and this is the

my_button.cpp

#include "my_Button.h"

Button::Button(const sf::Texture& txr, const float& x_spawn, const float& y_spawn){
        x = x_spawn;
        y = y_spawn;

        my_sprite = new sf::Sprite(txr);
        my_texture = new sf::Texture(txr);

        my_sprite->setPosition(x, y);

        size.setSize(sf::Vector2f(my_sprite->getGlobalBounds().width, my_sprite->getGlobalBounds().height));
}

Button::Button(const sf::Texture& txr, const sf::Texture& txr_p, const float& x_spawn, const float& y_spawn) {

        x = x_spawn;
        y = y_spawn;
       
        my_sprite        = new sf::Sprite(txr);
        my_texture       = new sf::Texture(txr);
        my_press_texture = new sf::Texture(txr_p);

        size.setSize(sf::Vector2f(my_sprite->getGlobalBounds().width, my_sprite->getGlobalBounds().height));
}
Button::~Button() {
        delete my_sprite;
        delete my_texture;
        delete my_press_texture;
}

sf::RectangleShape Button::getSize() {
        return size;
}

sf::Vector2f Button::getPosition() {
        return sf::Vector2f(x, y);
}

void Button::setTexture(const sf::Texture& txr) {
        delete my_sprite;
        delete my_texture;
        my_texture = new sf::Texture(txr);
        my_sprite = new sf::Sprite(txr);
}

void Button::setPressTexture(const sf::Texture& txr) {
        my_press_texture = new sf::Texture(txr);
}

bool Button::is_hovered(const sf::Vector2i& mouse_pos) {
       
        if (my_sprite->getGlobalBounds().contains((float)mouse_pos.x, (float)mouse_pos.y)) {
                return true;
        }
        return false;
}

sf::Sprite Button::getSprite() {
        return *my_sprite;
}

void Button::setPosition(const float& x_spawn, const float& y_spawn) {
        x = x_spawn;
        y = y_spawn;

        my_sprite->setPosition(x, y);
}

void Button::pressed(bool p) {
        me_pressed = p;

        if (me_pressed) {
                my_sprite->setTexture(*my_press_texture);
        }
        else {
                my_sprite->setTexture(*my_texture);
        }
}

bool Button::is_pressed() {
        return me_pressed;
}

 


I have 2 buttons which use these 4 images (2x idle and 2x pressed). If I remove them memory goes down by 20 MB.

the 2 BUTTONS :

Button *cmd_continue = new Button(*texture_continue, popup_win.getPosition().x+16, popup_win.getPosition().y + popup_win.getSize().y - 48);
        Button *cmd_leave = new Button(*texture_leave, popup_win.getPosition().x + (popup_win.getSize().x - 16 - texture_leave->getSize().x), popup_win.getPosition().y + popup_win.getSize().y - 48);

        cmd_continue->setPressTexture(*texture_continue_p);
        cmd_leave->setPressTexture(*texture_leave_p);

//MAIN GAME LOOP DOWN THERE
 



And I have other 4 Buttons (my class). If I remove images from folder that they used for their textures Process MEMORY STAYS THE SAME.

What is happening here ? Any ideas ?

I am using :
VS 2015
WIN 10 x64
RAM 6GB
CPU Intel Core 2 Duo E8600
GPU AMD 5670HD 1 GB



Other variables I use they shouldn't take that much memory. My Map is presented in 2D char vector which is 39*50. That is 0,23 KB


If I do
sf::Texture::getMaximumSize()
this returns 16384
« Last Edit: April 02, 2017, 01:08:39 am by lowarago »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Weird memory usage
« Reply #1 on: April 02, 2017, 02:51:22 am »
When you store an image it gets compressed. When you load an image in memory it will be stored uncompressed. So that's ((24bit * width * height)/8)/(2 * 1024) = x MB
Don't forget to run it in release mode, so ypu won't have all the debug symbols etc. loaded.
And finally the system will allocate more memory than needed ​for your application, so the application won't have to ask the OS for more memory (slow operation) whenever you allocate something new.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lowarago

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Weird memory usage
« Reply #2 on: April 02, 2017, 09:20:55 am »
@eXpl0it3r Can you give me an example how should I do it ?

I am already running it in release mode. My images are all 128x32, each of them use 1 KB of space.
So by doing this ((24bit * 128 * 32)/8)/(2 * 1024) I get the result 6MB ?
« Last Edit: April 02, 2017, 09:31:05 am by lowarago »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Weird memory usage
« Reply #3 on: April 03, 2017, 05:48:20 pm »
Not sure how you'd got to 6MB, but with the given size, that would be 12kB per texture, and with 32 textures, that'd be 384kB.
Just ask yourself, what exactly does it matter if your application uses 80 or 20MiB or RAM? Is it worth your time to think about it for hours?
If you feel that it's very important, then you should take a different approach than counting your resource files and instead start looking into tools that will tell you exactly what has been allocated and how. Or look into how your OS is handling allocations, how much is actually used and how much is just reserve, etc.
In most cases it's really not important. As long as your application isn't ramping up to some potential limits of very low-end systems (like 2-4 GB of RAM), it most of the time is not worth spending too much time thinking about it. I'm not saying you can't, but I'm saying, that you're way better off investing your time into working on your game, implementing all the features you want and can thinking of.
Your OS knows how do deal with RAM usage and SFML isn't misusing memory, so chances are that you'll never run into any memory issues with your game. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything