GC ignore SFML objects
VeryTastyMan opened this issue · comments
Try calling T.Dispose() at the end of the function and see if it reduces memory usage.
I've encountered this before and I think I know why this happens. The GC doesn't run until a certain amount of memory is used. The problem is that it only knows about the managed memory. The Texture class itself is extremely small, but it loads the image in c++ (and thus in unmanaged memory) which the GC doesn't know about.
Eventually the GC will dispose the textures which in turn does free the unmanaged memory, but by that time you probably already created enough textures to fill your entire memory.
I think there is a way to let the GC know about the size of the unmanaged memory, SFML.Net should probably use that internally which should fix the issue if my theory is correct.
I've already tried calling Dispose() - it's work, but I thought it wasn't quite the right way. It seems there is no other solution. However, thanks for the answer.
Most SFML.Net classes use internally ObjectBase
which implements IDisposable
for the unmanage memory that they allocate from CSFML that is used internally.
As such, all the class instantiation should really utilize using
(e.g. using (var texture = new Texture())
), that way dispose is called at the end of using it, regardless whether an exception is thrown.
This is true for any and all objects that implement IDisposable
unrelated to SFML.
https://docs.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/using-objects