Remix.run Logo
drdexebtjl 3 hours ago

I don’t understand the part about using heuristics and deciding what counts as used memory…

Used memory for the system is always total minus available.

Heuristics? I would hope that the system knows precisely what is using every single byte of physical and virtual memory. Is this a reporting problem? Why do we have to settle for heuristics and not the exact number?

CrociDB 2 hours ago | parent | next [-]

The thing is it's easy to define free, unused memory. But a lot of the used memory is your system caching stuff that would be free if you needed more than what's actually free. So you can see you have 1g of free memory out of your 4g, but then you allocate 3g and it will do without a sweat and you'd be confused. So you have to go and dig for what those caches are and report that they're effectively free too.

drdexebtjl 2 hours ago | parent [-]

Instantly reclaimable disk caches should count as available, and they do.

This isn’t hard. The OS should just expose a counter for available memory instead of having applications understand every type of memory reservation.

edit:

Linux does this, but it has its own share of issues with memory counters. The “cached” memory includes tmpfs and ramfs for seemingly no reason.

jkrejcha 42 minutes ago | parent [-]

> The “cached” memory includes tmpfs and ramfs for seemingly no reason.

If you're curious why that is by the way, it's because that's actually how these are implemented (tmpfs/ramfs is just a mount to a filesystem where the files never get marked clean[1])

[1]: https://www.kernel.org/doc/Documentation/filesystems/ramfs-r...

drdexebtjl 16 minutes ago | parent [-]

That’s clever. Makes for terrible UX, though.

AFAIK the only way for you to figure out how much of your disks is actually cached involves enumerating all tmpfs and ramfs mounts, summing their sizes, and subtracting the sum from the cache size reported by the kernel.

toast0 2 hours ago | parent | prev | next [-]

> I would hope that the system knows precisely what is using every single byte of physical and virtual memory.

Of course the system knows what is using every page. The difficulty is really in how to account for pages that are backed by disk.

If you count all of those as free, that's not accurate. If you count all of those as used, that's not accurate either. Additionally, FreeBSD (at least) doesn't have separate queues for disk backed pages, so there's not really a good way to know how much of your active (or inactive) memory is disk backed.

As an additional caveat that measuring active/inactive has costs. In the past, FreeBSD wouldn't really do the work for that until it needed to... I know some stuff changed, but I don't remember where it ended up; it wasn't great when it bulk marked a ton of pages as inactive and then the active ones would fault back in.

jkrejcha 6 minutes ago | parent | next [-]

This is only really a problem if you accept overcommit as a force of nature that can't be changed or tweaked (you can still do address space reservation without needing overcommit)

If you don't, it becomes rather easy (and strict commit accounting is done for example on Linux even if it isn't used in some cases)

Memory mapped files can be entirely recreated from the disk so no need to charge for them. Anonymous pages (whether private or shareable) have to be charged. Shareable memory is the harder one to charge. (The case where a mapping is used by only one can get charged as private commit.) These two previous cases are charged even if in a swap file or whatnot

drdexebtjl 2 hours ago | parent | prev [-]

> If you count all of those as free, that’s not accurate.

Why not? It depends on what you’re measuring. Physical memory? They count as free. Virtual memory? They count as used.

The ambiguities only arise when we stop making that distinction very clear.

man8alexd 2 hours ago | parent | prev [-]

You will be surprised by how inaccurate memory measurements are.