Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
- Managed C++
- Visual Basic 4 / 5 / 6
- linked list
- hello world
What is the advantage of paging?
Posted 13 September 2010 - 05:30 AM
So the question I want to ask is, first of all, am I right in my understanding of virtual memory and paging? Second, what advantage does paging serve and how does it improve the performance of a computer?
Posted 13 September 2010 - 06:32 AM
Basically correct. The RAM we have in our computers is called physical memory. We have a certain amount of it. It is contiguous and is addressed from 0 to the maximum we have (1GB, 2GB, 4GB, ...). The MMU (Memory Management Unit) is the responsible to fragment this contiguous space into pages and map each page to a position in the virtual address space (or virtual memory).
Here's my understanding: Memory is divided into fixed-size blocks called pages, which are not contiguous in memory but are treated as if they were. Page sizes are a power of 2. The operating system has the job of finding a page and returning its value to a process that requests it. It does this using the Memory Management Unit
As I've just said the MMU divides the physical memory (not the virtual memory, although it might be seen as blocks of the same size than a page) into pages and stores a mapping table between physical addresses and virtual addresses (of course, virtual addressess where the pages can be mapped are multiples of the page size)
the Memory Management Unit, a hardware component that divides virtual memory into pages and stores their virtual addresses.
Yes, the TLB contains the most recently used entries of the map table between physical and virtual addresses to speed up memory accesses.
The MMU caches virtual addresses in the Translation Lookaside Buffer (TLB), which is a cache stored in the CPU cache that holds entries from the page table. The page table is content-addressable memory (CAM), which is the hardware equivalent of an associative array, and it contains mappings of virtual addresses to physical addresses. When memory is requested from CAM, it searches its entire table and then returns a value.
Basically, you are right. There are a lot more details about paging and caches, but the basic concepts are correct.
So the question I want to ask is, first of all, am I right in my understanding of virtual memory and paging?
Paging has many advantages. First of all, paging allows you to relocate memory areas to other places where they are more useful. For example, any operating system must divide memory in two areas, one for the operating system itself (OS, data, management tables, ...) and one for the user programs. The area for the OS must be subdivided into areas to store each driver and its data. The area for the user must be subdivided to store each program and its data.
Second, what advantage does paging serve and how does it improve the performance of a computer?
If you do not have any virtual memory, you must precalculate the needed space for each program. For example you reserve from address 1000 to 2000 for the first program. The second program is stored between 2000 and 3000, and so on. What happens if the first program needs to allocate more memory than it was initially reserved ? will you deny the request, even if the area between 3000 and the end of memory is available ? or will you allow that programs have memory fragments dispersed through all the memory space ? Another problem is that each program could "see" the memory of the other programs.
Obviosly this is not good for any modern operating system. What paging adds in this case is:
- Space: every program has virtually all memory available
- Security: one program cannot access the memory area of other programs
For example, the first program uses 10 pages, and the operating system allocates these pages at address 1000. Another program is loaded. It uses 20 pages. The operating system removes the 10 pages used by the first program from the virtual address space and allocates the new 20 pages at address 1000 (the same address of the first program). When the operating system decides to switch the current process, it removes its pages from memory and remaps the pages of the other program.
If one of the processes requests more memory, the operating system searches a new free physical page and maps it just after the last used virtual page. Thus, the process sees a contiguous memory space all the time.
Another advantage of having pages is that you can assign rights independently to each page. So you can define some pages that the program can read but not write, other pages that cannot contain executable code, pages that are reserved for private use of the operating system...
Another advantage of paging is that you can have a mechanism that allows you to have more memory than you actually have (using a paging file or swap area in disk). Each page can be marked as present or not present. When some page is not used for a long period of time, the operating system writes the contents of this page to the disk and marks the page as not present. This means that the physical memory page is freed and available to other processes that need memory.
When a program tries to access a non present page, the MMU intercepts the access and tells the operating system that that page must be reloaded. Then, the OS reads the page from disk and marks it as present again so that the program can continue.
All this requires some processing time, but the advantages are worth it. Most of this things are totally transparent to the user and programs.
Posted 24 September 2010 - 07:40 PM