# How to find memory available as standard user



## stratacast1 (Aug 18, 2018)

Right now I'm trying to find an easy way to output to stdio from /bin/sh the memory available to me. I know there's a "avail memory" in dmesg, but that shows how much is available to my OS to use, not (total - cache - buffers) like I want. I can see that number being visualized in htop, but how can I get the actual number for memory available? Doesn't matter if I have to do some math, I just can't figure out where to get the values. I assume sysctl but it's unclear to me what I need from that.


----------



## ShelLuser (Aug 18, 2018)

I think you might want to look into vmstat(8) for that.


----------



## stratacast1 (Aug 18, 2018)

Hmmmm I've already pursued that option, I've been trying to figure out which fields to awk from


----------



## stratacast1 (Aug 18, 2018)

Looking around, I'm not sure If I understand these variables, or if their output is erroneous:

sysctl vm.stats.vm.v_cache_count
sysctl vfs.bufspace

On my system both of those = 0 but htop shows otherwise (4GB mem usage and the rest of my memory is shown to have a full cache). So I have no clue which one is accurate.


----------



## JAW (Aug 19, 2018)

stratacast1 said:


> Looking around, I'm not sure If I understand these variables, or if their output is erroneous:
> 
> sysctl vm.stats.vm.v_cache_count
> sysctl vfs.bufspace
> ...



This is what I did for a status bar to show memory available on FreeBSD;
https://github.com/Digital-Chaos/slstatus/blob/freebsd/components/ram.c

Hope it helps!


----------



## ralphbsz (Aug 19, 2018)

What do you mean by "available"?

Let's begin by assuming that you don't mean kernel memory that will be used by the kernel for tasks on your behalf (like your network traffic, or bounce buffers for you IOs).  Let's also assume that you don't mean buffer cache that will be used for write-behind or read caching of file IO you are doing.  Let's assume that you mean only memory that can be used by running applications.

Do you mean stack or heap?  Honestly, I have no idea how to determine how much stack space you have, programmatically, other than by trial and error.  Heap space is hard.  First, there are various limits that can be applied to a running program (such as ulimit).  Then, you can typically allocate a lot with sbrk() and malloc(), as long as you don't actually touch the pages.  And then, the number of pages you actually touch depends on whether you want to count only resident memory, or also shared libraries, mmap()'ed files, and memory that is currently swapped out.

Please explain what you are really trying to accomplish.  This thing sounds like a "XY problem".


----------



## stratacast1 (Aug 19, 2018)

So by available, you're correct I don't mean memory that will be used by the kernel, but I did mean buffer and cache. I put the 2 sysctl values in a response earlier
sysctl vm.stats.vm.v_cache_count
sysctl vfs.bufspace

These, to my understanding are the values I want that will display my cache and buffer space that is used. However, they're both displaying 0 for me on all 3 of my boxes (1 shows bufspace has data usage), and something says that's wrong. My goal is to get a cache value similar to what is output in the top output in OpenBSD. That's ultimately my goal.

JAW, I will take a look at the link you sent me  I'm hoping to solve this for myself, but I also found that pythonX psutil is gathering those 2 sysctl values I shared, and if I'm not the only one getting 0s for these values, that tells me the output is not what I think it is or there's a bug.


----------



## Crivens (Aug 19, 2018)

There is memory which is free and memory which can be made free (inactive, cache). Maybe you want to add them.


----------



## stratacast1 (Aug 21, 2018)

Hmmm well I'm confused. I think I'm just trying to translate some of the Linuxisms in how memory usage is reported into FreeBSD's way and not getting it. 

psutil's way of getting available memory is basically `sysctl vm.stats.vm.v_cache_count + sysctl vfs.bufspace`. I got 481, then also did psutil.free + psutil.inactive and it came to psutil.availble. So I think those numbers line up consistently with what you say Crivens ...correct me if I'm wrong. I should go back through memory usage reporting again because for memory, top(1) and sysutils/htop report pretty different info for memory, and it seems reading memory in htop on FreeBSD is not the same as you'd read it on Linux. Reason why I'm trying to figure this stuff out is mainly because I'm writing some scripts to give me system data between some of my FreeBSD/OpenBSD/Linux boxes


----------



## JAW (Aug 21, 2018)

I do pretty much what Crivens suggested for "available memory", and I think it's slightly different between Linux and FreeBSD;

available = free + inactive


----------



## stratacast1 (Aug 25, 2018)

Great! Thank you guys this really helps and makes more sense to me now


----------

