How to check CPU and RAM load?

Aronax

Client
Регистрация
29.01.2015
Сообщения
201
Благодарностей
59
Баллы
28
Hi all,

In one of my templates, I need to check the CPU and RAM load.

If CPU and / or RAM load are higher than 80-90%, I need this template to stop opening new threads (using .bat files). If these parameters are below 80-90% load, I need this template to continue opening new threads.

Thanks
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 181
Баллы
113
There you are one piece of code.
Get % of used RAM
 

Вложения

  • Спасибо
Реакции: Aronax

Aronax

Client
Регистрация
29.01.2015
Сообщения
201
Благодарностей
59
Баллы
28
Thanks a lot. It works like a charm
 

Aronax

Client
Регистрация
29.01.2015
Сообщения
201
Благодарностей
59
Баллы
28
If anybody needs it, I also found this code for getting the CPU load. You need to add System.Management in GAC references and using "System.Management;" (without "") on the "Using directives" tab in "Using directives tab and shared code" interface. Then, this code will work:

C#:
var searcher = new System.Management.ManagementObjectSearcher ("select * from Win32_PerfFormattedData_PerfOS_Processor");

var cpuUsage = searcher.Get()
    .Cast<System.Management.ManagementObject>()
    .Select(mo => new
    {
        Name = mo["Name"],
        Usage = mo["PercentProcessorTime"]
    }
    ).ToArray();

return (ulong)cpuUsage[0].Usage;
Works great for me
 
Последнее редактирование:

VladZen

Administrator
Команда форума
Регистрация
05.11.2014
Сообщения
22 226
Благодарностей
5 844
Баллы
113
Nice solution!
However, there is Resource monitor feature for that,
which does not allow to run new threads if CPU and Memory usage is too high.
Resource monitor.png
 
  • Спасибо
Реакции: Aronax

Aronax

Client
Регистрация
29.01.2015
Сообщения
201
Благодарностей
59
Баллы
28

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)