Cara menggunakan get current method php

Tony Lea ・ Sep 27th, 2012 Views

Table of Contents

  • How do I monitor PHP memory usage?
  • How much memory does each PHP process consume?
  • How do I check my memory usage?
  • Which of the following PHP functions can be used to get the current memory usage?

So, a couple weeks ago I experienced heavy server overloads on an app due to so many concurrent users. There may have also been some code clean up that was necessary. Either way it was kind of a headache, but also a learning experience. So upon searching the interent I could not find an easy way to print out the Server Memory Usage or CPU Usage as a percentage. So, with the help of some string manipulation and a fellow co-worker we created these 2 php functions. These configurations worked on a MediaTemple [ve] server with Ubuntu 11.04. The first function will return the Server Memory Usage:

function get_server_memory_usage[]{
	
	$free = shell_exec['free'];
	$free = [string]trim[$free];
	$free_arr = explode["\n", $free];
	$mem = explode[" ", $free_arr[1]];
	$mem = array_filter[$mem];
	$mem = array_merge[$mem];
	$memory_usage = $mem[2]/$mem[1]*100;

	return $memory_usage;
}

This function will return the Server CPU Usage:

function get_server_cpu_usage[]{

	$load = sys_getloadavg[];
	return $load[0];

}

So, if you were to call the two functions in a php file like this:

Server Memory Usage: = get_server_memory_usage[] ?>%

Server CPU Usage: = get_server_cpu_usage[] ?>%

You'll probably end up with a page that looks similar:

And that's how you can get the server memory and cpu usage from PHP. I hope that these function can help someone else along the way.

Functions to get current CPU load and memory usage under Windows and Linux.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

//www.php.net/manual/en/function.sys-getloadavg.php#118673
Function to get current CPU load as percentage value under Windows and Linux.
Note: Function is getServerLoad[]. It will return a decimal value as percentage of current CPU load or NULL if something went wrong [e. g. insufficient access rights].

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

memory_get_usage[] is used to retrieve the memory allocated to PHP only [or your running script]. But intuitively, many people expect to get the memory usage of the system, based on the name of the function.
So if you need the overall memory usage, following function might be helpful. If retrieves the memory usage either in percent [without the percent sign] or in bytes by returning an array with free and overall memory of your system. Tested with Windows [7] and Linux [on an Raspberry Pi 2]:

Bài mới nhất

Chủ Đề