首页 > 编程知识 正文

php获取当前内存占用的方法(php占用内存过高)

时间:2023-12-11 12:29:18 阅读:314300 作者:LYZM

本文目录一览:

如何获知PHP程序占用多少内存memory

memory_get_usage(bool),获取当前php程序所用内存,你要是问的php进程的话 就打开资源管理器

Windows下 PHP怎么获取CPU和内存使用率。。。。急

参考如下:

获取服务器性能CPU、内存、硬盘等使用率 get_used_status.php

?php

include("conn.php");

$obj_MyConnect = new MyConnect();

$obj_MyConnect - connect(DB_SERVER,DB_USER,DB_PWD,DB_NMAE);

function get_used_status(){

$fp = popen('top -b -n 2 | grep -E "^(Cpu|Mem|Tasks)"',"r");//获取某一时刻系统cpu和内存使用情况

$rs = "";

while(!feof($fp)){

$rs .= fread($fp,1024);

}

pclose($fp);

$sys_info = explode("n",$rs);

$tast_info = explode(",",$sys_info[3]);//进程 数组

$cpu_info = explode(",",$sys_info[4]); //CPU占有量 数组

$mem_info = explode(",",$sys_info[5]); //内存占有量 数组

//正在运行的进程数

$tast_running = trim(trim($tast_info[1],'running'));

//CPU占有量

$cpu_usage = trim(trim($cpu_info[0],'Cpu(s): '),'%us'); //百分比

//内存占有量

$mem_total = trim(trim($mem_info[0],'Mem: '),'k total');

$mem_used = trim($mem_info[1],'k used');

$mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //百分比

$fp = popen('df -lh | grep -E "^(/)"',"r");

$rs = fread($fp,1024);

pclose($fp);

$rs = preg_replace("/s{2,}/",' ',$rs); //把多个空格换成 “_”

$hd = explode(" ",$rs);

$hd_avail = trim($hd[3],'G'); //磁盘可用空间大小 单位G

$hd_usage = trim($hd[4],'%'); //挂载点 百分比

//print_r($hd);

//检测时间

$fp = popen("date +"%Y-%m-%d %H:%M"","r");

$rs = fread($fp,1024);

pclose($fp);

$detection_time = trim($rs);

如何获知PHP程序占用多少内存(memory

?phpecho '开始内存:'.memory_get_usage(), '';$tmp = str_repeat('hello', 1000);echo '运行后内存:'.memory_get_usage(), '';unset($tmp);echo '回到正常内存:'.memory_get_usage();

查看PHP程序占用多少内存

想要知道编写的 PHP 脚本需要占用多少内存么?很简单,直接使用 PHP 查看当前分配给 PHP 脚本的内存的函数 memory_get_usage() 就可以了$memory = round(memory_get_usage() / 1024 / 1024, 2).'MB';

上面的程序默认输出单位为 byte(s))我将它转换成MB,也就是当时 PHP 脚本使用的内存(不含 memory_get_usage() 函数本身占用的内存)。

更多问题可以去php中文网问答社区提问,大神在线帮你解决,希望对你有帮助

php如何获得CPU,内存是使用率

?php

$str = shell_exec('more /proc/stat');

$pattern = "/(cpu[0-9]?)[s]+([0-9]+)[s]+([0-9]+)[s]+([0-9]+)[s]+([0-9]+)[s]+([0-9]+)[s]+([0-9]+)[s]+([0-9]+)/";

preg_match_all($pattern, $str, $out);

echo "共有".count($out[1])."个CPU,每个CPU利用率如下:br";

for($n=0;$ncount($out[1]);$n++)

{

echo $out[1][$n]."=".(100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n]))."%br";

}

?

//////////////////////////////////////////////////////////////////////

?php

$str = shell_exec('more /proc/meminfo');

$pattern = "/(.+):s*([0-9]+)/";

preg_match_all($pattern, $str, $out);

echo "物理内存总量:".$out[2][0]."br";

echo "已使用的内存:".$out[2][1]."br";

echo "-----------------------------------------br";

echo "内存使用率:".(100*($out[2][0]-$out[2][1])/$out[2][0])."%br";

?

//////////////////////////////////////////////////////////////////////

?php

$str = shell_exec('more /proc/net/dev');

$pattern = "/(eth[0-9]+):s*([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)s+([0-9]+)/";

preg_match_all($pattern, $str, $out);

echo "共有".count($out[1])."个网络接口,每个网络接口利用率如下:br";

for($n=0;$ncount($out[1]);$n++)

{

echo $out[1][$n].":收到 ".$out[3][$n]." 个数据包,发送 ".$out[11][$n]." 个数据包br";

}

?

php怎么查看一个变量的占用内存

我们在前面的php高效写法提到,尽量不要复制变量,特别是数组。一般来说,PHP数组的内存利用率只有 1/10, 也就是说,一个在C语言里面100M 内存的数组,在PHP里面就要1G。下面我们可以粗略的估算PHP数组占用内存的大小,首先我们测试1000个元素的整数占用的内存:

[php] view plain copy print?

?php

echo memory_get_usage() , 'br';

$start = memory_get_usage();

$a = Array();

for ($i=0; $i1000; $i++) {

$a[$i] = $i + $i;

}

$mid = memory_get_usage();

echo memory_get_usage() , 'br';

for ($i=1000; $i2000; $i++) {

$a[$i] = $i + $i;

}

$end = memory_get_usage();

echo memory_get_usage() , 'br';

echo 'argv:', ($mid - $start)/1000 ,'bytes' , 'br';

echo 'argv:',($end - $mid)/1000 ,'bytes' , 'br';

输出是:

353352

437848

522024

argv:84.416bytes

argv:84.176bytes

大概了解1000

个元素的整数数组需要占用 82k 内存,平均每个元素占用 84 个字节。而纯 C 中整体只需要 4k(一个整型占用4byte * 1000

)。memory_get_usage() 返回的结果并不是全是被数组占用了,还要包括一些 PHP

运行本身分配的一些结构,可能用内置函数生成的数组更接近真实的空间:

[php] view plain copy print?

?php

$start = memory_get_usage();

$a = array_fill(0, 10000, 1);

$mid = memory_get_usage(); //10k elements array;

echo 'argv:', ($mid - $start )/10000,'byte' , 'br';

$b = array_fill(0, 10000, 1);

$end = memory_get_usage(); //10k elements array;

echo 'argv:', ($end - $mid)/10000 ,'byte' , 'br';

得到:

argv:54.5792byte

argv:54.5784byte

从这个结果来看似乎一个数组元素大约占用了54个字节左右。

首先看一下32位机C语言各种类型占用的字节:

[cpp] view plain copy print?

#include "stdafx.h"

//#include stdio.h

int main() {

printf("int:%dnlong:%dndouble:%dnchar*:%dnsize_t:%dn",

sizeof(int), sizeof(long),

sizeof(double), sizeof(char *),

sizeof(size_t));

return 0;

}

int:4

long:4

double:8

har*:4

size_t:4

在PHP中都使用long类型来代表数字,没有使用int类型

大家都明白PHP是一种弱类型的语言,它不会去区分变量的类型,没有int float char *之类的概念。

我们看看php在zend里面存储的变量,PHP中每个变量都有对应的 zval, Zval结构体定义在Zend/zend.h里面,其结构:

[cpp] view plain copy print?

typedef struct _zval_struct zval;

struct _zval_struct {

/* Variable information */

zvalue_value value; /* The value 1 12字节(32位机是12,64位机需要8+4+4=16) */

zend_uint refcount__gc; /* The number of references to this value (for GC) 4字节 */

zend_uchar type; /* The active type 1字节*/

zend_uchar is_ref__gc; /* Whether this value is a reference () 1字节*/

};

PHP使用一种UNION结构来存储变量的值,即zvalue_value 是一个union,UNION变量所占用的内存是由最大

成员数据空间决定。

[cpp] view plain copy print?

typedef union _zvalue_value {

long lval; /* long value */

double dval; /* double value */

struct { /* string value */

char *val;

int len;

} str;

HashTable *ht; /* hash table value */

zend_object_value obj; /*object value */

} zvalue_value;

最大成员数据空间是struct str,指针占*val用4字节,INT占用4字节,共8字节。

struct zval占用的空间为8+4+1+1 = 14字节,

其实呢,在zval中数组,字符串和对象还需要另外的存储结构,数组则是一个 HashTable:

HashTable结构体定义在Zend/zend_hash.h.

[cpp] view plain copy print?

typedef struct _hashtable {

uint nTableSize;//4

uint nTableMask;//4

uint nNumOfElements;//4

ulong nNextFreeElement;//4

Bucket *pInternalPointer; /* Used for element traversal 4*/

Bucket *pListHead;//4

Bucket *pListTail;//4

Bucket **arBuckets;//4

dtor_func_t pDestructor;//4

zend_bool persistent;//1

unsigned char nApplyCount;//1

zend_bool bApplyProtection;//1

#if ZEND_DEBUG

int inconsistent;//4

#endif

} HashTable;

HashTable 结构需要 39 个字节,每个数组元素存储在 Bucket 结构中:

[cpp] view plain copy print?

typedef struct bucket {

ulong h; /* Used for numeric indexing 4字节 */

uint nKeyLength; /* The length of the key (for string keys) 4字节 */

void *pData; /* 4字节*/

void *pDataPtr; /* 4字节*/

struct bucket *pListNext; /* PHP arrays are ordered. This gives the next element in that order4字节*/

struct bucket *pListLast; /* and this gives the previous element 4字节 */

struct bucket *pNext; /* The next element in this (doubly) linked list 4字节*/

struct bucket *pLast; /* The previous element in this (doubly) linked list 4字节*/

char arKey[1]; /* Must be last element 1字节*/

} Bucket;

Bucket

结构需要 33 个字节,键长超过四个字节的部分附加在 Bucket 后面,而元素值很可能是一个 zval 结构,另外每个数组会分配一个由

arBuckets 指向的 Bucket 指针数组, 虽然不能说每增加一个元素就需要一个指针,但是实际情况可能更糟。这么算来一个数组元素就会占用

54 个字节,与上面的估算几乎一样。

一个空数组至少会占用 14(zval) + 39(HashTable) + 33(arBuckets) = 86

个字节,作为一个变量应该在符号表中有个位置,也是一个数组元素,因此一个空数组变量需要 118

个字节来描述和存储。从空间的角度来看,小型数组平均代价较大,当然一个脚本中不会充斥数量很大的小型数组,可以以较小的空间代价来获取编程上的快捷。但如果将数组当作容器来使用就是另一番景象了,实际应用经常会遇到多维数组,而且元素居多。比如10k个元素的一维数组大概消耗540k内存,而10k

x 10 的二维数组理论上只需要 6M 左右的空间,但是按照 memory_get_usage

的结果则两倍于此,[10k,5,2]的三维数组居然消耗了23M,小型数组果然是划不来的。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。