How to find memory available as standard user

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.
 
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.
 
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.

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!
 
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".
 
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.
 
There is memory which is free and memory which can be made free (inactive, cache). Maybe you want to add them.
 
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 :)
 
I do pretty much what Crivens suggested for "available memory", and I think it's slightly different between Linux and FreeBSD;

available = free + inactive
 
Back
Top