C # How to completely remove an object from memory

I have a C # application consisting of Pivot with several Pivotitems. Regular Pivotitems are unloaded properly and do not leak memory. However, on two elements, I have Drawingsurfaces on which I use Monogame to render 3d models. After unloading and loading their anchor points, I try to completely destroy and then completely recreate instances of the Game (mainly because Monogame does not allow you to draw on two surfaces at the moment and because they use a lot of memory, and there is not enough both of them).

My problem is that when I delete the Xamlgame created at boot, it does not free up all the memory used. This means that every time I recreate Xamlgame, it just starts to use more and more memory until there is more left. So I would like to know if there is any C # way to completely get rid of all the memory that was used when loading the object.

Any particular Monogame method will also be appreciated. At the moment, I first manage my vertex and index buffer, then I clear the vertex and index lists, then I delete my Basiceffect and my Graphicsdevicemanager, then I call dispose on the "game" itself, and finally I make a variable that contains " game "equal to zero.

PS. If you have ever done this in XNA, you could also help me because the syntax is basically the same.

EDIT (may be a MonoGame error):

Now I managed to clear all the buffers, etc. However, there is still a memory leak on my BasicEffect. I tried to get rid of it and make it null, but regardless of the fact that it uses more and more memory all the time every time I reload my axis.

+6
source share
3 answers

Obviously, MonoGame simply does not support creating more than once in the game and there will be a memory leak if this operation is performed. It is somewhat more difficult to use only one game instance and change the panel on which it draws.

-2
source

Use the garbage collector and wait for it to complete.

GC.Collect(); GC.WaitForPendingFinalizers(); 
+6
source

First you need to set it to null so that it is no longer used:

  XamlGame = null 

Then you can activate the garbage collector:

  GC.Collect(); GC.WaitForPendingFinalizers(); 

If it is not deleted, either the XamlGame object is not null, or the reference to it is still held by another object.

Keep in mind that if XamlGame inherits from the Game class, you cannot destroy it, because it contains a run loop that supports the entire game application. If you delete it, the application will crash.

+4
source

Source: https://habr.com/ru/post/948874/


All Articles