0

I have a PHP script that triggers the starting of other processes (Chromium instances) on the machine. I want to know if I can somehow get the amount of memory available on the current system so that I know that I can safely start another process without reaching the memory limit (which for instance may cause additional issues with a running Redis instance).

Do I have to do some sort of system call directly through exec()?

Flame
  • 6,663
  • 3
  • 33
  • 53
  • In linux (ubuntu), `free -m` (the 4th string on the 2nd line is the free memory). So use exec() or similar from PHP to do what you want – Ken Lee Sep 24 '22 at 14:58
  • Do you have to do system calls? Possibly. This question's answers includes various examples of accessing memory information (most are Linux specific but some include Windows as well): https://stackoverflow.com/q/1455379/1456201 – Jim Sep 24 '22 at 16:05

1 Answers1

0

You can read /proc/meminfo

Example below

MemTotal:        4025840 kB
MemFree:         1189212 kB
MemAvailable:    1984948 kB
Buffers:          105404 kB
Cached:           883880 kB
SwapCached:        22012 kB
Active:           652224 kB
Inactive:        2055408 kB
Active(anon):      23560 kB
Inactive(anon):  1714568 kB
Active(file):     628664 kB
Inactive(file):   340840 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:       2097148 kB
SwapFree:        1881452 kB
Dirty:                16 kB
Writeback:             0 kB
AnonPages:       1691600 kB
Mapped:           125292 kB
Shmem:             19780 kB
KReclaimable:      62736 kB
Slab:              88052 kB
SReclaimable:      62736 kB
SUnreclaim:        25316 kB
KernelStack:        2912 kB
PageTables:        11144 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     4110068 kB
Committed_AS:    2819784 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       15344 kB
VmallocChunk:          0 kB
Percpu:             1472 kB
HardwareCorrupted:     0 kB
AnonHugePages:   1536000 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
FileHugePages:         0 kB
FilePmdMapped:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      112492 kB
DirectMap2M:     4081664 kB
DirectMap1G:     2097152 kB

Edit: and here's some example code on how to obtain the values, taken from here

// mem usage
$get_meminfo = file('/proc/meminfo');

$meminfo_total = filter_var($get_meminfo[0], FILTER_SANITIZE_NUMBER_INT);
$meminfo_cached = filter_var($get_meminfo[2], FILTER_SANITIZE_NUMBER_INT);
$meminfo_free = filter_var($get_meminfo[1], FILTER_SANITIZE_NUMBER_INT);

if ($meminfo_total >= 10485760) {
    $mem_total = round(($meminfo_total / 1048576), 2);
    $mem_cached = round(($meminfo_cached / 1048576), 2);
    $mem_free = round((($meminfo_free + $meminfo_cached) / 1048576), 2);
    $mem_multiple = 'GB';
} else {
    $mem_total = round(($meminfo_total / 1024), 2);
    $mem_cached = round(($meminfo_cached / 1024), 2);
    $mem_free = round((($meminfo_free + $meminfo_cached) / 1024), 2);
    $mem_multiple = 'MB';
}

$mem = array(
    'total' => $mem_total,
    'cached' => $mem_cached,
    'free' => $mem_free
);
cantsay
  • 1,967
  • 3
  • 21
  • 34