Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get server ram with php

Tags:

php

ram

Is there a way to know the avaliable ram in a server (linux distro) with php (widthout using linux commands)?

edit: sorry, the objective is to be aware of the ram available in the server / virtual machine, for the particular server (even if that memory is shared).

like image 240
yoda Avatar asked Sep 21 '09 16:09

yoda


People also ask

How do I find the RAM size of my server?

Right-click on Start menu button and then click on Task Manager . Click on Performance tab and then on Memory option. The Memory option shows details such as Total memory, used memory and available memory. This option also shows two graphs related to the Memory in the server.

How can I get CPU and memory usage in PHP?

To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.

How do I check my server RAM utilization?

To determine memory usage statistics on a server, follow these steps: Log in to the server using SSH. For easier readability, use the -m option to display memory usage statistics in megabytes. To display statistics in bytes, run the free command without the -m option.

How do I monitor PHP memory usage?

The memory_get_usage function can be used to track the memory usage. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally. The above mentioned memory usage can be tracked using memory_get_usage().


1 Answers

If you know this code will only be running under Linux, you can use the special /proc/meminfo file to get information about the system's virtual memory subsystem. The file has a form like this:

MemTotal:       255908 kB MemFree:         69936 kB Buffers:         15812 kB Cached:         115124 kB SwapCached:          0 kB Active:          92700 kB Inactive:        63792 kB ... 

That first line, MemTotal: ..., contains the amount of physical RAM in the machine, minus the space reserved by the kernel for its own use. It's the best way I know of to get a simple report of the usable memory on a Linux system. You should be able to extract it via something like the following code:

<?php   $fh = fopen('/proc/meminfo','r');   $mem = 0;   while ($line = fgets($fh)) {     $pieces = array();     if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {       $mem = $pieces[1];       break;     }   }   fclose($fh);    echo "$mem kB RAM found"; ?> 

(Please note: this code may require some tweaking for your environment.)

like image 113
rcoder Avatar answered Sep 21 '22 13:09

rcoder