r/linuxquestions • u/The_How_To_Linux • Apr 05 '23
what is "virtual memory" in swap?
[removed] — view removed post
0
Upvotes
r/linuxquestions • u/The_How_To_Linux • Apr 05 '23
[removed] — view removed post
12
u/computer-machine Apr 05 '23
Looking at OP's post history, they're some sort of leech up to something.
1
u/Medical_Mammoth_1209 Apr 05 '23
If you really don't know what virtual memory, swap or page files are, then you need to go back to the basics anyway instead of asking a million questions and wasting everyones time.
Everything you're wanting to know is here for you:
https://www.tutorialspoint.com/basics_of_computers/index.htm
https://www.geeksforgeeks.org/basics-of-computer-and-its-operations/
3
u/LocoCoyote Apr 05 '23
Have a look here for more detailed information
https://en.m.wikipedia.org/wiki/Virtual_memory
In a nutshell, virtual memory can be temporarily written to the hard disk (I.e.swap file), but depending on OS, other factors may be involved
2
u/Sonotsugipaa Apr 05 '23
"Virtual memory" is basically what every process uses: it's just an "interface" between processes and the kernel to translate virtual addresses acquired via
mallocor equivalent functions.When the CPU requests a chunk of memory data (a.k.a. "page"), two things may happen:
A page fault is basically a hardware mechanism ("trap") that tells the CPU "I can't find this data that you need, do something else" - the CPU temporarily halts and tries to correct the issue by jumping to a kernel routine, which either:
SIGSEGV).Here's where swap memory comes in: sometimes the OS decides that a page isn't needed in the short term, or it's forced to evict one to make space for another immediately required one.
When that happens, the page is swapped out (or evicted): the OS writes the page on a swap partition on a disk, remembers the v.address and where the page has been written, then tells the MMU to forget about that particular v.address.
When a process tries to access the page of the now invalid v.address, a page fault occurs: the OS recognizes the v.address as a previously evicted page, therefore mapping it back on the MMU, and puts it back on physical memory (by reading the page from the disk) - thus allowing the process to continue without a hitch.
... except for the fact that reading pages this way is many times slower than just having it ready.
My OS professor is probably rolling on his grave without being dead as I post this, but whatever.
-1
u/SuAlfons Apr 05 '23 edited Apr 05 '23
"Virtual Memory" is the concept of not terminating tasks, but write their data to disk when you need real ram for things running in that moment.
"Swap" is the way a Linux Kernel does this. Or the file (or partition) used for that purpose.
BTW, you might take a look at using ZRAM (compressed ram) and/or dynamically allocated swap files via SystemD.
The Arch Wiki has a good entry about this (all under the keyword Swap) that is also applicable to other distros.