There are hundreds of processes but only a few g of memory. Please explain how these hundreds of processes share memory.

  html5, question

I feel that the “memory occupied” by each process should add up to more than the physical memory.
What kind of memory management and scheduling can make these hundreds of processes seem to run together?

Ha ha, this is exactly the problem that virtual memory should solve.
The memory allocated by the process is actually virtual memory, and the actual physical memory will not be allocated immediately. The physical memory will only be allocated abnormally through missing pages during access, so the total amount of virtual memory occupied by all processes can exceed the physical memory, but the total amount of physical memory cannot. In the top command result, VIRT and RES represent the usage of virtual memory and physical memory respectively.
Regarding scheduling, cpu time division multiplexing allocates time slices to each process according to a certain algorithm. In short, each process runs for a short period of time at intervals. Due to the small granularity of time slices (usually ms level), the perception of users and last service is usually not obvious.

The same physical page can be mapped to the address space of different processes. For example, if multiple versions of programs use the same Visual C++ runtime DLL, then read-only data is shared. A process’s private memory is actually occupied.

In addition, the system will swap temporarily unused memory (such as the memory of the program whose window is being minimized) to the virtual memory swap file on the disk until the next time the program remembers to use it (such as the window is being restored). The available memory on a machine should be physical memory visible to the operating system (some memory may not be available for 32-bit Windows due to address space limitations)+virtual memory.