TLDR: your computer knows how to manage memory better than you, even if it seems like it is doing things weirdly.
i have seen a lot of people, in linux communities and elsewhere, worried about idle memory usage, wanting it to be lower, etc. i thought it might be a good idea to clue people in to how memory usually works in a modern computer system, why it behaves weirdly sometimes, and why "high" memory usage isnt actually a bad thing until you see noticeable performance problems.
arguably the most important thing to know is that the kernel is entirely in control of how much memory a process has access to. every OS has a system for programs to interact with the kernel for any privileged tasks, such as I/O (networking, disk access, etc), file permissions, and memory management. if you've ever heard of malloc, that is effectively how a program tells the kernel that it wants more memory.
because programs running on the system know next to nothing about the memory they are using, it allows the OS to do things such as swap space, where program memory is stored on disk until it is actually needed, with the program being none the wiser. the problem with this is that disk access is orders of magnitude slower than RAM access, even if you have incredibly fast SSDs, so the OS will limit using swap until it is actually necessary.
a corollary to that is that because disk access is so slow, the OS will keep as much data as possible in RAM. doing that keeps the experience feeling very snappy. this is also why you will see lone or a few programs that are able to use a seemingly large amount of memory - the OS is doing that on purpose to make it feel fast.
as i said earlier though, the OS is in total control of the memory on a system. if the memory being actively used on a system gets to be too high, the OS will begin to copy programs from memory into swap. you will probably be able to notice this if you have a program that is open, but takes a noticeable amount of time to get back to a "working" state, like a browser tab that has to reload or a graphical application whose interface takes a little time to load. these arent necessarily problems, as the programs will still work just fine. however, if that is a typical workload for your computer, putting your swap space on a faster SSD or getting more RAM will likely benefit you.
where RAM usage starts to be a problem is when you see programs or tabs being closed or crashing. tabs can crash for other reasons, but the times that i have seen it happen were due to the OS forcefully killing the process that managed that tab. linux in particular has what is called the "OOM killer" that will list all running processes on the system, sort them according to memory usage over the program's running time (i.e a program that is using 1 GB of RAM and has only been running for 1 minute would score higher than a program that is using 1 GB of ram but has been running for an hour), and kill the high scorers until memory usage is back in check. if that happens to you regularly, a temporary solution could be to increase swap space, but the simplest and best one would be to get more physical RAM.
if you are interested in learning more about how it ACTUALLY works, Operating Systems: Three Easy Pieces is a great book that can be found online for free that goes into how it works on a very low level. looking up stuff related to linux directly is also a good way to learn more due to the kernel being open source.
[link] [comments]