💾 Archived View for perso.pw › blog › articles › openbsd-understand-memory-usage.gmi captured on 2024-05-12 at 15:13:41. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-09-08)

-=-=-=-=-=-=-

Some explanations about OpenBSD memory usage

Comment on Mastodon

Introduction

I regularly see people reporting high memory usage on OpenBSD when looking at some monitoring program output.

Those programs may be not reporting what you think. The memory usage can be accounted in different ways.

Most of the time, the file system cache stored in-memory is added to memory usage, which lead to think about a high memory consumption.

How to figure the real memory usage?

Here are a few methods to gather the used memory.

Using ps

You can actually use `ps` and sum the RSS column and display it as megabytes:

ps auwxx | awk '{ sum+=$6 } END { print sum/1024 }'

You could use the 5th column if you want to sum the virtual memory, which can be way higher than your system memory (hence why it's called virtual).

Using top

When running `top` in interactive mode, you can find a memory line at the top of the output, like this:

Memory: Real: 244M/733M act/tot Free: 234M Cache: 193M Swap: 158M/752M

This means there are 244 MB of memory currently in use, and 158 MB in the swap file.

The cache column displays how much file system data you have cached in memory, this is extremely useful because every time you open a program, this would avoid seeking it on the storage media if it's already in the memory cache, which is way faster. This memory is freed when needed if there are not enough free memory available.

The "free" column only tell you that this ram is completely unused.

The number 733M indicates the total real memory, which includes memory in use that could be freed if required, however if someone find a clearer explanation, I'd be happy to read it.

Using systat

The command `systat` is OpenBSD specific, often overlooked but very powerful, it has many displays you can switch to using left/right arrows, each aspect of the system has its own display.

The default display has a "memory totals in (KB)" area about your real, free or virtual memory.

Going further

Inside the kernel, the memory naming is different, and there are extra categories. You can find them in the kernel file `sys/uvm/uvmexp.h`:

GitHub page for sys/uvm/uvmexp.h lines 56 to 62

Conclusion

When one looks at OpenBSD memory usage, it's better to understand the various field before reporting a wrong amount, or that OpenBSD uses too much memory. But we have to admit the documentation explaining each field is quite lacking.