Using games as an example - I could be playing a game which takes up 30GB of storage on my HDD, but the game could be only using 2GB of memory. Obviously the game isn't loaded all at once, but is the RAM usage equal to how much data is being read + used from the HDD?
tl;dr Why does a game using 2GB memory actually use 2GB memory, what is the deciding factor?
Answer
"is the RAM usage equal to how much data is being read + used from the HDD?" No, it is not that simple. Today's operating systems all use virtual memory. The RAM the program uses will almost always be a subset of the total virtual address space the program defines, for two reasons.
First, because the program will almost never access all of the v.a.s. it defines. For example, suppose you never explore a particular corridor on a game level - there's no reason to bring that data in from disk. Or if you don't happen to use the numbered list feature in Word, there's no reason to bring in that code. Code and data is generally "demand paged", meaning it is only paged in if it's actually referenced ("demanded") by the program.
Second, even after stuff has been paged in, the operating system may decide that something else can make better use of some of the RAM and page some of it out - particularly if it hasn't been referenced for a while, and especially on systems with small amounts of RAM for their workload (called "RAM pressure"). This too can apply to both code and data separately. A naive example would be the data for a level you haven't been to for a while.
The result of the above is that the amount of RAM a program "uses" is really better thought of as the amount of virtual address space the program has referenced recently, limited further by what the OS allows the program to use. It can vary widely depending on how much RAM you have, what else is running on the system, and what the program is doing.
tl;dr: It's complicated. The "memory management" chapter of Windows Internals, 6th. ed., is about 200 pages, which is a fair-sized book all by itself.
No comments:
Post a Comment