Serana, on 08 December 2021 - 02:11 PM, said:
GPU memory is generally used by textures. Do you think this memory "leak" is due to textures remaining in VRAM?
Textures, vertex buffers, and index buffers come to mind but there might be others.
Serana, on 08 December 2021 - 02:11 PM, said:
On StackOverflow, I have read that textures loaded manually are not removed from VRAM automatically if we don't use the content manager of XNA/MonoGame.
We have to call the Dispose function manually and I don't think we are currently calling this function. (See
https://stackoverflow.com/a/48777516)
It would be quite surprising to me if we
had to call "Dispose()" ourselves, since textures (and other graphics) device objects are all "IDisposable" and have what appears to be a correctly-implemented C# destructor.
I can see three possible reasons why we might be leaking graphical resources:
- Open Rails is incorrectly keeping a reference to the object somewhere
- MonoGame is incorrectly keeping a reference to the object somewhere
- The runtime/CLR is unable to keep up with the rate at which "IDisposable" objects are being created/let go
Serana, on 13 December 2021 - 10:52 AM, said:
1) Reduce the GC pressure as much as possible:
- We have to avoid throwing away variables and reuse them as much as possible.
The HUD DEBUG page includes a display of how much managed memory is allocated per frame, and we should always be trying to minimise this, but I do not want the code to become unreadable either. :)
Serana, on 13 December 2021 - 10:52 AM, said:
2) Be careful about string usage:
- Each time you modify a string, you are creating a new object because strings are immutable.
- When creating strings with multiple operations, StringBuilder must be used.
Strings could be a problem, but it probably makes more sense to profile what's being allocated before trying to target anything.
Serana, on 13 December 2021 - 10:52 AM, said:
3) Try to find leaks in the unmanaged momory:
- Mainly GPU RAM and when we are using libraries that were coded in C/C++
For me,
the most important thing to figure out here is
why the textures are not being cleaned up automatically, because they should be - and they have in the past. For example, there's no need to start a war on GC garbage if the cause is Open Rails or MonoGame keeping unexpected extra references to the textures. :)
One thing that this thread and
another recent memory usage thread have shown, is that there can be significant confusion over memory measurements and what they are actually measuring. I am therefore working on an update to the memory reporting in the HUD DEBUG screen that will include:
- DRAM - private, working set, and virtual breakdown
- VRAM - dedicated, shared, and committed breakdown
This will add a lot of new measurements, reducing the need for 3rd party tools (this will be the first time we can measure VRAM usage) and - as a result - reducing the possibility for confusion when comparing between people.