Manage memory in MonoGame

Hi! On begin, i want greet all :wink: Just now, I sorry for my english - i learn it.
I’m writing to you, because I have problem with manage memory in MonoGame. I’m beginner programmer and I know MonoGame since one week ;P.
Going to the case. I have my project, what I divided on classes. I have one MainClassGame, and other classes, what are scenes. This look like this:
MainClass:

public class MainGame : Game
{
protected override void Initialize()
{
}
protected override void LoadContent()
{
}
protected override void UnloadContent()
{

    }

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
if (MenuState.IsShowMainMenuScene)
mainMenuScene.Update();
if (MenuState.IsShowAboutScene)
aboutScene.Update();
if (MenuState.IsShowSettingsScene)
settingsScene.Update();
//
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
if (MenuState.IsShowMainMenuScene)
mainMenuScene.Draw(spriteBatch, gameTime);
if (MenuState.IsShowAboutScene)
aboutScene.Draw(spriteBatch, gameTime);
if (MenuState.IsShowSettingsScene)
settingsScene.Draw(spriteBatch, gameTime);
if(MenuState.IsShowGameScene)
//
base.Draw(gameTime);
}
}

And in other class, it’s methods Update and Draw.
This is:

public class SettingsScene : DrawableGameComponent
{
Game game;
ContentManager Content;

    public SettingsScene(Game game) : base(game)
    {
        this.game = game;
        LoadContent();
    }
    protected override void LoadContent()
    {
        Content = new ContentManager(game.Services);
        Content.RootDirectory = game.Content.RootDirectory;          
    }
    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin();
        //
        spriteBatch.End();
    }
    public void Update()
    {
        
    }
}

I find solved my problem with destruct unusable classes and unload unusable content:
I write destructor in “other class”:

~MyScene(){
Content.Unload();
}

And call it, in my MainClass:

if (MenuState.IsShowSettingsScene)
{
if(settingsScene !=null)
settingsScene.Update();
else{
settingsScene = new SettingsScene(this);
}
}
else
{
settingsScene = null;
GC.Collect();
}

This solved, called my destructor in “other class” and unload my content, but usable memory is for all time does not change.

My question:

  1. How to do unload my content, when i don’t use my “other class” ?

[EDIT Where are my manners, HI!, also did not realise how much of a bump this would be…]

Are you using unmanaged resources?

If you are you could look at expanding into the Dispose() Method

https://msdn.microsoft.com/en-us/library/fs2xkftw(v=vs.110).aspx

Looks like if this is File related, it may help…

Hope it helps, if not you learnt a useful C# feature :slight_smile: Win Win situations :slight_smile:

Valentine