How do i get around Mouse.GetState generating garbage?

mousestate = Mouse.GetState(Window); // with or without the window as a parameter.

The above line all alone generates about 1 megabyte of garbage every minute when i run with is timestep off.

Granted this is basically a empty project but im trying to eliminate garbage collection in tuning up my ui class to allow it to be a standalone project. This is vexing because i constantly see it dropping about 8kb like a hearbeat even when i run at 60 fps.

Does anyone know of a way to prevent it from creating garbage ? Or a alternate way to poll the mouse.

Ive been tracking garbage for month in my game and now you say it, there is garbage in the mouse. Getstate method.
At least in the mouse.windows.cs when doing a setposition call. A var and a new point is created each time it is called.
Have to go to work ill check the other methods later

So guys, this garbage is collected though, right? I mean… I feel like I would have noticed if my RAM use kept escalating, but I haven’t actually checked, and I DO have all those modern gigs of ram for garbage to pile up on.

Sure it may be unnoticeable, but calling GC takes some cycles and makes the app slowdown/hand/stutter in certain cases.
For instance, the mouse is not a big issue for me but as i set its position every frame to be at middle screen, it WILL create unwanted garbage.
I don’t have garbage with GetState though. A blank project with just this methods says nothing special about a memory leak

A variable is allocated on the stack. A Point is a struct, and again it is allocated on the stack. These do not allocate memory from the heap, and it is heap memory that is tracked by the garbage collector.

This is why it is important to declare which platform you are using. Mouse.GetState() in MonoGame.Framework.Windows does not generate garbage. Mouse.GetState() in MonoGame.Framework.DesktopGL does generate garbage.

This is why it is important to declare which platform you are using.
Mouse.GetState() in MonoGame.Framework.Windows does not generate
garbage. Mouse.GetState() in MonoGame.Framework.DesktopGL does generate
garbage.

Great arrg, Im using the cross platform one so ya basically the desktopGL on windows i guess, i should of said that…

Ya its creating garbage i add it and see it make garbage, remove it and it stops the real problem is i can’t get that garbage to stop being collected or reuse it.

Keyboard does it too when a key is actually pressed, but i haven’t isolated the call from my keyboard class to test that its not just my calls. I can live with garbage only when i press a key even if it is. The mouse is just doing it having that call in there which is constant.

So the dx version doesn’t generate garbage then ?

Seems odd that a call like this would generate garbage anyways unless somethings getting boxed that shouldn’t be.

Is there a work around other then switching to DX to prevent GetState on GL from creating garbage and collecting it ?

You got it. It is using the Enum.HasFlag extension method which does some boxing internally. We have a PR (#5356) up already to fix this.

It generates garbage, but it is quickly and efficiently cleaned up the garbage collector. It doesn’t grow during the life of the application.

1 Like

Is that pr already been accepted ? Just dl the newest build then ?

Ya i don’t mind start up garbage i don’t even start tracking it for about 10 seconds till my app fully loads.

I have just merged it now. The next build will have it.

FYI the directX version also generates garbage. It’s the most garbage generated in my application right now, one can see the gc generate a lot when moving, it’s almost static when keeping still. Not explicitly getstate() though.

Just interesting to see

1 Like

Unfortunately it’s all being allocated in System.Windows.Forms which is out of our control.

I have a fix for MouseState garbage here (6168).

1 Like

The Gl version seems to no longer generate garbage very nice.
The dx version is still slipping a little, only when you move though.

The test files are here if you would like to reproduce it.

I’m interested by this too

New PR https://github.com/MonoGame/MonoGame/pull/6273

WinForms still allocates a few bytes on each message. I don’t think it’s possible to eliminate all of them.