-1

Bit of an unusual setup here,

  • I'm creating mods for the video game RUST
  • It uses HARMONY to load mods into the server code
  • At some point I did something to cause a memory leak, the application grows from 10GB in memory to 30GB over the course of a day with hundreds of players using it.
  • The mods are written in C#, but Unity's core is C++
  • Unloading all of the mods does not cause the GC to clean up the used memory, even when calling gc.collect manually.

I've tried hooking up traditional memory profilers like ANTS and dotMemory but they do not connect -- I'm sure that's to do with not having a release build or the fact that it's a Unity application.

I've tried attaching the Unity debugger, but it fails to recognize the game.

I'm running out of options, does anyone know if it's possible to ask the GC to dump what objects are currently being tracked? Perhaps its possible to use Harmony to hook into the actual allocation of memory step in the GC and I could track manually?

ThompsonCodes
  • 35
  • 1
  • 9

2 Answers2

1

If you download the same exact version of Unity that the game was made in (you can check what it is in the properties page of UnityPlayer.dll), you can swap UnityPlayer.dll from a development build into the game and then use Unity's memory profiling tools. That said, this will only work if the game uses a relatively recent version of Unity (made within the last 3-5 years).

Sunius
  • 2,789
  • 18
  • 30
  • Thanks for the great suggestion! Will just any UnityPlayer.dll work if a project was exported with debug build? I don't have access to the development build of the game itsself -- is UnityPlayer.dll totally separate from individual game code? – ThompsonCodes Mar 01 '23 at 22:16
  • UnityPlayer.dll is identical for all games made in Unity, given a particular version of the engine. It ships precompiled with the engine and game developers cannot modify it. – Sunius Mar 02 '23 at 05:21
  • this worked, it allowed me to use the memory profiler to debug the game. thanks so much all! – ThompsonCodes Mar 13 '23 at 03:18
0

Unity has multiple backends. It has the Mono one which is C# and it has the IL2CPP one which is C++. Also release builds on Unity, afaiu, cannot be profiled.

Since we are talking about mods, perhaps do some test on whether it's your mod, or other loaded mods, or if it happens when you have multiple or specific ones loaded, or even on their own etc.

NPatch
  • 66
  • 6
  • I can confirm the memory is piling up on the mono side of things. It's almost definitely due to the mod, but it's this monolithic beast, and disabling the mod is out of the question -- it's a full conversion game mode. Still holding out hope for something I could hook into with Harmony to track active references. I can also say that it's not a ton of game objects piling up -- at least in the scene. It was relatively easy to print out game objects. – ThompsonCodes Feb 28 '23 at 20:59
  • I just remembered, if the mods are regular C# assemblies, then perhaps you can modify them with [dnSpy](https://github.com/dnSpy/dnSpy) and add some form of tracking yourself. Assuming you don't have access to the mod source that is. – NPatch Feb 28 '23 at 21:57
  • I have access to the mod source code -- if there's some way to get that thing to print out all references and the memory its using, that would be a start! I use dnSpy to read the source code from the original game, but I'm not sure how to apply that in tracking down a memory leak that my mod is causing. – ThompsonCodes Mar 01 '23 at 22:18
  • I'd try what Sunius is suggesting. Profiler can show you some per frame allocations so you can filter some data there , or use the MemoryProfiler and look at diffs of snapshots. Both Profilers can attach to players(although MemProf only on dev/debug builds, but Sunius' advice might work). – NPatch Mar 01 '23 at 22:34