htop shows high RAM use vs top?

mem.png


I had a game client updater in the background and a few Firefox tabs, but I'm pretty confident it wasn't using anywhere near 13GB RAM. I ran similar apps on Linux and htop seemed to report ok out-the-box.

I'm thinking maybe htop has different defaults compared to Linux and reports memory usage different? The 2GB active on top looks more-expected, but I'm not really familiar with what inact/laundry/etc mean.

Alternatively, does anyone know of another resource monitoring app that might report memory use differently and is lightweight? I like htop for running in terminal and having the bars :p (a little more eye-candy vs top)
 
[…] I'm thinking maybe htop has different defaults compared to Linux and reports memory usage different? […]
Thread 71432. htop(1) reads /compat/linux/proc/meminfo. linprocfs(5) calculates the Cached value as inactive + active + laundry.​
htop shows high RAM use vs top? […] but I'm not really familiar with what inact/laundry/etc mean.
High RAM use is good (within reason). You want to utilize your memory as much as possible; inactive and laundry are just different aging levels of cached data. A proposed memory(7) manual page explaining it a bit more is undergoing a differential review. Thread 84695.​
[…] I like htop for running in terminal and having the bars […]
Me too, but I’m afraid few if any sysutils distinguish between the different FreeBSD‐specific categorizations.​
 
Last edited:
In htop enable the option "Show cached memory in graph and bar modes"
Thanks, but it looks like it was enabled already (pretty sure I didn't change it from default; toggling it adds orange bars at the end of the green but doesn't change the reported amount)
 
There's a Perl script on the internet to see the exact memory usage on FreeBSD, I have ported it to sh and updated it a little (added ZFS ARC/cache/buffers/laundry to the math)
Not sure if it's 100% accurate, in fact it uses the same logic as neofetch does.


sh:
#!/bin/sh

# Retrieve required sysctl values
hw_physmem=$(sysctl -n hw.physmem)
pagesize=$(sysctl -n hw.pagesize)
v_page_count=$(sysctl -n vm.stats.vm.v_page_count)
v_wire_count=$(sysctl -n vm.stats.vm.v_wire_count)
v_active_count=$(sysctl -n vm.stats.vm.v_active_count)
v_inactive_count=$(sysctl -n vm.stats.vm.v_inactive_count)
v_cache_count=$(sysctl -n vm.stats.vm.v_cache_count)
v_free_count=$(sysctl -n vm.stats.vm.v_free_count)
v_laundry_count=$(sysctl -n vm.stats.vm.v_laundry_count)
zfs_arc=$(sysctl -n kstat.zfs.misc.arcstats.size)
zfs_arc_min=$(sysctl -n kstat.zfs.misc.arcstats.c_min)
zfs_arc_max=$(sysctl -n kstat.zfs.misc.arcstats.c_max)
mem_buf=$(sysctl -n vfs.bufspace)
# Calculate values in bytes
mem_phys=$hw_physmem
mem_all=$((v_page_count * pagesize))
mem_wire=$((v_wire_count * pagesize))
mem_active=$((v_active_count * pagesize))
mem_inactive=$((v_inactive_count * pagesize))
mem_cache=$((v_cache_count * pagesize))
mem_free=$((v_free_count * pagesize))
mem_laundry=$((v_laundry_count * pagesize))

# Round up to next reasonable power-of-two based on assumed 8 memory modules
mem_rounded() {
    size=$1
    chip_guess=$(( (size / 8) - 1 ))
    chip_size=1
    while [ "$chip_guess" -gt 0 ]; do
        chip_guess=$(( chip_guess >> 1 ))
        chip_size=$(( chip_size << 1 ))
    done
    echo $(( ((size + chip_size - 1) / chip_size) * chip_size ))
}

mem_hw=$(mem_rounded "$mem_phys")

# Gaps
mem_gap_vm=$((mem_all - (mem_wire + mem_active + mem_inactive + mem_cache + mem_free + mem_laundry)))
mem_gap_sys=$((mem_phys - mem_all))
mem_gap_hw=$((mem_hw - mem_phys))

# Logical memory
mem_total=$mem_hw
mem_avail=$((mem_inactive + mem_cache + mem_free + mem_buf))
mem_avail_total=$((mem_avail + zfs_arc - zfc_arc_min))
mem_used=$((mem_total - mem_avail))

# Print results
print_line() {
    label=$1
    symbol=$2
    bytes=$3
    desc=$4
    percent=$5
    mb=$((bytes / (1024 * 1024)))
    printf "%-15s%s %12d (%7dMB) " "$label:" "$symbol" "$bytes" "$mb"
    if [ -n "$percent" ]; then
        printf "[%3d%%] " "$percent"
    else
        printf "       "
    fi
    echo "$desc"
}

percent() {
    awk "BEGIN { printf \"%d\", ($1 / $2) * 100 }"
}

HR="---------------- ------------ ----------- ------"
echo "SYSTEM MEMORY INFORMATION:"
print_line "mem_wire"     " " "$mem_wire"     "Wired: disabled for paging out"       "$(percent $mem_wire $mem_all)"
print_line "mem_active"   "+" "$mem_active"   "Active: recently referenced"          "$(percent $mem_active $mem_all)"
print_line "mem_inactive" "+" "$mem_inactive" "Inactive: recently not referenced"    "$(percent $mem_inactive $mem_all)"
print_line "mem_cache"    "+" "$mem_cache"    "Cached: almost avail. for allocation" "$(percent $mem_cache $mem_all)"
print_line "mem_buf"      "+" "$mem_buf"      "UFS buffer cache: almost avail. for allocation"              "$(percent $mem_buf $mem_all)"
print_line "mem_free"     "+" "$mem_free"     "Free: fully available for allocation" "$(percent $mem_free $mem_all)"
print_line "mem_laundry"  "+" "$mem_laundry"  "Laundered: must be cleaned before re-allocation" "$(percent $mem_laundry $mem_all)"
print_line "mem_gap_vm"   "+" "$mem_gap_vm"   "Memory gap: UNKNOWN"                   "$(percent $mem_gap_vm $mem_all)"
echo "$HR"
print_line "mem_all"      "=" "$mem_all"      "Total real memory managed"           100
print_line "mem_gap_sys"  "+" "$mem_gap_sys"  "Memory gap: Kernel?!"
echo "$HR"
print_line "mem_phys"     "=" "$mem_phys"     "Total real memory available"
print_line "mem_gap_hw"   "+" "$mem_gap_hw"   "Memory gap: Probably BIOS + iGPU VRAM (?)"
echo "$HR"
print_line "mem_hw"       "=" "$mem_hw"       "Total real memory installed"
echo "$HR"
echo
echo "SYSTEM MEMORY SUMMARY:"
print_line "mem_used"     " " "$mem_used"     "Logically used memory"              "$(percent $mem_used $mem_total)"
print_line "mem_avail_cur"    "=" "$mem_avail"    "Logically available memory"         "$(percent $mem_avail $mem_total)"
print_line "mem_avail_max"    "=" "$mem_avail_total"    "Maximum available memory"         "$(percent $mem_avail_total $mem_total)"
echo "$HR"
print_line "mem_total"    "+" "$mem_total"    "Logically total memory"             100
echo "$HR"
echo
echo "EXTRA INFO:"
print_line "zfs_arc"      "=" "$zfs_arc"      "ZFS Adaptive Replacement Cache (part of mem_wire)"  "$(percent $zfs_arc $mem_all)"
print_line "zfs_arc_min"      "=" "$zfs_arc_min"      "Minimum amount of ZFS ARC"  "$(percent $zfs_arc_min $mem_all)"
print_line "zfs_arc_max"      "=" "$zfs_arc_max"      "Maximum amount of ZFS ARC"  "$(percent $zfs_arc_max $mem_all)"
echo "$HR"

Note: "available/free RAM" is not an exact number on FreeBSD, it can free up memory if required. This is why I introduced the mem_avail_max thingy, hope it's correct.

Note #2: the original way to get "sysctl -a" then grep it multiple times was very slow, meanwhile this approach is pretty fast. I'm happy with it :)

Example output on my hybrid (ufs+zfs) system:

SYSTEM MEMORY INFORMATION:
mem_wire: 1386438656 ( 1322MB) [ 34%] Wired: disabled for paging out
mem_active: + 1825419264 ( 1740MB) [ 44%] Active: recently referenced
mem_inactive: + 675008512 ( 643MB) [ 16%] Inactive: recently not referenced
mem_cache: + 0 ( 0MB) [ 0%] Cached: almost avail. for allocation
mem_buf: + 51978240 ( 49MB) [ 1%] UFS buffer cache: almost avail. for allocation
mem_free: + 166166528 ( 158MB) [ 4%] Free: fully available for allocation
mem_laundry: + 2093056 ( 1MB) [ 0%] Laundered: must be cleaned before re-allocation
mem_gap_vm: + 3497984 ( 3MB) [ 0%] Memory gap: UNKNOWN
---------------- ------------ ----------- ------
mem_all: = 4058624000 ( 3870MB) [100%] Total real memory managed
mem_gap_sys: + 130568192 ( 124MB) Memory gap: Kernel?!
---------------- ------------ ----------- ------
mem_phys: = 4189192192 ( 3995MB) Total real memory available
mem_gap_hw: + 105775104 ( 100MB) Memory gap: Probably BIOS + iGPU VRAM (?)
---------------- ------------ ----------- ------
mem_hw: = 4294967296 ( 4096MB) Total real memory installed
---------------- ------------ ----------- ------

SYSTEM MEMORY SUMMARY:
mem_used: 3401814016 ( 3244MB) [ 79%] Logically used memory
mem_avail_cur: = 893153280 ( 851MB) [ 20%] Logically available memory
mem_avail_max: = 1645260616 ( 1569MB) [ 38%] Maximum available memory
---------------- ------------ ----------- ------
mem_total: + 4294967296 ( 4096MB) [100%] Logically total memory
---------------- ------------ ----------- ------

EXTRA INFO:
zfs_arc: = 752107336 ( 717MB) [ 18%] ZFS Adaptive Replacement Cache (part of mem_wire)
zfs_arc_min: = 130912256 ( 124MB) [ 3%] Minimum amount of ZFS ARC
zfs_arc_max: = 3115450368 ( 2971MB) [ 76%] Maximum amount of ZFS ARC

 
I like btop, but there is still a bit of difference between top and btop (but like stated above, not all are perfect and it's not as bad as htop). FYI: I like btop in low color mode.
 
Back
Top