I'm aware of this question https://stackoverflow.com/questions/41224738/how-to-calculate-system-memory-usage-from-proc-meminfo-like-htop
Answer seems outdated regarding memory usage and this question seems more complex.
htop definitely now doesn't calculate total memory via MemTotal - MemFree
anymore.
There https://access.redhat.com/solutions/406773 I found a soluation that could fit. According to that it would be calculated via MemTotal - MemFree - Buffers - Cached - Slab
but that leads to a too low memory usage compared to htop.
Memory usage in htop seems to be quite related as free -m
shows in cell Mem: used
But if you create a ramdisk, things change a lot:
mkdir mnt
sudo mount -t tmpfs none mnt
cd mnt
sudo dd if=/dev/urandom of=test bs=32M status=progress
Above will create a RAM disk 50 % size of installed memory and fill this ramdisk.
htop now shows a bit more than half of the physical RAM used, in test case 3.82 GB of 7.44 GB used. However free -m
shows still only 104 MB used compared to 102 MB before creating and filling the RAM disk. So 104 MB used is definitely wrong, because half of the physical system memory can't be available anymore since it is used by RAM disk.
In /proc/meminfo
size of RAM disk seems to be included in Cached
, so for calculating used memory cached shouldn't be substracted.
So why does free -m report such a wrong free memory value? By default there are various tmpfs on distributions like Debian or Ubuntu which then means free -m gives wrong impression of free memory.
Memory calculation of htop seems quite reasonable. So how can the htop value be calculated from /proc/meminfo
?