search
HomeSystem TutorialLINUXLinux virtual memory, do you understand it well?

Linux virtual memory, do you understand it well?

Feb 05, 2024 pm 12:42 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxgood promiseGetting started with linuxlinux learning

Preface

Recently, there is a sharing topic in the group that I am very looking forward to: "Linux Virtual Memory". One night when we were working overtime, we were discussing the concept of virtual memory. Our leader found that several colleagues did not understand virtual memory clearly enough, so he specially selected this topic for this colleague (laughs).

Before, I had some understanding of the concept of operating systems, but after graduation, I felt a little regretful about the waste of my four-year computer major in college. Therefore, after work, I took time to watch Harbin Institute of Technology's open operating system class on NetEase Cloud Classroom, and also read a book "Linux Kernel Design and Implementation" that explains the basic concepts of operating systems. In addition, last year I wrote a simple server using C language and learned more about the underlying system. This knowledge gave me a better grasp of the application layer and helped me a lot during a recent troubleshooting.

A few days ago, another colleague asked me another question related to virtual memory. I realized that my understanding of virtual memory was not deep enough, and some concepts were even contradictory. Therefore, I went through some information and reorganized this knowledge, hoping to be more fluent in practical application next time.

origin


Virtual Memory

There is no doubt that virtual memory is definitely one of the most important concepts in the operating system. I think it's mainly because of the importance of memory in the entire system. The CPU is very fast, but has limited capacity and a single function. Other I/O hardware supports various fancy functions, but they are slower than the CPU. Therefore, they need a lubricant to act as a buffer between them, and this is where memory comes into play.

In modern operating systems, multitasking has become standard. Multi-tasking parallelism greatly improves CPU utilization, but it also brings about conflicts in memory operations between multiple processes. The concept of virtual memory is to solve this problem.

Linux virtual memory, do you understand it well?

The above picture is the simplest and most intuitive explanation of virtual memory.

The operating system has a piece of physical memory (the middle part) and two processes (actually more) P1 and P2. The operating system secretly tells P1 and P2 respectively that my entire memory is yours, use it as you like. , enough care. But in fact, the operating system just gave them a big pie. These memories were said to be given to P1 and P2, but in fact they were only given a serial number. Only when P1 and P2 actually start to use these memories, the system starts to move around and piece together the various blocks for the process. P2 thinks that it is using A memory, but in fact it has been quietly redirected to the real B by the system. Even when P1 and P2 share C memory, they don't know.

This method of deceiving the process of the operating system is virtual memory. For processes such as P1 and P2, they all think that they occupy the entire memory, and they do not know and do not need to care which address of the physical memory they use.

Paging and page tables

Virtual memory is a concept in the operating system. To the operating system, virtual memory is a comparison table. When P1 obtains the data in A memory, it should go to the A address of the physical memory and look for it in the B memory. The data should go to the C address of physical memory.

We know that the basic unit in the system is Byte. If each Byte of virtual memory is mapped to the address of physical memory, each entry requires at least 8 bytes (32-bit virtual address -> 32-bit physical address), in the case of 4G memory, 32GB of space is needed to store the comparison table, so this table is too big to fit even the real physical address, so the operating system introduces Pagethe concept of.

When the system starts, the operating system divides the entire physical memory into pages in units of 4K. When memory is allocated in the future, the unit is page, so the mapping table of virtual memory pages corresponding to physical memory pages is greatly reduced. 4G memory only requires an 8M mapping table. Some processes do not use virtual memory. There is no need to save the mapping relationship, and Linux also designs a multi-level page table for large memory, which can advance a page to reduce memory consumption. The mapping table between operating system virtual memory and physical memory is called page table.

Memory addressing and allocation

We know that through the virtual memory mechanism, each process thinks that it occupies all the memory. When the process accesses the memory, the operating system will convert the virtual memory address provided by the process into a physical address, and then obtain the data at the corresponding physical address. . There is a kind of hardware in the CPU, Memory Management Unit MMU (Memory Management Unit) is specially used to translate virtual memory addresses. The CPU also sets a cache strategy for page table addressing. Due to the locality of the program, its cache hit rate can reach 98%.

The above situation is the mapping of virtual address to physical address in the page table memory. If the physical address accessed by the process has not been allocated, the system will generate a Page Missing Interrupt. During interrupt processing, The system switches to kernel mode and allocates a physical address to the process virtual address.

Function


Virtual memory not only solves the problem of memory access conflicts between multiple processes through memory address translation, but also brings more benefits.

Process Memory Management

It helps the process to manage memory, mainly reflected in:

  • Memory integrity: Due to the "deception" of virtual memory on the process, each process thinks that the memory it obtains is a continuous address. When we write an application, we don't need to consider the allocation of large blocks of address. We always think that the system has enough large blocks of memory.
  • Security: Since when a process accesses memory, it must be addressed through the page table. The operating system can implement memory permission control by adding various access permission flags to each item in the page table.

data sharing

It is easier to share memory and data through virtual memory.

When a process loads a system library, it always allocates a piece of memory first and loads the library file on the disk into this memory. When using physical memory directly, because the physical memory address is unique, even if the system finds that the same library is in It is loaded twice in the system, but the loading memory specified by each process is different, and the system is unable to do anything.

When using virtual memory, the system only needs to point the virtual memory address of the process to the physical memory address where the library file is located. As shown in the figure above, the B addresses of processes P1 and P2 both point to physical address C.

It is also very simple to use shared memory by using virtual memory. The system only needs to point the virtual memory address of each process to the shared memory address allocated by the system.

SWAP

Virtual memory allows the process to "expand" memory.

We mentioned earlier that virtual memory allocates physical memory to the process through page fault interrupts. Memory is always limited. What if all physical memory is occupied?

Linux proposes the concept of SWAP. SWAP partitions can be used in Linux. When physical memory is allocated but the available memory is insufficient, the temporarily unused memory data will be placed on the disk first, allowing processes in need to use it first, and then wait for the process to use it again. When the data needs to be used, the data is loaded into the memory. Through this "swapping" technology, Linux can allow the process to use more memory.

common problem


I also had a lot of questions when understanding virtual memory.

32-bit and 64-bit

The most common problem is 32-bit and 64-bit.

CPU accesses memory through the physical bus, so the range of access addresses is limited by the number of machine buses. On a 32-bit machine, there are 32 buses. Each bus has two potentials, high and low, representing bits 1 and 0 respectively. , then the maximum accessible address is 2^32bit = 4GB, so it is invalid to insert memory larger than 4G on a 32-bit machine, and the CPU cannot access memory larger than 4G.

But 64-bit machines do not have a 64-bit bus, and their maximum memory is limited by the operating system. Linux currently supports a maximum of 256G memory.

According to the concept of virtual memory, it is okay to run 64-bit software on a 32-bit system. However, due to the system's structural design of virtual memory addresses, 64-bit virtual addresses cannot be used in 32-bit systems.

Directly operate physical memory

The operating system uses virtual memory. What should we do if we want to directly operate the memory?

Linux will map each device to a file in the /dev/ directory. We can directly operate the hardware through these device files, and memory is no exception. In Linux, the memory settings are mapped to /dev/mem, and the root user can directly operate the memory by reading and writing this file.

The JVM process occupies too much virtual memory

When using TOP to view system performance, we will find that in the VIRT column, the Java process will occupy a large amount of virtual memory.

Linux virtual memory, do you understand it well?

The reason for this problem is that Java uses Glibc's Arena memory pool to allocate a large amount of virtual memory and not use it. In addition, files read by Java will also be mapped into virtual memory. Under the default configuration of the virtual machine, each Java thread stack will occupy 1M of virtual memory. For details, you can check why multi-threaded programs under Linux consume so much virtual memory.

The actual physical memory occupied depends on the RES (resident) column. The value of this column is the size that is actually mapped to the physical memory.

Common management commands


We can also manage Linux virtual memory ourselves.

View system memory status

There are many ways to check the system memory status. free, vmstat and other commands can output the memory status of the current system. It should be noted that the available memory is not just the free column. Due to the lazy characteristics of the operating system, a large number of buffers/cache will not be cleared immediately after the process is no longer used. If the process that previously used them can continue to be used again, they can also be used when necessary.

In addition, through cat /proc/meminfo you can view the details of system memory usage, including dirty page status, etc. Details can be found at: /PROC/MEMINFO Mystery.

pmap

If you want to view the virtual memory distribution of a process individually, you can use the pmap pid command, which will list the occupancy of each virtual memory segment from low address to high address.

You can add -XX parameters to output more detailed information.

Modify memory configuration

We can also modify the Linux system configuration, use sysctl vm [-options] CONFIG or directly read and write files in the /proc/sys/vm/ directory to view and Change setting.

SWAP Operation

The SWAP feature of virtual memory is not always beneficial. Allowing the process to continuously exchange large amounts of data between memory and disk will greatly occupy the CPU and reduce system operating efficiency, so sometimes we do not want to use swap.

We can modify vm.swappiness=0 to set the memory to use swap as little as possible, or simply use the swapoff command to disable SWAP.

summary


The concept of virtual memory is very easy to understand, but it will derive a series of very complex knowledge. This article only talks about some basic principles and skips many details, such as the use of mid-segment registers in virtual memory addressing, the operating system's use of virtual memory to enhance cache and buffer applications, etc. If there is an opportunity, I will talk about it separately.

The above is the detailed content of Linux virtual memory, do you understand it well?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
Why is Windows commonly used for desktop computing and gaming?Why is Windows commonly used for desktop computing and gaming?Apr 27, 2025 am 12:01 AM

Windowsispreferredfordesktopcomputingandgamingdueto:1)itsvastsoftwareandgamelibrary,2)user-friendlyandcustomizableinterface,3)extensivehardwarecompatibility,and4)performanceoptimizationcapabilities,despitesomeresource-heavyandupdate-relatedissues.

The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?The Future of Linux Software: Will Flatpak and Snap Replace Native Desktop Apps?Apr 25, 2025 am 09:10 AM

For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

What are the differences in how Linux and Windows handle device drivers?What are the differences in how Linux and Windows handle device drivers?Apr 25, 2025 am 12:13 AM

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

Compare and contrast the security models of Linux and Windows.Compare and contrast the security models of Linux and Windows.Apr 24, 2025 am 12:03 AM

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function