Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

My game in C# : How to release unused memory?

$
0
0

Hello Ladies and Boys from MSDN

I m making a videogame with Visual C# 2008 and SFML. It works Ok, but at a certain time, it runs out of memory. I already read some threads about the GC (Garbage Collector) here, and tried to do what was recommended there, but there is an object (GameLevel) referenced by the same identifier at each loop, that is allocated with the "new" operator, that, although I set it to null, and the call GC.Collect(), it seems the memory is not releasesd, as tells me the GC.GetTotalMemory().

Here I give you the code where my problem can be seen:

class GameMatch{

    public void RunGame(){

        int ini, lev, a;

        string[] levels = new string[]{ "LEVEL 1-1", .... };

        GameLevel level, bonus;

        LoadGameData();

        while (true){

            Introduction();

            ini = StartingOptions();

            if (ini == 0){         // start game

                player = new Player(); 

                for (a=0; a<levels.Length; ){

                    level = null; bonus = null;

                    GC.Collect();       // I think it does not work fine, or the previous level used memory is not released

                    level = new GameLevel(levels[a], player);   // level constructor: this takes a lot of memory

                    if (level.bonuslevel != null)

                        bonus = new GameLevel(level.bonuslevel, player);  // bonus sublevel if there is

                        if ((lev = level.RunGameLevel(player, bonus)) == 1) a++;  // level passed

                        else if (lev == 2) break;  // game was reset 

                        // if you lose a life in the level, then continue, repeat the level

                }

            }

            else break;  // quit application

        }

    }

}





I also give you the link to the game:

(PS I hope I ll be able to share it soon, by now the website didn t allow, until it verifies my account, I oue you)




What I suposse is happening is that all the memory the GameLevel object, referenced by the local variable 'level' is not released in each loop, weather you pass or not the level. I can t understand why, cos as I read in other post from MSDN, if there are no more references to an object, or they are set to null, the GC should release that memory.

Please, if someone knows what I m doing wrong, tell me. My game works Ok, but crashes cos of "Out of memory". You can see it yourselves if you want.

Thank you very much.

Pablo

Bs As Argentina

Note: I m not native English speaker, please forgive if there are errors.


Viewing all articles
Browse latest Browse all 31927

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>