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

Author Topic: Failed to load image. Reason: Out of memory  (Read 1677 times)

0 Members and 1 Guest are viewing this topic.

_Fiction_

  • Newbie
  • *
  • Posts: 35
    • View Profile
Failed to load image. Reason: Out of memory
« on: October 27, 2020, 10:12:25 pm »
Hi. I am developing a game with SFML, and I'm running into a memory issue that I don't understand. If I load too much memory in textures, I get a message "Failed to load image. Reason: Out of memory" from SFML. The reason that this is confusing is I feel like I'm using significantly less memory than should cause this issue. I have a modern computer with a solid graphics card (NVIDIA GeForce GTX 1060 6GB). I have written a small program to demonstrate my issue. Using the formula 2048x2048x4x(number of textures) to figure out roughly how much memory I've used, it crashes at around 1700 MB, or 100 textures of size 2048x2048. Not sure if the number is right. Does anyone know what I'm doing wrong or what this issue is?

#include <iostream>
#include <SFML\Graphics.hpp>
#include "Tileset.h"
#include <SFML/OpenGL.hpp>

using namespace std;
using namespace sf;

const static int NUM_TEX = 1000;

int main()
{
        Texture *texs[NUM_TEX];

        string thing = "goal_w1_b_512x512.png";

        int x;
        for (int i = 0; i < NUM_TEX; ++i)
        {
                Texture *tex = new Texture();
                if (!tex->loadFromFile(thing))
                {
                        delete tex;
                        cout << "failed to load: " << thing << "\n";

                        cin >> x;

                        return 0;
                }

                int num = tex->getSize().x * tex->getSize().y * 4 * i;
                double memD = num;
                double megs = memD / 1000000.0;
                cout << "megs: " << megs << endl;

                texs[i] = tex;
                cout << i << "\n";
        }


        for (int i = 0; i < NUM_TEX; ++i)
        {
                delete texs[i];
        }
       
        cout << "done" << endl;

        cin >> x;
       

        return 0;
}



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Failed to load image. Reason: Out of memory
« Reply #1 on: October 28, 2020, 08:03:41 am »
This is really a lot, I'm not sure that you really need all these huge textures all loaded at the same time.

Don't forget that your graphics card's resources are also used by the OS and other apps, that the amount of textures or the memory dedicated to them may be limited by the driver, that the available memory may be less than 6 GB because of fragmentation, etc.

If the driver says "out of memory", I'm pretty sure you can trust it ;)
Laurent Gomila - SFML developer