Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Virtual memory
Virtual memory is a common part of most operating systems on desktop computers. It has become so common because it provides a big benefit for users at a very low cost. Most computers today have something like 4 or 6 gigabytes of RAM available for the CPU to use. Unfortunately, that amount of RAM is not enough to run all of the programs that most users expect to run at once. For example, if you load the operating system, an e-mail program, a Web browser and word processor into RAM simultaneously, 4 gigabytes is not enough to hold it all. If there were no such thing as virtual memory, then once you filled up the available RAM your computer would have to say, "Sorry, you cannot load any more applications. Please close another application to load a new one." With virtual memory, what the computer can do is look at RAM for areas that have not been used recently and copy them onto the hard disk. This frees up space in RAM to load the new application. Because this copying happens automatically, you don't even know it is happening, and it makes your computer feel like is has unlimited RAM space even though it only has 4 gigabytes installed. Because hard disk space is so much cheaper than RAM chips, it also has a nice economic benefit. The read/write speed of a hard drive is much slower than RAM, and the technology of a hard drive is not geared toward accessing small pieces of data at a time. If your system has to rely too heavily on virtual memory, you will notice a significant performance drop. The key is to have enough RAM to handle everything you tend to work on simultaneously -- then, the only time you "feel" the slowness of virtual memory is when there's a slight pause when you're changing tasks. When that's the case, virtual memory is perfect. When it is not the case, the operating system has to constantly swap information back and forth between RAM and the hard disk. This is called thrashing, and it can make your computer feel incredibly slow. The area of the hard disk that stores the RAM image is called a page file. It holds pages of RAM on the hard disk, and the operating system moves data back and forth between the page file and RAM. On a Windows machine, page files have a .SWP extension.
In the "Minimum:" box, enter the smallest amount of hard drive space you wish to use for virtual memory on the hard disk specified. The amounts are in megabytes. For the "C:" drive, the minimum should be 2 megabytes. The "Maximum:" figure can be anything you like, but one possible upper limit is twice physical RAM space. Windows default is normally 12 megabytes above the amount of physical RAM in your computer. To put the new settings into effect, close the dialog box and restart your computer. The amount of hard drive space you allocate for virtual memory is important. If you allocate too little, you will get "Out of Memory" errors. If you find that you need to keep increasing the size of the virtual memory, you probably are also finding that your system is sluggish and accesses the hard drive constantly. In that case, you should consider buying more RAM to keep the ratio between RAM and virtual memory about 2:1. Some applications enjoy having
lots of virtual memory space but do not access it very much. In that case, large paging files work well. One trick that can improve the performance of virtual memory (especially when large amounts of virtual memory are needed) is to make the minimum and maximum sizes of the virtual memory file identical. This forces the operating system to allocate the entire paging file when you start the machine. That keeps the paging file from having to grow while programs are running, which improves performance. Many video applications recommend this technique to avoid pauses while reading or writing video information between hard disk and tape. Another factor in the performance of virtual memory is the location of the page file. If your system has multiple physical hard drives (not multiple drive letters, but actual drives), you can spread the work among them by making smaller page files on each drive. This simple modification will significantly speed up any system that makes heavy use of virtual memory.
What is Fragmentation? Fragmentation of any virtual address space occurs over time as more and more blocks of memory are allocated and freed. Memory allocations can only succeed in contiguous blocks of memory, for example if a 2mb allocation was requested it would be allocated from a complete contiguous 2mb block of virtual memory and not from a 1mb block at the start of the pool and a 1mb block at the end. When a virtual address space is initially created all of it is available for allocation and requests are simply satisfied from the next available virtual address.
Fig 2 shows a single fragmentation of 20Mb. Blocks of free space in between allocations can be reused providing the requested allocation is small enough to fit. Figure 3 below shows the 20mb fragmentation reused by 2 additional 10mb allocations.
When an address space becomes very fragmented it can cause larger memory allocations to fail as there is not enough contiguous space to satisfy the request. Take Figure 4 as an example, as the address space is fairly fragmented the largest single allocation possible is 30mb, anything larger would fail even though there is a total of 80mb available for allocation in the address space. 8 allocations of 10mb would succeed where as 2 allocations of 40mb would fail. Is this a problem? Typically a user would never have problems with fragmentation, in user mode a process would need to allocate close to the 2Gb limit (x86), before experiencing any problems, it also depends on how an application developer has written their application. For example a 10kb buffer is required to perform an operation which will be repeated several thousand times. Does the developer allocate the 10kb buffer once when the program starts, or does it get allocated and freed each time it is required. The more memory allocations an application performs the more fragmented the address space can become. Memory allocation failures due to fragmentation are most often seen in the kernel mode address space specifically in the paged and nonpaged system memory pools. As these 2 pools are used by almost every component in all operations performed in kernel mode they can
become exhausted and very fragmented especially under workloads such as Terminal Services. I have seen examples of users failing to connect to a Citrix XenApp server because the ICA remote display driver could not allocate 400Kb of paged pool memory even though there was more than 30mb available at the time. What can be done? Today there is not a lot that can be done to resolve this. When memory allocations succeed a pointer to the start of the memory block is returned, if Microsoft were to implement background memory defragmentation routines, the original returned memory pointers would change as a result. There are no notification methods available in Windows to signal processes that memory allocations have moved. Windows applications assume the address given after a successful memory allocation is valid and will not change until it is freed.
Conclusion
The virtual memory functions enable a process to manipulate or determine the status of pages in its virtual address space. They can perform the following operations:
Reserve a range of a process's virtual address space. Reserving address space does not allocate any physical storage, but it prevents other allocation operations from using the specified range. It does not affect the virtual address spaces of other processes. Reserving pages prevents needless consumption of physical storage, while enabling a process to reserve a range of its address space into which a dynamic data structure can grow. The process can allocate physical storage for this space, as needed. Commit a range of reserved pages in a process's virtual address space so that physical storage (either in RAM or on disk) is accessible only to the allocating process. Specify read/write, read-only, or no access for a range of committed pages. This differs from the standard allocation functions that always allocate pages with read/write access. Free a range of reserved pages, making the range of virtual addresses available for subsequent allocation operations by the calling process. Decommit a range of committed pages, releasing their physical storage and making it available for subsequent allocation by any process. Lock one or more pages of committed memory into physical memory (RAM) so that the system cannot swap the pages out to the paging file. Obtain information about a range of pages in the virtual address space of the calling process or a specified process. Change the access protection for a specified range of committed pages in the virtual address space of the calling process or a specified process.