What is Virtual Memory? MMU, Page Tables, and more!

  Рет қаралды 33,529

Gary Explains

Gary Explains

2 жыл бұрын

All modern operating systems use Virtual Memory so that the physical memory can be used by multiple processes, each with its own virtual address space that is mapped onto physical RAM using pages.
Let Me Explain T-shirt: teespring.com/gary-explains-l...
Twitter: / garyexplains
Instagram: / garyexplains
#garyexplains

Пікірлер: 80
@casperes0912
@casperes0912 2 жыл бұрын
I wrote a kernel for my bachelor project so I've always interacted quite a bit with the x86 MMU, setting up paging, managing the TLB, setting up the segmentation system for boot purposes and then promptly ignoring it once booted and all that :P - Still fun to watch your video on the topic and get a nice refresher on it all
@panchdeosrivastava9781
@panchdeosrivastava9781 2 жыл бұрын
Bc
@Yazan_Majdalawi
@Yazan_Majdalawi 6 ай бұрын
I'm planning to do the same as my graduation project, any advice?
@ac695
@ac695 2 жыл бұрын
I absolutely love your channel, made me interested in computer architecture so much that I've decided to major in Computer science. I would be really grateful if you could do a series on operating systems and distributed systems. Thanks again for the awesome content!!
@abhishekdas8695
@abhishekdas8695 2 жыл бұрын
I am working on verifying a riscv processor's MMU. Excellent video, brings back the memories of going through the spec and drawing out the multi level paging.
@mips-m
@mips-m 2 жыл бұрын
Nice! Better explanation than my college professor.
@GaryExplains
@GaryExplains 2 жыл бұрын
Wow, thanks!
@AbhishekMishra-sm1lg
@AbhishekMishra-sm1lg 2 жыл бұрын
Watching your videos are enough to answer basic questions on Operating System and Computer Networks in interviews.
@logicalfundy
@logicalfundy 2 жыл бұрын
What gets me is when I see some comment in a gaming forum of somebody saying they turned off Virtual Memory. Not really possible in a modern OS, what you really did was to tell the OS you don't want a page file. Things like virtual machines and running 32 bit software on a 64 bit OS need virtual memory to function properly, regardless of whether you have a swap file.
@dansmith4842
@dansmith4842 7 ай бұрын
Probably talking about using hard drive as virtual memory which windows does on low available ram
@muddyexport5639
@muddyexport5639 2 жыл бұрын
Thanks yet again. Looking forward to the next installment (as usual).
@middleclasspoor
@middleclasspoor 2 жыл бұрын
Great refresher! Than you sir!
@RamLaska
@RamLaska 2 жыл бұрын
Holy cow! I've been into computers for 40 years, but had NO idea that paging and MMUs were THAT complex. I didn't even know what I didn't know. I remember that the 386 & 030 were the first processors with built-in MMUs. What were the first processors with multi-level paging??
@jop003
@jop003 2 жыл бұрын
Great information, thank you!!!
@jellytabby2135
@jellytabby2135 Жыл бұрын
Woah this was super helpful! Thanks!
@CloudContext
@CloudContext 2 жыл бұрын
Great explanation Gary. Thank you!
@GaryExplains
@GaryExplains 2 жыл бұрын
Glad it was helpful!
@parimi001
@parimi001 Ай бұрын
Loved this video
@yaBoyDreamer
@yaBoyDreamer 2 жыл бұрын
The legend delivered! :D
@juliajin4732
@juliajin4732 Жыл бұрын
Awesome explanation!
@GaryExplains
@GaryExplains Жыл бұрын
Glad you liked it!
@balinthuszar3552
@balinthuszar3552 2 жыл бұрын
Perfect timing 🙂 I'm gonna have a test on this topic on Friday 😅
@jenkinssthomson8879
@jenkinssthomson8879 2 жыл бұрын
Great stuff!
@PHPtutorialRobbe
@PHPtutorialRobbe Жыл бұрын
Good explanation! Thanks :-)
@byronwatkins2565
@byronwatkins2565 2 жыл бұрын
The bulk of MMU is just a RAM. To substitute 10b you need 10 address lines and 10 data lines. The OS writes the re-mapping into the RAM so that the processor's address lines provide the RAM's address and the RAM's data becomes the memory's new address lines. There are also cycle skips to allow for MMU fetches when the output changes, interactions with cache, etc. MUCH of memory space does not need to change: ROM, peripherals, and OS RAM can be mapped permanently. One MCU address bit could be dedicated to this static purpose and the system ROM (or flash) could store this static memory map.
@thisisagreatidea3061
@thisisagreatidea3061 2 жыл бұрын
Thanks for the memories! :)
@wlcrutch
@wlcrutch 5 ай бұрын
Can you please teach my Operating Systems class?? Wow...Thanks Gary!
@TheSulross
@TheSulross 2 жыл бұрын
the Intel x86 and ilk hardwire designed the page table mechanism where the lookup pages need to be organized just so so. whereas there are other CPU architectures where are provided a hardware interrrupt for a page access that needs to be resolved and one writes a software interrrupt handler - that leaves the paging data structure(s) wide open so on 64-bit systems can implement a paging system that is better geared to the extremely sparse page utilization of a 64-bit address space and not get ocerwhelmed by page tables overhead. Of course a TLB is still present to make things more efficient. And some CPUs have TLB entries that can be tagged with a process ID which can be used to make process context switching and multi-threading more efficient - vs just blindly purge all TLB and replace on a process context switch. And some systems allow for jumbo page sizes that are megabytes in size as can make mapping code pages more efficient.
@BGBTech
@BGBTech 2 жыл бұрын
I have implemented a processor for such an architecture (with a Software Managed TLB). And yes, much above 2 or 3 levels, a traditional nested page tables start to have serious issues in terms of memory overhead (particularly with sparse address spaces; in my case, the processor supports a virtual address space up to 96 bits, but the overheads when doing ASLR with an 8-level page-table is absurd). Had tested various options, and curiously one of the better-performing options here seems to be AVL Trees (I had compared these both with Hash Tables and B-Trees), along with a few "hybrid" options (such as combining the lower level of a page table with the upper levels as an AVL tree). A hash table has simpler up-front logic, and is faster for a small space, but doesn't scale up particularly well. Chain-hashes are bottlenecked by the size of the front-end hash, whereas direct hashes don't resize cleanly (expanding the hash typically requires re-hashing its contents). Even with a 2048-entry hash on the front-end, the "break even point" (past which point the AVL tree became faster) wasn't all that large. B-Trees seem better in-theory (and in many other contexts), but in testing were slower and had higher memory overhead than the AVL Tree nodes in this case. This is likely due the particular consequences of the (fairly large) 96-bit virtual address space (B-Trees would have more of a space advantage with 48 or 64 bit addresses than with 96 bit addresses). B-Trees could still potentially have an advantage though in that nodes can be paged-out to swap in a "sane" way (unlike either AVL Trees or Hash Tables), assuming the use of page-sized B-Tree nodes. One could potentially also stick all the processes in the OS into a single large B-Tree (by treating the PID/ASID as part of the B-Tree's lookup key). ...
@TheSulross
@TheSulross 2 жыл бұрын
@@BGBTech paging seemed so cool and straight forward back when the 32-bit 80386 came out where the MMU and CPU got hitched, but then in the 64-bit world, it's technique doesn't really translate forward very well maybe one thing to do is just throw more TLB caching at the problem - to mitigate those deeper multilevel lookups? but you're probably tracking in the right direction - exploring alternative data structures
@BGBTech
@BGBTech 2 жыл бұрын
@@TheSulross The problem isn't really speed, but rather "sparseness". With a much bigger address space, and more page-table levels, most of the pages in the upper levels of the page table end up with maybe only a few entries "actually holding anything", and "a whole lot of zeroes", wasting considerable amounts of RAM in the process (at 2 or 3 levels, it isn't too bad, but at 8 levels, it gets "real bad"; and "level skipping" tricks don't really work with ASLR). With both B-Tree or AVL-Tree designs, one can simply skip storing these vast expanses of zeroes (albeit the computational cost of the page lookup is a little higher). The per-page overhead is also a bit higher (say, 28 or 32 bytes rather than 8 bytes), but the space needed for the page-table as a whole is reduced considerably (due mostly to avoiding the "vast expanses of zeroes"). This is why I had also experimented with hybrid trees, which keep the bottom level as a traditional page-table. These can be a little faster, but only "win" in terms of storage space if (on average) the bottom level page tables are more than around 25-33% full. Otherwise, B-Tree or AVL-Tree is smaller. B-Tree has a lower per-page cost than AVL, but tends to end up losing "on average" in terms of storage overhead due to node related overheads (B-Tree nodes are much bigger and tend to end up only around half-full due to "splitting"). In my implementation, the per-page cost was close enough (28B per B-Tree entry, 32B per AVL node), that this caused the balance to lean in favor of the AVL Trees. But, ability to page-out B-Tree nodes could still give them the win. Both tree types have a roughly O(log2 n) lookup cost, where 'n' in this case is the total number of pages allocated into the address space. Chain hashing is faster for "small n", but the break-even point was low enough, and the speed difference small enough, that I felt it "wasn't really worth the bother" in this case.
@sudajared
@sudajared Жыл бұрын
Very interesting stuff
@pa1kumarbhukya
@pa1kumarbhukya 2 жыл бұрын
If only my teachers explained me , this good.
@LeonisYT
@LeonisYT 2 жыл бұрын
In all of my recent classes I've learned more about real life applications of tech and how it works from watching this guy, level1techs/Linux and others of the same sort. I've been able to apply then to where I work now even.
@pa1kumarbhukya
@pa1kumarbhukya 2 жыл бұрын
@@LeonisYT Thats great .
@ognjenjakovljevic494
@ognjenjakovljevic494 2 жыл бұрын
95% of teachers dont like their work and just appear to class to earn paycheck, but still there is the 5% and youtube ppl who like their job :D
@pa1kumarbhukya
@pa1kumarbhukya 2 жыл бұрын
@@ognjenjakovljevic494 true
@eyeshezzy
@eyeshezzy 2 жыл бұрын
Explain Gary, Explain!
@nkarthik12
@nkarthik12 10 ай бұрын
Simply explained.Can u also how software management of mmu
@bhuvankiran3922
@bhuvankiran3922 2 жыл бұрын
I'm waiting for Speed test G videos, on the latest chips😟😟😟
@aakashbishnoi7947
@aakashbishnoi7947 2 жыл бұрын
Sir when are we going to see speed test g videos for snapdragon gen 1
@SomeRandomPiggo
@SomeRandomPiggo 2 жыл бұрын
ayy! i remember a comment asking about this
@falandobadheeu8801
@falandobadheeu8801 2 жыл бұрын
Gary What happened to your speed test G series ? Haven't seen a speed test for a long time !
@hpgramani
@hpgramani 2 жыл бұрын
Any plans for videos on Analog computers?
@EyesOfByes
@EyesOfByes 2 жыл бұрын
9:45 So I guess this is why 3DXpoint (Optane) is so responsive?
@guilherme5094
@guilherme5094 2 жыл бұрын
👍
@rashie
@rashie 2 жыл бұрын
👍👍
@Geek_alien
@Geek_alien 6 ай бұрын
What's your thoughts on "Extended RAM" often found in the Settings of every android?
@thaernejem7317
@thaernejem7317 2 жыл бұрын
Multi tasking please!
@GaryExplains
@GaryExplains 2 жыл бұрын
I have covered multitasking in several videos, try my videos on "Multitasking vs Multithreading vs Multiprocessing" and my two videos on Piccolo OS.
@thaernejem7317
@thaernejem7317 2 жыл бұрын
@@GaryExplains I dont miss any of your videos! I will look it again. Thank you
@chidorirasenganz
@chidorirasenganz 2 жыл бұрын
As far as I’m aware iOS hasn’t had virtual memory (excluding iPadOS 16 adding it for m1 iPad) but instead uses jetsum which boots inactive apps from memory once it’s full
@GaryExplains
@GaryExplains 2 жыл бұрын
Don't confuse virtual memory and swapping.
@chidorirasenganz
@chidorirasenganz 2 жыл бұрын
@@GaryExplains ahh thanks for the clarification
@JarppaGuru
@JarppaGuru 2 жыл бұрын
4:00 yes yes its just location on physical memory. if this is c64 then you cant have more memory use than 64k so u cant have "virtual" memory. it have to point somewhere there is no virtual memory it saved for somewhere. can be in air LOL. so it is just physical memory OR hard disc, but it have to be written somewhere. not in magically virtual air memory lol yes aplication start it will have address base. that virtual space. no other program can use that. if can you can hack xD
@Flankymanga
@Flankymanga 2 жыл бұрын
Isn't the soft page fault also about dynamic memory allocation? Essentialy a program has mapped only certain amount of physical memory to save resources and when he needs more then another couple of pages are allocated in the physical memory and marked as mapped in the MMU?
@GaryExplains
@GaryExplains 2 жыл бұрын
Yes, that is true. Depending on OS, the C runtime and the size of the malloc. Such requests can be satisfied by asking the OS for new pages which are allocated lazily, no actual page is allocated until the first access (which then causes the soft fault and forces the OS to allocate the actual pages and update the MMU).
@Flankymanga
@Flankymanga 2 жыл бұрын
@@GaryExplains thanks for clarification!
@petevenuti7355
@petevenuti7355 Жыл бұрын
Is there a mmu driver that will allow one to address more "ram" then physically possible in a given machine? For instance, my motherboard has 2mb supports 8gb, app needs 16gb, i want 32gb available... App balks at increased swap file, wants raw ram... Is there a way to override normal mmu function to trick the os and app into treating an ssd or other hardware AS real ram?
@LouieC
@LouieC 2 жыл бұрын
Is virtual memory what Samsung's RAM Plus is?
@AbdulMajeedHamid
@AbdulMajeedHamid 2 жыл бұрын
Please can you talk about kvm on Android and I have seen someone run full win 11 on the new pixel phone us virtual OS
@jadhal6649
@jadhal6649 2 жыл бұрын
Is virtual memory is paging file in windows?
@jengelenm
@jengelenm 2 жыл бұрын
We once had a “Virtual memory full” or something like that in a customer’s Windows XP computer. Checked the performance and needed to uninstall some application (driver) for a wifi dongle. It was drawing all that memory! Guess the windows version was too old for the newer hardware driver….
@android199ios25
@android199ios25 2 жыл бұрын
So if process A has virtual address 123 and process B has virtual address 123 how does the MMU give the right real address?
@GaryExplains
@GaryExplains 2 жыл бұрын
The kernel manages the page tables.
@android199ios25
@android199ios25 2 жыл бұрын
@@GaryExplains So all the logic is done by the kernel and MMU is just for lookup? Does each page table have some sort of id of the process to which it belongs? Thnx for the answer and keep uploading such enjoyable content :)
@GaryExplains
@GaryExplains 2 жыл бұрын
Yes, yes, and my pleasure.
@wayland7150
@wayland7150 Жыл бұрын
Virtual Memory is very last decade. I'm pretty sure most computers don't bother with it now.
@GaryExplains
@GaryExplains Жыл бұрын
LOL, no. If you think that then it means you didn't understand the video at all.
@tonysheerness2427
@tonysheerness2427 2 жыл бұрын
To speed things up they have a TLB cache, to me it all seems like a paper chase. Why not use the TLB as memory instead of a look up table. For all this work you need a minimum amount of resources just to handle memory. The computer scientists know what they are doing. It would nice if you could switch this off and see how fast it would run if pages in memory were directly mapped to page frames.
@GaryExplains
@GaryExplains 2 жыл бұрын
I guess you need to go back and check out the section on how large the tables are.
@BGBTech
@BGBTech 2 жыл бұрын
@@GaryExplains FWIW: In my case, I had designed a custom CPU (on an FPGA), and had used a Software Managed TLB. In such a design, the OS is responsible for loading pages into the TLB on behalf of the program (using TLB Miss interrupts instead of Page Fault interrupts). I had also ended up going with 16K pages, partly as these worked out as a "local optimum" in terms of overhead (vs either 4K or 64K pages). Main reason for doing it this way was mostly cost reasons (raising an interrupt on TLB Miss being cheaper than having a hardware page walker). Because the page-table walking is done in software, one isn't necessarily limited to conventional nested page tables. Had done experiments also with Hash Tables, B-Trees, and AVL Trees for page management. Curiously, the AVL Trees do surprisingly well here (for sparse address spaces and ASLR, they can use significantly less memory than traditional page-tables). B-Trees and Hash Tables came close, but had other drawbacks. Debugging all this stuff has thus far been kind of a massive pain though...
@goldnoob6191
@goldnoob6191 2 жыл бұрын
A clever way to waste ressources that you mitigate 30 years later allowing 1Mb and 4Gb pages.. Indeed It's usefull !
@lusher00
@lusher00 Жыл бұрын
I don’t know why I found this intimidating. There’s clearly nothing to it.
@power-max
@power-max 2 жыл бұрын
6:17 why are you wasting an entire bit per mapping for not allocated memory? In the case where it is zero, all the address bits become "don't cares" Why not just have 0x0 be a special NULL memory that is like /dev/null? Then any address not 0x0 be a valid mapping?
@BGBTech
@BGBTech 2 жыл бұрын
With the dedicated bit, you can use the remaining bits (for non-valid pages) to encode the location of pages which have been paged out to the swapfile or similar (and use the other bits to remember the page access mode once it is pulled back in from the swap file, ...). This is rather useful, and couldn't be done with a scheme like that described.
@peter-eh2oq
@peter-eh2oq Жыл бұрын
where is virtual memory explanation?
@GaryExplains
@GaryExplains Жыл бұрын
Eh?
@mplovecraft
@mplovecraft 2 жыл бұрын
"We are Meta are now offering you - Virtual Memory! Missing Granny? You can now visit your lost near and dear ones here in the Metaverse as 3D representations of their most Liked posts and images during all their years on facebook!". Coming to a dystopian future near you...
@Martin_Entertains
@Martin_Entertains 2 жыл бұрын
Virtual memory is memory you don't get, even though you paid for it.
But, what is Virtual Memory?
20:11
Tech With Nikola
Рет қаралды 232 М.
Best father #shorts by Secret Vlog
00:18
Secret Vlog
Рет қаралды 21 МЛН
Дарю Самокат Скейтеру !
00:42
Vlad Samokatchik
Рет қаралды 7 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 545 М.
What's Virtual Memory? - Computerphile
22:40
Computerphile
Рет қаралды 175 М.
RISC vs CISC - Is it Still a Thing?
11:18
Gary Explains
Рет қаралды 179 М.
What is virtual memory? - Gary explains
11:28
Android Authority
Рет қаралды 298 М.
How a Clever 1960s Memory Trick Changed Computing
20:05
LaurieWired
Рет қаралды 268 М.
Design Your Own CPU Instruction Set
11:13
Gary Explains
Рет қаралды 88 М.
Linux Crash Course - Understanding Memory and Swap Usage
20:55
Learn Linux TV
Рет қаралды 43 М.
How does KERNEL memory allocation work? //Source Dive// 004
44:42
Low Byte Productions
Рет қаралды 46 М.
Linux Internals: Memory Management
37:56
DJ Ware
Рет қаралды 28 М.