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";
	} 
}

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: Unity Script Reference – Mono Compatibility

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).

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);

}

}