SoundEffectInstance - Memory consumption

Hey,

I am working on my own game using MonoGame v. 3.6.
Currently, I am implementing the sound of my game, where I am using SoundEffectInstances to play some background music.
I have read over some topics and have seen a response by @nkast.
There, he has created a loop using the LoadContent() and Unload()-methods of the content manager and created some SoundEffectInstances. The observation was that the memory consumption was increasing steadily.
In my current work, I share these observations.

My question is:
What can I do to minimize the memory consumption created by the SoundEffectInstances?

I have thought of a pool of SoundEffectInstances and reusing the already stopped ones… but are there other ideas or way to do so?
Or have I completely missed something?

Thank you in advance!

Can you link the topic you’re talking about? I don’t know about this issue. MonoGame already uses a pool for sound effect instances internally.

1 Like

Hm… unfortunately I cannot find the topic anymore (I found it 2-3 hours ago and the topic was not directly related to SoundEffectInstance).
However, I think that I have found the issue.
My code is the following:

while (true)
            {
                ContentManager localContentManager = new ContentManager(this.Content.ServiceProvider, this.Content.RootDirectory);
                SoundEffect se = localContentManager.Load<SoundEffect>("someSound");
                SoundEffectInstance sei = se.CreateInstance();
                localContentManager.Unload();
            }

I am using different contentManager for different screens. However, switching between screens does also create new contentManager, even if the screen was used before.
Should I reuse the contentManager in this case?
My intention is to use as less memory as possible.

I don’t know what that code is trying to achieve. The sound effects will be immediately destroyed after loading when the ContentManager is unloaded and disposed.

SoundEffect is not intended for background music as the entire sound is loaded entirely into memory. For background music with minimal memory, use MediaPlayer and Song.

1 Like

Hey,

Hm yes. After enabling the garbage collection, my example does not work.
Strange… maybe I have some other issues regarding references to the created SoundEffectInstances.
Creating new contentManagers doesn’t seem to be the problem.

The reason why I have used SoundEffect for the background music is, that I wanted to fade in/out when music is switched.
As I see, this can also be done using the MediaPlayer.
Thank you for your advice. I will use the MediaPlayer and Song for background music and hope that I will not face this issue anymore when I add sound effects.

After some further debugging, I was able to find the bug in my code.
Unfortunately, I have overseen it until now…
The memory consumption is now as it should be.

Thank you for your help!

2 Likes

I had an old class to play small loops, basically something you would do with XACT. Back then XACT was not supported on WP7 and later they dropped XNA altogether.

@CKalt, Noways MG has full support of XACT, perhaps you should give it a try since it can do much more, (although I am not an expert on it).

Thank you for the advice.
I will definitely look into it.
Since my sound stuff is in the very beginning, I could easily switch to XACT.

I think the Dispose() method can be used, when you know the end of the sound, and you want to manually save it from memory. Earlier, in SoundEffectIstance, I was able to prevent the memory from overflowing.