The difference between swap, virtual memory, and page in Linux

Source: Internet
Author: User

To understand this, first you need to know what is protected mode and real mode.

The previous operating system was a real mode, such as DOS. Each time only one process is running, this process uses all the physical memory.

Later developed to the protection model, time-sharing multi-process. A CPU runs multiple processes, but the process does not know exactly how much memory is available and it can access the maximum memory address. For example, a 16-bit system can access the 2^16BYTE,32 bit is the 2^32 bit. But actually not that much memory? What to do? The protection model was born.

Assuming the process is a Liu Xiang, the referee (System) he began to run as soon as he started. But the referee said to give you 1 seconds, you can run 100 meters. So Liu Xiang began to run (memory address), a second after Liu Xiang ran only 10 meters, the referee whistle said: "Liu Xiang you take a break, I will go to Wang Junxia whistle it, now remember where you go (protection), and so will start from here." Referee to Wang Junxia whistle, one run a second, back to Liu Xiang blow whistle again run 1 seconds, so reciprocating. Few people start running, Liu Xiang can also stand on the runway to rest. The runners were running more and more, and the runways were full of people, so they could only move some people to the lawn next to the runway to rest (in exchange). Later found that some people disorder lying, occupy a lot of space, so that everyone can only stand (page), so that can accommodate a lot of people.

Swap-the meadow, which is the hard disk space to store page.

Virtual memory-Assuming Liu Xiang ran the N-circle is already the distance to Beijing, but they are still in situ. There is no such thing as virtual.

Page-a lattice on the grass that accommodates only one person at a time.

In order to improve disk access efficiency, Linux has done some careful design, in addition to the Dentry cache (for VFS, speed up the file path name to Inode conversion), but also adopted two main cache methods: Buffer cache and Page Cache. The former is for disk block read and write, the latter for the file inode read and write. These caches effectively shorten the time for I/O system calls (such as read,write,getdents).

Memory activity can basically be quantified with 3 numbers: The total amount of active virtual memory, Exchange (swapping) rate and paging (paging) rate. The first number indicates the total memory demand, and the last two digits indicate how much of the memory is in use. The goal is to reduce memory activity or increase the amount of storage until the paging rate remains at an acceptable level.

Total Active virtual memory (VM) = actual memory size (size of real memories) (physical memory) + Swap space Used (amount of swap spaces used)

When the program needs more memory than the physical memory, the Linux system uses paging mechanism, that is, the system copy some in-memory pages to disk, freeing up space for the process to use.

Most systems can tolerate occasional paging, but frequent paging can drastically degrade system performance.

Linux memory management: Linux system through 2 methods of memory management, "paging algorithm", "Switching technology."

Paging algorithm is to swap the most recently used pages in memory to disk, and keep the Frequently used pages (active pages) in memory for the process to use.

Switching technology is the system that swaps the entire process, not part of the page, to the disk. Under normal circumstances, some exchange processes occur.

When memory is critically low, paging and swapping are used frequently, which increases the load on disk I/O. It further reduces the execution speed of the system to the job, that is, the system I/O resource problem will affect the allocation of memory resources.

Virtual Memory for Linux

Linux virtual memory is a very complex subsystem, it realizes the transparency of inter-process code and data sharing mechanism, and can allocate more memory than the system's existing physical memory, some operating system virtual storage can even affect the performance of the file system by providing caching function. Different styles of Linux virtual storage implementation is very different, but can not be separated from the following 4 concepts.

1: Actual memory

Actual memory refers to the physical memory that is actually present in a system, called RAM. Real memory is the fastest and most efficient way to store temporary data, so it must be distributed to the application as much as possible, and there are now several forms of RAM: SIMM, DIMM, Rambus, DDR, and many more RAM can use error correction mechanism (ECC).

2: Swap Space

Swap space is a piece of disk space dedicated to temporarily storing memory, typically used when paging and exchanging process data, and it is generally recommended that the swap space should be two to four times times the size of physical memory.

3: Page Scheduling

Page scheduling refers to the transfer of data from disk to memory, and the reverse process, which is called page scheduling, because Linux memory is divided evenly into pages of equal size, usually page size is 4KB and 8KB (in Solaris can be viewed with the pagesize command). When an executable program starts running, its image is swapped out of the disk on a page-by-page, similar to when some memory is idle for a period of time, it can be swapped out into the swap space, so that the free RAM can be given to other programs that need it to use.

4: Exchange

Page scheduling is usually easy to confuse with the concept of swapping, which refers to the transfer of an idle portion of a process's memory to disk, whereas swapping refers to transferring the entire process to disk when the actual memory in the system is not enough to meet the new allocation requirements, which usually means that the swap activity is low on memory.

Vmstat Monitoring memory Performance: This command is used to check the statistics of virtual memory and to display information about process status, idle and swap space, paging, disk space, CPU load and exchange, cache refreshes, and interrupts.

The difference between swap, virtual memory, and page in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.