1
\$\begingroup\$

I'm having a huge problem with memory accumulation in all my games. This happened after switching from monogame 3.4 to 3.5 so I switched back. Then recently I went to 3.6(latest release) and started having the same problem again. I would switch back again but I need 3.6 rather than 3.4 for certain reasons.

I've tried both .Dispose() and .Unload() for the content but it seems to only release a small portion of what was previously loaded.

Here is the code

public void UnloadContent()
        {
            if (overworldContent != null)
            {
                overworldContent.Unload();
            }
        }

I've also done

public void UnloadContent()
        {
            if (overworldContent != null)
            {
                overworldContent.Dispose();
            }
        }

This is a picture of the memory usage from just going in and out of an area just a few times over the course of several minutes. A player could easily have multiple gigabytes taken up from playing for even just one hour if it goes from 86MB to 235MB in just 2 minutes

enter image description here

I seriously need help.

\$\endgroup\$
  • \$\begingroup\$ I think UnloadContent only gets called at the end of the game if this is the method from the main class. You need to dispose of content elsewhere too if you don't want it to increase constantly. \$\endgroup\$ – Bálint Nov 21 '17 at 19:20
  • \$\begingroup\$ Unload is in fact being called and every area in the game has it's own content manager that I'm utilizing. \$\endgroup\$ – Brent Nov 21 '17 at 20:13
  • \$\begingroup\$ This is quite a tricky question to answer. You'll probably need to do some reading on what causes memory issues in C# and consider using a memory profiler \$\endgroup\$ – craftworkgames Nov 22 '17 at 0:53
  • \$\begingroup\$ is the only change moving to another version, or are there related code changes? for debugging, you can try GC.Collect () after you unload/dispose to see if the memory clears. if not, then the objects likely still have references somewhere. \$\endgroup\$ – Joel Nov 22 '17 at 8:43
2
\$\begingroup\$

Thanks for your suggestions everyone, I think I've figured it out. Instead of using just Unload() or Dispose() using both at once seems to help a great deal. After it accumulates a certain amount (around 250MB) it clears over 100MB all at once. I've also noticed that if I also set the ContentManager to null it will clear the memory even faster. My code looks like this now.

        if (overworldContent != null)
        {
            overworldContent.Unload();
            overworldContent.Dispose();
            overworldContent = null;
        }

Apparently after using Unload() it will still keep some junk in a scratchbuffer (whatever that is) and using the other method along with nullifying the ContentManager for that class will ensure that the rest gets cleared out.

\$\endgroup\$

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.