CPU Usage and Available Ram in Unity 5

CPU Usage and Available Ram in Unity 5

In Unity 5 I would like to be able to print out the CPU Usage and Available Ram left in MB. I followed the code posted on http://zamov.online.fr/EXHTML/CSharp/CSharp_927308.html and used it, however my CPU usage is always 100 and available ram is 0. According to the windows task manager CPU usage should be around 15-20% and available memory is around 4700MB. This is my first time using performance counters and further searching gives me the same code as below (from link):
public class ClientValues : MonoBehaviour {

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;
public float myCPU;
public float myRAM;

// Use this for initialization
void Start () {

    cpuCounter = new PerformanceCounter();

    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    ramCounter = new PerformanceCounter("Memory", "Available MBytes");

// Update is called once per frame
void Update () 
    myCPU = getCurrentCpuUsage ();
    myRAM = getAvailableRAM ();
    //UnityEngine.Debug.Log (myCPU);

public float getCurrentCpuUsage(){
    return cpuCounter.NextValue();

public float getAvailableRAM(){
    return ramCounter.NextValue();

What needs to change for this to give me the correct values?


Answer 1:

On Unity 5, you may use Profiler window