I’m trying to get the amount of memory my game is using during a game build (at runtime). For example, on Windows I can look in the “task manager” and see that my game is currently using 1,600 MB (1.6 GB). I was wondering if there was a Unity API way to do this in code/scripting – or maybe a .net API way as well? Whatever might work.
Unity has a “Profiling.Profiler.GetTotalReservedSizeLong()” function what works (as well as other functions), however that only works if the Profiler is available. In a game build, the profiler is not available. So I can’t use those Profiler functions.
I’ve also tried using these code functions:
// get the current process
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();
// get the physical mem usage
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;
This code always returns “0” bytes when I tested it in the Editor…but I havn’t tested it in an actual game build yet.
My use-case is that when my game reaches a certain number of MB used in memory on the system, I want to run something in code.
You need to call process.Refresh() before you access anything like WorkingSet64. If you want to monitor the memory you have to call Refresh() every frame that you’re monitoring, otherwise it never updates the values of anything.
Hmmm that is weird. Google searching I found some people saying that if you’re calling process.WorkingSet64 from a 32-bit process it can give you the wrong number. Are you sure you’re using 64-bit everything? You’d need 64-bit Windows, the 64-bit version of the Unity Editor (for it to work in the editor) and be building as x86_64 Standalone (for it to work in a build). Or you could try process.WorkingSet instead of WorkingSet64 and see if that works; if you’re using under 4GB of RAM it should work.
Yes, as far as I know I’m using 64 bit everything. 64 bit Unity Editor, 64 bit windows, and it was built for x86_64 Standalone. I see an asset on the asset store that claims to be able to show the memory, so I might try that. If not, I’ll keep asking until I find an answer to this
It says the “process.WorkingSet” command is obsolete. I think I can still use it, but visual studio is telling me it’s obsolete.
If the docs say so, maybe read a little bit further
Because the Memory Profiler Module’s manual page list the counters shown in the module and the names with which you can get these via ProfilerRecorders and which of these are available in release builds.
In particular, I think you might be looking for “System Used Memory”, which is getting the app memory usage directly from OS counters.