3
\$\begingroup\$

I am making a level selector, which works like a web slider. I am showing 12 levels per page, and I have a total number of 60, which means 5 pages of 12 levels.

Each level has his on background image, and I tried to load all the backgrounds into an Texture2D array. Since now I loaded only 12 backgrounds, because I had only them, everything worked perfectly. Now, I have more, about 40, and when I try to load them into the array I receive this error:

Additional information: HRESULT: [0x8007000E], Module: [General], ApiCode: [E_OUTOFMEMORY/Out of memory], Message: Not enough storage is available to complete this operation.

Code line:

for (int i = 1; i <= 36; i++)
                LevelBackgroundTextures[i] = Resource.ContentManager.Load<Texture2D>("Levels/Backgrounds/bg_" + i);

If I understood correctly I receive this error because I do not have enough space (RAM) to load all the content once and I need to use multiple content processors, and here is the problem.

I do not know how can I use them to be effective, when to load/unload the content, etc. Or is there another way that I can fix this error?

Thanks.

\$\endgroup\$
2
\$\begingroup\$

Yep, you're out of memory. Loading all of those textures (which I assume are going to be very large since they're backgrounds) is going to eat through your RAM pretty quickly. There are a couple different ways you could solve this.

  1. Make sure no unnecessary values are floating around. It might just be that you have other data that you're not using that's in memory. This is less of a solution and more of a quick fix. You're still loading a lot of data into memory and if you move to a device with less memory you'll hit the same problem.

  2. Load smaller backgrounds. It won't look as nice but if you stretch the textures a bit you'll save memory. This also doesn't perfectly solve the problem. If you want more backgrounds in the future you'll still probably run into the same problem again and then have to downgrade the quality further. That's a pain.

  3. Don't load all of your backgrounds at once. When you move to the next page, add a transition like a fade where you, in a separate thread, load all of the background images. Then as you fade back in, unload the previous page's textures. That way you have 24 textures loaded at most.

  4. Have fewer levels per page. This one may not be the most ideal but it will work. Heck if you lower it to like, 6 or 8 per page you can do something where you have the displayed page loaded, and the next and previous pages loaded as well. That's 18-24 textures loaded at once. Then when you move to the next page you don't have to load anything to display the page, but you can unload the page that's no longer 'adjacent' and load the next 'adjacent' page.

The solution that works best for you might be some combination of the solutions I've proposed, or even none of them! With problems like this there isn't always a #1 top solution so pick one that fits your project the best.

\$\endgroup\$
1
  • \$\begingroup\$ Ok, thank you very much for your ideas. I have already tought partially to one solution. xD \$\endgroup\$
    – Edvin
    Jul 8 '15 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.