You are on page 1of 2

Real and Virtual Memory Real memory refers to the actual memory chips that are installed in the

computer. All programs actually run in this physical memory. However, it is often useful to allow the computer to think that it has memory that isn't actually there, in order to permit the use of programs that are larger than will physically fit in memory, or to allow multitasking (multiple programs running at once). This concept is called virtual memory. The way virtual memory works is relatively simple. Let's suppose the operating system needs 80 MB of memory to hold all the programs that are running, but there are only 32 MB of RAM chips installed in the computer. The operating system sets up 80 MB of virtual memory and employs a virtual memory manager, a program designed to control virtual memory, to manage the 80 MB. The virtual memory manager sets up a file on the hard disk that is 48 MB in size (80 minus 32). The operating system then proceeds to use 80 MB worth of memory addresses. To the operating system, it appears as if 80 MB of memory exists. It lets the virtual memory manager worry about how to handle the fact that we only have 32 MB of real memory. Of course, not all of the 80 MB will fit in the physical 32 MB that exist. The other 48 MB reside on the disk, in the file controlled by the virtual memory manager. This file is called a swap file. Whenever the operating system needs a part of memory that is currently not in physical memory, the virtual memory manager picks a part of physical memory that hasn't been used recently, writes it to the swap file, and then reads the part of memory that is needed from the swap file and stores it into real memory in place of the old block. This is called swapping, for obvious reasons. The blocks of memory that are swapped around are called pages. Virtual memory was a very important invention in computing, as it allows multitasking as we know it today. Without virtual memory, you couldn't run a spreadsheet, word processor and database program at the same time unless you had enough memory to hold all of them at once, because you would constantly be running out of memory and having to shut down program "A" in order to open program "B". Most PCs, when running multitasking operating systems like Windows 95, are using virtual memory. However, virtual memory can also hamper performance. The larger the virtual memory is compared to the real memory, the more swapping has to occur to the hard disk. The hard disk is much, much slower than the system memory. Trying to use too many programs at once in a system with too little memory will result in constant disk swapping, called thrashing. Thrashing can cause system performance to slow to a crawl. It is caused especially when trying to use an operating system with a lot of system overhead, such as Windows NT, on a computer with insufficient system RAM. If you try to run Windows NT on a PC with only 16 MB, the operating system will have to swap almost constantly whenever you run an application, because Windows NT itself needs about 12-16 MB to run. The PC can't hold NT and the application you are trying to use in memory at the same time, and it will thrash wildly swapping system tasks and application tasks back and forth from the hard disk. In a real system, it's important to carefully manage how the total memory in the system is used. Some systems try to improve performance through the use of disk caching. As they say, the road to performance hell is often paved with good intentions. In some cases, the system will reserve so much of system memory for caching disk accesses, that the remaining memory isn't enough and thrashing will occur. So you are using part of disk as virtual memory, and part of your memory as virtual disk. Talk about self-defeating! See this section of the Optimization Guide for ideas on optimizing virtual memory and the swap file.

Difference between virtual memory consumption and real memory with Memory Monitor on iOS
I am stuck with an issue in my app. I have been testing up until now mostly on my iPad 3 with occasional checks on my iPad 1 to make sure all is well. I am playing a UIImageView animation in my app and it keeps quitting with only a "Received memory warning" message before it quits. I have been using the Object Allocations tool in instruments but according to that, my memory usage is way low. So after researching for a bit I came across this post by Mr. Larson: Now I am using the Memory Monitor tool instead. However, I don't understand why I am quitting. The 1st gen. iPad has 256MB of ram. Now I know I can't use all of that...some say you shouldn't use more than 100MB. Is that real memory though, or virtual memory...or maybe some combination? My real memory is consistently between 20 - 25MB but the virtual is around 190 - 205MB when it crashes. Here's a screenshot:

Virtual memory
In computing, virtual memory is a memory management technique that is implemented using both hardware and software. It maps memory addresses used by a program, called virtual addresses, into physical addresses in computer memory. Main storage as seen by a process or task appears as a contiguous address space or collection of contiguous segments. The operating system manages virtual address spaces and the assignment of real memory to virtual memory. Address translation hardware in the CPU, often referred to as a memory management unit or MMU, automatically translates virtual addresses to physical addresses. Software within the operating system may extend these capabilities to provide a virtual address space that can exceed the capacity of real memory and thus reference more memory than is physically present in the computer. The primary benefits of virtual memory include freeing applications from having to manage a shared memory space, increased security due to memory isolation, and being able to conceptually use more memory than might be physically available, using the technique of paging.