Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Tudor · Aug 02, 2013 at 09:46 PM · memorycpu

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


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tudor · Aug 03, 2013 at 07:59 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tudor · Aug 23, 2013 at 08:46 AM 0
Share

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.

avatar image whydoidoit · Aug 23, 2013 at 09:05 AM 0
Share

You are right, according to that link I provided it is not available in Web builds.

avatar image Zufry · Aug 23, 2013 at 09:39 AM 0
Share

@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

avatar image whydoidoit · Aug 23, 2013 at 10:05 AM 0
Share

Your player settings have .NET 2.0 selected (not subset)?

alt text

screen shot 2013-08-23 at 11.04.13.png (49.9 kB)
avatar image Exalia · Sep 03, 2013 at 10:57 AM 1
Share

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";
     } 
 }
Show more comments
avatar image
1

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tudor · Aug 03, 2013 at 07:20 PM 0
Share

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.

avatar image leodluz · Oct 19, 2016 at 11:39 PM 1
Share

Here it did not work. I've read many topics on the internet and nothing.

avatar image KnightRiderGuy leodluz · Jul 29, 2017 at 02:57 AM 1
Share

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??

avatar image
1

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

 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Aug 22, 2017 at 12:38 PM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges