Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use MemTotal: - MemAvailable: for 'memused' calculation on Linux 3.14+ #1591

Closed
ChrisCheney opened this issue Nov 2, 2020 · 0 comments
Closed

Comments

@ChrisCheney
Copy link

ChrisCheney commented Nov 2, 2020

Linux 3.14 in 2014 added a proper way to determine how much memory is actually in use by adding MemAvailable to /proc/meminfo since it is very complicated to determine what is 'correct' from userspace.

/proc/meminfo: provide estimated available memory git commit for Linux 3.14-rc1

Neofetch also used to support reporting memory using this method on modern kernels, until a patch reverted it on Mar 25, 2016.

Neofetch reverted this support for unknown reasons to 'work on older linux kernel versions' instead of just adding a case statement to support those ancient kernels. By now the support for ancient kernels could probably be dropped altogether although there a few users on CentOS/RHEL 7 (3.10.0) or SLES 11 (3.0.76) that might complain. Note that CentOS 6 (2.6.32) will be EOL at the end of this month (Nov 30, 2020). I don't know of any other supported distribution revisions that still support such an ancient kernel version.

The amount of memory that is not properly reported, by using the old calculations, can end up being many GB in size. I saw the difference between Neofetch and (MemTotal-MemAvailable) vary by over 5GB on my system last week. At the moment on my system it only differs by ~ 1GB, which is still quite a lot. Not only does Neofetch not report the correct amount of memory used on newer kernels but it also does not match conky or screenfetch output so it is especially confusing to new users that don't know why neofetch reports a different amount of used memory. I've seen a number of users ask why the numbers differ between different apps reporting memory used.

$ cat /proc/meminfo 
MemTotal:       65828792 kB
MemFree:        45133872 kB
MemAvailable:   53042844 kB
Buffers:          692196 kB
Cached:         12510680 kB
commit 5939c27b60deea10e88482489ab1ea90ef2a06f2
Author: Dylan <dyl@tfwno.gf>
Date:   Fri Mar 25 21:51:21 2016 +1100

    Rewrite memory function to work on older linux kernel versions

diff --git a/neofetch b/neofetch
index 8bac24d..8711c55 100755
--- a/neofetch
+++ b/neofetch
@@ -1112,17 +1112,9 @@ getgpu () {
 getmemory () {
     case "$os" in
         "Linux")
-            # Read first 3 lines
-            mem=$(awk -F ':' '/MemTotal|MemAvailable/ {printf $2}' /proc/meminfo )
-
-            # Do some substitution on each line
-            memtotal=${mem/kB*/kB}
-            memavail=${mem/${memtotal}}
-            memtotal=${memtotal/kB*}
-            memavail=${memavail/kB*}
-
-            memused=$((memtotal - memavail))
-            memory="$((memused / 1024))MB / $((memtotal / 1024))MB"
+            mem=($(awk -F ':| kB' '/MemTotal|MemFree|Buffers|Cached/ {printf $2}' /proc/meminfo ))
+            memused=$((${mem[0]} - ${mem[1]} - ${mem[2]} - ${mem[3]}))
+            memory="$((memused / 1024))MB / $((${mem[0]} / 1024))MB"
         ;;
 
         "Mac OS X")
@ChrisCheney ChrisCheney changed the title Use MemTotal: - MemAvailable: for usedMem calculation on Linux 3.14+ Use MemTotal: - MemAvailable: for 'memused' calculation on Linux 3.14+ Nov 2, 2020
phdenzel added a commit to phdenzel/neofetch that referenced this issue Apr 30, 2021
* upstream/master: (355 commits)
  neofetch: Fix naming conflict with mine. See dylanaraps#1616
  NixOS - squash logo, add new package location. (dylanaraps#1617)
  neofetch: swap to apt for packages. Possible fix for dylanaraps#1616
  Add Apple Music (dylanaraps#1611)
  Fix font support for yakuake (dylanaraps#1615)
  Refactor ueberzug (dylanaraps#1614)
  New Garuda Neofetch Logo (dylanaraps#1613)
  Add support for `swupd` package manager (dylanaraps#1610)
  disk: Fix bash 3.2 incompatibility. Closes dylanaraps#1586
  fix whitespace
  fix macOS issue. See dylanaraps#1607
  update man page (dylanaraps#1600)
  Support multiple displays under wayland (dylanaraps#1596)
  Update SEMC logo (dylanaraps#1593)
  feat(backend): add viu image backend (dylanaraps#1592)
  neofetch: Use MemAvailable for memory on Linux if supported. Closes dylanaraps#1591
  Update NixOS Logo (dylanaraps#1590)
  update garuda linux logo (dylanaraps#1579)
  Adding support for Pengwin (dylanaraps#1578)
  various: fix windows issues
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant