开发者

Get Memory Usage For A PHP Script

Need advise here. Currently I am get the memory usage of the script by using this code

$sysMem = escapeshellcmd(system('echo $(free)'));
memory_get_usage();

This is the result i am getting :

Total Mem : 1034708

Used Mem : 1014572

Free Mem : 20136

Shared Mem : 0

Buff Mem : 73456

Cached Mem : 480752

----------------------------------------------------

Mem Usegae : 开发者_高级运维509464

Total Mem : 1034708

Used Mem : 1014564

Free Mem : 20144

Shared Mem : 0

Buff Mem : 73456

Cached Mem : 480828

----------------------------------------------------

Mem Usegae : 343904

However i found out that the memory usage is kind of inconsistent and at time the memory usage might even exceed the total memory which is impossible.

Is memory_get_usage(); the best option to get the memory usage of the php script? Or is it i need to use unset() function. However even if i use the memory still about the same

if there are other methods please kindly advise.

Thanks a millions

Guys and Ladies


Maybe this will help using pure PHP. You can get the exact total memory using:

ini_get('memory_limit');

But this will be something like '128M'. To convert this form into bytes look at Stack-Overflow-Convert String Mem-Bytes into bytes. Now you have 2 methods for getting the script memory usage:

memory_get_peak_usage();

and

memory_get_usage();

With these ingedients the puzzle is easy (imho).


This is going to be difficult to state precisely. You might need to specify more carefully exactly what you are trying to do.

For one, calling out to the 'free' command is stepping off into the void of Linux's virtual memory scheme. You need to know how to interpret these meaningfully.

As far as I can tell, get_memory_usage could behave in a number of different ways depending on how PHP is running. It appears to be returning how much memory is allocated to the current PHP interpreter in total. This is unlikely to be indicative of how much memory the current script is using.

Also, free returns memory in kilobytes. PHP's get_memory_usage returns it in bytes. You are off by a factor of 1024. Divide the latter by 1024 to have comparable units.

So what are you trying to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜