PHP gets real-time CPU memory usage in Windows environment

Source: Internet
Author: User
This article mainly introduces PHP in the Windows environment to get CPU memory real-time usage of relevant data, very good, with reference and learning PHP value, interested in PHP friends can refer to this article

Background real-time monitoring of the server's Cup and memory usage of the scene is very common, although not done, but before the handwriting code I did not expect to spend 2 hours to finally realize.

Although the Internet search PHP CUP memory usage of this kind of keywords come out of a lot of articles, but mostly in the Linux environment, under Windows only put forward by the CMD statement implementation of the point of view, but few practical directly feasible code, and tried again wmic, systeminfo A variety of not familiar with the cmd command, and did not find a viable solution, and finally through the Baidu know an answer to find the implementation using VBS, slightly improved, can be used directly in the Windows environment.

Class Code

class SystemInfoWindows
 / **
  * Judge whether the specified file exists in the specified path, if not, create it
  * @param string $ fileName file name
  * @param string $ content file content
  * @return string return file path
  * /
 private function getFilePath ($ fileName, $ content)
  $ path = dirname (FILE). "\\ $ fileName";
  if (! file_exists ($ path)) {
   file_put_contents ($ path, $ content);
  return $ path;
 / **
  * Get CPU usage vbs file generation function
  * @return string return vbs file path
  * /
 private function getCupUsageVbsPath ()
  return $ this-> getFilePath (
   "On Error Resume Next
    Set objProc = GetObject (\ "winmgmts: \\\\. \\ root \ cimv2: win32_processor = 'cpu0' \")
    WScript.Echo (objProc.LoadPercentage) "
 / **
  * Obtain total memory and available physical memory JSON vbs file generation function
  * @return string return vbs file path
  * /
 private function getMemoryUsageVbsPath ()
  return $ this-> getFilePath (
   "On Error Resume Next
    Set objWMI = GetObject (\ "winmgmts: \\\\. \\ root \ cimv2 \")
    Set colOS = objWMI.InstancesOf (\ "Win32_OperatingSystem \")
    For Each objOS in colOS
     Wscript.Echo (\ "{\" \ "TotalVisibleMemorySize \" \ ": \" & objOS.TotalVisibleMemorySize & \ ", \" \ "FreePhysicalMemory \" \ ": \" & objOS.FreePhysicalMemory & \ "} \")
    Next "
 / **
  * Get CPU usage
  * @return Number
  * /
 public function getCpuUsage ()
  $ path = $ this-> getCupUsageVbsPath ();
  exec ("cscript -nologo $ path", $ usage);
  return $ usage [0];
 / **
  * Get memory usage array
  * @return array
  * /
 public function getMemoryUsage ()
  $ path = $ this-> getMemoryUsageVbsPath ();
  exec ("cscript -nologo $ path", $ usage);
  $ memory = json_decode ($ usage [0], true);
  $ memory ['usage'] = Round ((($ memory ['TotalVisibleMemorySize']-$ memory ['FreePhysicalMemory']) / $ memory ['TotalVisibleMemorySize']) * 100);
  return $ memory;

Invocation mode

$ info = new SystemInfoWindows ();
$ cpu = $ info-> getCpuUsage ();
$ memory = $ info-> getMemoryUsage ();
echo "Current system CPU usage: {$ cpu}%, memory usage {$ memory ['usage']}%"; 


The above is a small part of the introduction of PHP in the Windows environment to get CPU memory real-time usage, I hope to help you!!

Recommended for PHP:

Php+redis for snapping features

Before we share with you the PHP and Redis implementation of the mall seconds Kill function code sharing, this article is mainly for everyone to introduce ...

How PHP handles high concurrent requests for snapping-like features

This article mainly and everybody introduced in detail the PHP processing snapping up class function high concurrent request, has certain reference value ...

Workaround to bypass vulnerability in PHP with offset feature

This article mainly introduces the information about the bypass vulnerability caused by the character offset feature in PHP, not only ...

PHP code to achieve a shopping cart storage cycle of 1 days

Shopping cart cookies are stored for 1 days. Note: Browsers must support cookies to be able to use them. This paper......

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.