Monogame 3.71 garbage issues

Hi,

I just noticed that Monogame 3.71 creates about 8k garbage every couple of seconds for a windows game, I created an empty project and removed pretty much everything, not even drawing nor updating anything, and still I get garbage. Even removed the gamepad from the update and still get garbage. I set also:

        graphics.SynchronizeWithVerticalRetrace = false;
        IsFixedTimeStep = false;

and looks like garbage gets created faster. Is this a bug? I was trying to port my game from XNA , which generates almost no garbage but in Monogame I get a lot of garbage and I haven’t tried any previous versions. Anyone can help?

Hmm, a lot of work has been done to eliminate allocations, so not sure where allocations still happen. A memory profiler should be able to tell.

Since garbage is created each frame, it makes sense that unlocking the frame rate generates garbage faster.

Im guessing you mean garbage collections not just garbage.

Quite a few things can generate garbage collects still.

If you call .ToString() on any number.
Even to output information to the console or screen like for fps or gc memory it will generate garbage.

if you have something like this in your code it will generate garbge.

    KeyboardState kb = new KeyboardState();
    protected override void Update(GameTime gameTime)
    {
        // test
        kb = Keyboard.GetState();
        Keys[] keys = kb.GetPressedKeys();  // the array of struct keys goes out of scope and gets collected 
        base.Update(gameTime);
    }

or text input.

I have no code in Update nor in Draw, I only check the garbage collector in draw with a variable declared in the program, so I keep reusing it, there is no code at all anywhere else, but every few frames I see the garbage accumulate.

    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 
        //base.Update( gameTime );
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw( GameTime gameTime ) {
        //GraphicsDevice.Clear( Color.CornflowerBlue );

        f = GC.GetTotalMemory( false );

        // TODO: Add your drawing code here 
        //base.Draw( gameTime );
    }

This is what I have, I only check the value of “f” every couple of seconds, and it grows by around 8k, and as you can see I don’t do anything else.

I tested this on a Windows project on the most recent develop branch. Are you getting something like this?

Looking further into that hashtable enumerator shows this. I can’t seem to get much more information, but it looks like it may be caused by native WinForms code.

If I click and hold the game window, garbage is created from MouseEventArgs.

Update: I did a better test and didn’t touch the game window at all after running it.

There are allocations in the first ~3 seconds, but I presume that’s for initializing everything. After that, there were no allocations for the remaining 25 seconds I tested.

Can you try a build of the latest develop branch and see if the issue persists?

2 Likes

Thanks for looking into this @Kimimaru :slight_smile:

1 Like

I ran the diagnostic to check allocations , this is what i got, not same as yours, main allocation comes as expected in the beginning but from the 6th second (from the period I highlighted) till end there is a small allocation coming from other sources. Maybe I should uninstall monogame 3.7.1 and install it again?, I installed it just 2 weeks ago, and it was a brand new install. The small allocations start after around 2.32 seconds (after the big allocation in the beginning)

as mentioned before I don’t have any code in the sample empty project.

I tried a few more things and I started to get allocations for mouse and windows, it looks like it is something that windows is doing and doesn’t looks like is Monogame doing it, including the snap I put in this message