Search code examples
c#xnamedia-playermonogame

Ram usage increases when MediaPlayer switches songs (Monogame c#)


I'm making a game using the Monogame engine and I'm using MediaPlayer to play background music. When I switch whatever song is playing I can see the ram usage increase by several MB (using the 'Performance and Diagnostic Tool') even though it's a song that's been preloaded. After a lot of song changes this could become a problem. I want to make the game use as little of the computers resources as possible.

 Song song1;
 Song song2;

 protected override void LoadContent()
        {
            song1 = Content.Load<Song>("song1");
            song2 = Content.Load<Song>("song2");
         }

 void updateMusic()
        {
            if (gameState == "lvl1") { MediaPlayer.Play(song1); }
            else if (gameState == "lvl2") { MediaPlayer.Play(song2);}
        }

Solution

  • Turns out that the current version of MonoGame I was using (3.5) is bugged and has some kind of memory management issue. After switching to MonoGame version 3.4, loading and unloading works properly now.