- Home /
Measure cpu and memory load in code?
Hello,
I know you can measure and display the FPS (based on delta time) like it was described on the wiki here: http://wiki.unity3d.com/index.php?title=FramesPerSecond
(I am also aware of FPS Graph and I don't want/need to use that)
But for my game, I would like to have access to (and also print out) the cpu load and the system memory load in addition to the FPS.
Is that kind of stuff available to us in javascript/etc., and can we calculate it? If so, how could I go about doing it?
Cheers!
[EDIT]
I can't seem to be able to use anything under System.Diagnostics in unity c#. Anything I try results in a "The type or namespace name PerformanceCounterCategory' does not exist in the namespace
System.Diagnostics'. Are you missing an assembly reference?"
Here's what I tried:
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class CPUMemTest {
//PerformanceCounter cpuCounter;
//PerformanceCounter ramCounter;
System.Diagnostics.PerformanceCounter cpuCounter;
System.Diagnostics.PerformanceCounter ramCounter;
void Start() {
System.Diagnostics.PerformanceCounterCategory.Exists("PerformanceCounter");
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
}
void Update(){
Debug.Log("> cpu: "+getCurrentCpuUsage()+"; >ram: "+getAvailableRAM());
}
public string getCurrentCpuUsage(){
return cpuCounter.NextValue()+"%";
}
public string getAvailableRAM(){
return ramCounter.NextValue()+"MB";
}
}
However, it appears that this System.Diagnostics approach (if it works) only works under Windows. Which is not good if you want to make a cross platform game.
Answer by whydoidoit · Aug 23, 2013 at 08:16 AM
In order to use PerformanceCounter you have to include the complete .NET 2.0 libraries (not the subset).
See here for compatibility information with the various options: http://docs.unity3d.com/410/Documentation/ScriptReference/MonoCompatibility.html
However, I'm pretty sure I won't have access to these if I target the webplayer and/or flash player. I don't think fancy .net stuff is included in those builds.
You are right, according to that link I provided it is not available in Web builds.
@whydoidoit: Thank you for yor quick reply. I am new to unity. I installed .net 4.5 but no use.. I am using unity 4.0 32bit in win 7 64bit platform. $$anonymous$$y monoDevelop shows System.Diagnostics.PerformanceCounter in available. but when I run it in unity in says the exception "CS0234: The type or namespace name PerformanceCounter' does not exist in the namespaceSystem.Diagnostics'. Are you missing an assembly reference?"
Do you have any idea on this? Thanks in advance
Your player settings have .NET 2.0 selected (not subset)?
Hi there, would also like a similar kind of Performance counter, I tried to run your code but all I get is CPU 100% and RA$$anonymous$$ 0$$anonymous$$B :( Is there something I'm doing wrong here?
using UnityEngine;
using System.Collections;
using System.Diagnostics;
public class ProfilingScript : $$anonymous$$onoBehaviour
{
PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
void Start()
{
cpuCounter = new PerformanceCounter();
cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";
ramCounter = new PerformanceCounter("$$anonymous$$emory", "Available $$anonymous$$Bytes");
}
void Update()
{
print(getCurrentCpuUsage());
print(getAvailableRA$$anonymous$$());
}
public string getCurrentCpuUsage(){
return cpuCounter.NextValue()+"%";
}
public string getAvailableRA$$anonymous$$(){
return ramCounter.NextValue()+"$$anonymous$$B";
}
}
Answer by Jamora · Aug 02, 2013 at 10:01 PM
Seems like our friends at StackOverflow have already found an answer; check here. It should work with UnityScript as well.
Basically you use The System.Diagnostics namespace and the ProgramCounter to access the same data as the Windows Performance Monitor (perfmon.exe).
Hmm I wonder if that works in Unity's C#. And I wonder how this behaves on different platforms; different operating systems as well as when you export to flash.
I'll have to give it a try.
Here it did not work. I've read many topics on the internet and nothing.
Ditto, I keep seeing people posting that this should work but all I can get it to do is display a CPU value of 100% and a ram value of 0$$anonymous$$B I'm trying it on a $$anonymous$$ac and that's the results I get... does this even really work??
Answer by ErwinJB · Aug 22, 2017 at 03:39 AM
How fix the problem 100% cpu usage and 0 MB ram from this code?
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Management; using System.Diagnostics; public class Measure : MonoBehaviour { private PerformanceCounter cpuCounter; private PerformanceCounter ramCounter;
void Start()
{
InitialiseCPUCounter();
InitializeRAMCounter();
}
void Update()
{
print( "CPU Usage: " +System.Convert.ToInt32(cpuCounter.NextValue()).ToString() +"%");
print( System.Convert.ToInt32(ramCounter.NextValue()).ToString()+"Mb");
}
private void InitialiseCPUCounter()
{
cpuCounter = new PerformanceCounter(
"Processor",
"% Processor Time",
"_Total",
true
);
}
private void InitializeRAMCounter()
{
ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);
}
}
@ErwinJB You know this does not work right? Well I tried this and the first error I get is this:
Assets/$$anonymous$$easure.cs(4,14): error CS0234: The type or namespace name `$$anonymous$$anagement' does not exist in the namespace `System'. Are you missing an assembly reference?
If I remove that from the document: CPU Usage: 0% UnityEngine.$$anonymous$$onoBehaviour:print(Object) $$anonymous$$easure:Update() (at Assets/$$anonymous$$easure.cs:22)
0$$anonymous$$b
UnityEngine.$$anonymous$$onoBehaviour:print(Object)
$$anonymous$$easure:Update() (at Assets/$$anonymous$$easure.cs:23)
CPU Usage: 100%
UnityEngine.$$anonymous$$onoBehaviour:print(Object)
$$anonymous$$easure:Update() (at Assets/$$anonymous$$easure.cs:22)
That's what reads out in the console.
Your answer
Follow this Question
Related Questions
What's better? Managers using DontDestroyOnLoad or keep one in each scene? 1 Answer
inactive object or unseen object 1 Answer
How do inactive game objects affect CPU and memory? 1 Answer
CPU and memory usage between a Unity projet in Web Player and as a standalone 0 Answers
iphone variable values reset on application focus!? 2 Answers