Linux Memory Management - Virtual Memory and Demand Paging
Linux Memory Management - Virtual Memory and Demand Paging
Memory management is one of the most complex activity done by Linux kernel. It has various concepts/issues associated with it. This article is part of our on-going UNIX kernel overview series. In the previous article of the kernel series, we discussed about the UNIX process overview, and Reentrant Kernels. In this article we will try to touch base on virtual memory and demand paging as these are some of the important concepts related to memory management.
Virtual Memory
The concept of virtual memory is one of the very powerful aspects of memory management. Since the initial era of computers the need of memory more than the existing physical memory has been felt. Over the years, many solutions were used to overcome this issue and the most successful of them has been the concept of virtual memory. Virtual memory makes your system appear as if it has more memory than it actually has. This may sound interesting and may prompt one to as how is this possible. So, lets understand the concept. To start, we must first understand that virtual memory is a layer of memory addresses that map to physical addresses. In virtual memory model, when a processor executes a program instruction, it reads the instruction from virtual memory and executes it. But before executing the instruction, it first converts the virtual memory address into physical address. This conversion is done based on the mapping of virtual to physical addresses that is done based on the mapping information contained in the page tables (that are maintained by OS).
www.thegeekstuff.com/2012/02/linux-memory-management/ 1/8
12/5/13
The virtual and physical memory is divided into fixed length chunks known as pages. In this paged model, a virtual address can be divided into two parts : An offset (Lowest 12 bits) A virtual page frame number (rest of the bits) When ever the processor encounters a virtual address, it extracts the virtual page frame number out of it. Then it translates this virtual page frame number into a physical page frame number and the offset parts helps it to go to the exact address in the physical page. This translation of addresses is done through the page tables. Theoretically we can consider a page table to contain the following information : A flag that describes whether the entry is valid or not The physical page frame number as described by this entry Access information regarding the page (like read-only, read-write etc) A page table is accessed through virtual page frame number using it as offset for entries in the page table. For example, a virtual page frame number of 2 points to the entry 1 in the page table (the entry numbers begin with 0 ). In the image below, VPFN stands for Virtual page frame number, and PFN indicates the physical page frame number.
It may happen that a processor goes to a processes page table entry with a virtual page frame number and finds the entry as invalid. In this case it is the processors responsibility to pass the control to kernel and ask it to fix the problem. Different processors pass the control in different ways but this phenomenon is known as a page fault. But if the entry was valid then processor takes the physical page frame number, multiplies with the size of the page to get the base address of the physical page and then adds the offset to get to the exact physical
www.thegeekstuff.com/2012/02/linux-memory-management/ 2/8
12/5/13
address. So now we understand that through the concept of virtual memory, each process thinks that it has all range of virtual address at its disposal and hence this concepts make the system appear as if it has more physical memory than actually available.
Demand Paging
In the previous sectioned we learned that if the processor goes to the processes page table with a virtual page frame number for which no entry was present in the table then two cases arise. 1. Either the process has tried to access an invalid memory address 2. The physical page corresponding to the virtual address was not loaded into physical memory Out of the two cases above, the case 1 is the case where the process tries to memory address which it is not allowed. In this case a page fault is generated and the kernel terminates the process. While in case 2, as already explained, the physical page corresponding to the virtual address is not yet loaded into physical memory. In this case also a page fault is generated and the kernel then tries to bring the required memory page into physical memory from hard disk. Since this operation of bringing a page from hard disk into physical memory is time consuming so by this time a context switch between processes happens and some other process is brought into execution. Meanwhile the page of the earlier process is brought into physical memory and the page tables are updated and then this process is brought back into execution again from the same instruction that caused the page fault. This is known as demand paging where all the memory pages corresponding to a process are not present in the physical memory at any given time. This saves the physical memory from clogging up with non-required memory pages while when necessary these pages can be brought into physical memory through page fault (as explained above).
32 Tw eet Like 11
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you dont know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator. Get the Linux Sysadmin Course Now!
Awk Introduction 7 Awk Print Examples Advanced Sed Substitution Examples 8 Essential Vim Editor Navigation
3/8
12/5/13
Debugging Tools 4. Mommy, I found it! 15 Practical Linux Find Command Examples 5. Linux 101 Hacks 2nd Edition eBook
Fundamentals 25 Most Frequently Used Linux IPTables Rules Examples Turbocharge PuTTY with 12 Powerful AddOns
{ 6 comments read them below or add one } 1 Narendra February 17, 2012 at 8:49 am Very good article, keep writing. 2 Himanshu February 17, 2012 at 11:26 am @Narendra Thanks 3 Ansil Hameed February 20, 2012 at 7:22 am Simple and clearwell done .expecting more 4 Dan Saint-Andre March 1, 2012 at 10:52 am I would appreciate a similar discussion of the device detection, driver identification, module loading, new device event processing. This is what happens in upstart or systemd as devices come and go. We take this behavior for granted, but I have seen precious little that walks through how it is supposed to happen. I believe, too, that there are variations on behavior that depend on distribution idiosyncrasies. ~~~ 8d;-/ Dan 5 Haroun Ansari February 8, 2013 at 2:24 am Good work .. Well explained 6 KK February 24, 2013 at 7:20 am I would like to see with practical example. Leave a Comment Name
www.thegeekstuff.com/2012/02/linux-memory-management/ 4/8
12/5/13
E-mail Website
Previous post: XSS Attack Examples (Cross-Site Scripting Attacks) Next post: 5 Practical Linux fuser Command Examples RSS | Email | Twitter | Facebook | Google+
Search
COURSE
Linux Sysadmin CentOS 6 Course - Master the Tools, Configure it Right, and be Lazy
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library 50 UNIX / Linux Sysadmin Tutorials 50 Most Frequently Used UNIX / Linux Commands (With Examples) How To Be Productive and Get Things Done Using GTD
www.thegeekstuff.com/2012/02/linux-memory-management/ 5/8
12/5/13
30 Things To Do When you are Bored and have a Computer Linux Directory Structure (File System Structure) Explained with Examples Linux Crontab: 15 Awesome Cron Job Examples Get a Grip on the Grep! 15 Practical Grep Command Examples Unix LS Command: 15 Practical Examples 15 Examples To Master Linux Command Line History Top 10 Open Source Bug Tracking System Vi and Vim Macro Tutorial: How To Record and Play Mommy, I found it! -- 15 Practical Linux Find Command Examples 15 Awesome Gmail Tips and Tricks 15 Awesome Google Search Tips and Tricks RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams Can You Top This? 15 Practical Linux Top Command Examples Top 5 Best System Monitoring Tools Top 5 Best Linux OS Distributions How To Monitor Remote Linux Host using Nagios 3.0 Awk Introduction Tutorial 7 Awk Print Examples How to Backup Linux? 15 rsync Command Examples The Ultimate Wget Download Guide With 15 Awesome Examples Top 5 Best Linux Text Editors Packet Analyzer: 15 TCPDUMP Command Examples The Ultimate Bash Array Tutorial with 15 Examples 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id Unix Sed Tutorial: Advanced Sed Substitution Examples UNIX / Linux: 10 Netstat Command Examples The Ultimate Guide for Creating Strong Passwords 6 Steps to Secure Your Home Wireless Network Turbocharge PuTTY with 12 Powerful Add-Ons
CATEGORIES
Linux Tutorials Vim Editor Sed Scripting Awk Scripting Bash Shell Scripting Nagios Monitoring OpenSSH IPTables Firewall Apache Web Server MySQL Database Perl Programming Google Tutorials Ubuntu Tutorials PostgreSQL DB
www.thegeekstuff.com/2012/02/linux-memory-management/ 6/8
12/5/13
Hello World Examples C Programming C++ Programming DELL Server Tutorials Oracle Database VMware Tutorials Ramesh Natarajan
Follow
My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.
Support Us
Support this blog by purchasing one of my ebooks. Bash 101 Hacks eBook Sed and Awk 101 Hacks eBook Vim 101 Hacks eBook Nagios Core 3 eBook
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!. Follow us on Google+ Follow us on Twitter Become a fan on Facebook
www.thegeekstuff.com/2012/02/linux-memory-management/ 7/8
12/5/13
www.thegeekstuff.com/2012/02/linux-memory-management/
8/8