Get application CPU/RAM usage from device

Hi everyone :slight_smile:
I would like to have a debug screen in my application that shows the actual CPU/RAM usage (on the actual iOS/Android device) of the application.
Is it possible to do so using unity or do I have to write native iOS/Android plugins?

Thanks :wink:

I did not found something like that. For Android I currently use this link: cpu usage - Android cpu cores reported in /proc/stat - Stack Overflow
One can calculate the cpu usage using the data in /proc/stat file:

private long lastWork;
        private long lastTotal;

        private float readUsage()
        {
            // http://stackoverflow.com/questions/22405403/android-cpu-cores-reported-in-proc-stat
            try
            {

                string loadString = File.ReadAllLines("/proc/stat")[0];
                //Log.Debug("First line: %s", loadString);
                string[] toks = loadString.Replace("  ", " ").Split(' ');

                long work = long.Parse(toks[1]) + long.Parse(toks[2]) + long.Parse(toks[3]);
                long total = long.Parse(toks[1]) + long.Parse(toks[2]) + long.Parse(toks[3]) +
                        long.Parse(toks[4]) + long.Parse(toks[5])
                      + long.Parse(toks[6]) + long.Parse(toks[7]) + long.Parse(toks[8]);

                float load = (float)(work - lastWork) / ((total - lastTotal));

                if (work < 0 || total < 0 || load < 0) Log.Error("ERR Something is negative: %s %s, %s %s, %s, %s", work, lastWork, total, lastTotal, load, toks);

                lastWork = work;
                lastTotal = total;

                return load;
            }
            catch (IOException ex)
            {
                Log.Error("Error while calculating cpu usage. %s", ex);
                return -1;
            }
        }

It always uses the last reading as input for the new. So the first call will result in garbage :wink: Also you should call it in an interval, as stated in the link provided

What about using the ‘top’ command in android?

And what about memory in android by the way?

in file /proc/meminfo you get
MemTotal: 7998244 kB
MemFree: 6840272 kB
but i wish there was some better way to receive all this data. How this works on ios i do not know. I guess you could use top command aswell, but I went for the other way

That is great to get the overall ram usage, but I need my application’s Ram usage. From what I searched, it seems to be a fairly complex with no clear answer (at least to me).
And thx for your help btw:)

Hm, yeah maybe you can use top then, it tells you cpu% and memory i guess
I tried it now:

string bundleIdentifier = Application.bundleIdentifier;
  Process process = new Process();
  process.StartInfo.FileName = "sh";
  process.StartInfo.Arguments = "-c top | grep " + bundleIdentifier;
  process.StartInfo.UseShellExecute = false;
  process.StartInfo.RedirectStandardOutput = true;
  process.StartInfo.RedirectStandardError = true;
  process.OutputDataReceived += (sender, args) =>
  { // this is not executed in main thread, so dont use unity functions
  if (args.Data.Contains(bundleIdentifier))
  {
  Log.Debug("Stats: %s", args.Data);
  }
  Log.Debug("received output: %s", args.Data);
  };
  process.ErrorDataReceived += (sender, args) => Log.Debug("Error output: %s", args.Data);
  process.Start();
  process.BeginOutputReadLine();
  process.BeginErrorReadLine();

  if (process.HasExited)
  Log.Error("Process has already exited.");

This works, but my grep command does not work, so i filter again in data received. And you need to close/kill process when app closes, or it will still run

Nice :slight_smile:
Thanks