


This article introduces the principles and practical operations of Linux memory mechanism, virtual memory swap, buffer/cache release, etc.
1. What is the memory mechanism of Linux?
2. When did Linux start using virtual memory (swap)?
3. How to release memory? 4. How to release swap?
1. What is the memory mechanism of Linux?
We know that reading and writing data directly from physical memory is much faster than reading and writing data from the hard disk. Therefore, we hope that all data reading and writing will be faster. It is completed in memory, and memory is limited, which leads to the concepts of physical memory and virtual memory.
Physical memory is the memory size provided by the system hardware. It is real memory. Compared with physical memory, there is a concept of virtual memory under Linux. Virtual memory is a strategy proposed to meet the shortage of physical memory. , it is a piece of logical memory that is virtualized using disk space. The disk space used as virtual memory is called swap space.
As an extension of physical memory, Linux will use the virtual memory of the swap partition when the physical memory is insufficient. In more detail, the kernel will write the temporarily unused memory block information to the swap space. In this way, The physical memory is released, and this memory can be used for other purposes. When the original content is needed, the information will be read from the swap space into physical memory again.
Linux's memory management adopts a paging access mechanism. In order to ensure that physical memory can be fully utilized, the kernel will automatically swap infrequently used data blocks in physical memory into virtual memory at appropriate times. , while retaining frequently used information to physical memory.
To have a deep understanding of the Linux memory operating mechanism, you need to know the following aspects:
Linux system will perform page swap operations from time to time to maintain as much free physical memory as possible, even if there is no When something requires memory, Linux will also swap out temporarily unused memory pages. This avoids the time required to wait for the exchange.
Linux page swapping is conditional. Not all pages are swapped to virtual memory when not in use. The Linux kernel only swaps some infrequently used page files to virtual memory based on the "most recently used" algorithm. Memory, sometimes we will see this phenomenon: Linux still has a lot of physical memory, but a lot of swap space is also used. In fact, this is not surprising. For example, when a process that takes up a lot of memory needs to consume a lot of memory resources when running, some uncommon page files will be swapped into virtual memory, but later this process that takes up a lot of memory resources will be swapped into virtual memory. When the process ends and a lot of memory is released, the page file that was just swapped out will not be automatically swapped into the physical memory. Unless this is necessary, the system's physical memory will be much free at this moment, and the swap space is also being used. The phenomenon just mentioned occurred. There's nothing to worry about at this point, as long as you know what's going on.
The pages in the swap space will first be swapped to physical memory when they are used. If there is not enough physical memory to accommodate these pages at this time, they will be swapped out immediately. In this way, there may be no virtual memory. Enough space to store these swap pages will eventually lead to problems such as false crashes and service abnormalities in Linux. Although Linux can recover by itself within a period of time, the recovered system is basically unusable.
Therefore, it is very important to properly plan and design the use of Linux memory.
In the Linux operating system, when an application needs to read data in a file, the operating system first allocates Some memory, read data from the disk into these memories, and then distribute the data to the application; when it is necessary to write data to the file, the operating system first allocates memory to receive the user data, and then writes the data from the memory to the disk. superior. However, if there is a large amount of data that needs to be read from the disk to the memory or written from the memory to the disk, the read and write performance of the system becomes very low, because whether it is reading data from the disk or writing data to the disk, it is a very long process. A process that consumes time and resources. In this case, Linux introduced the buffers and cached mechanisms.
Buffers and cached are both memory operations, used to save files and file attribute information that have been opened by the system. In this way, when the operating system needs to read certain files, it will first search in the buffers and cached memory areas. If found, Directly read out and send it to the application. If the required data is not found, it will be read from the disk. This is the caching mechanism of the operating system. Through caching, the performance of the operating system is greatly improved. But the contents of buffers and cached buffers are different.
Buffers is used to buffer block devices. It only records the metadata of the file system and tracking in-flight pages, while cached is used to buffer files. To put it more simply: buffers are mainly used to store the contents of the directory, file attributes and permissions, etc. And cached is directly used to remember the files and programs we have opened.
In order to verify whether our conclusion is correct, you can open a very large file through vi to see the changes in cache, and then vi the file again to feel the similarities and differences in the speed of the two openings. Is it the first time? Is the second opening speed significantly faster than the first time? Then execute the following command:
find / -name .conf to see if the value of buffers changes, and then execute the find command repeatedly to see the difference in display speed between the two times.
2. When did Linux start using virtual memory (swap)?
[root@wenwen ~]# cat /proc/sys/vm/swappiness 60
The 60 above means that swap will only be used when 40% of the physical memory is used (refer to network information: when the remaining physical memory is less than 40% (40 =100-60), start using the swap space) When swappiness=0, it means using the physical memory to the maximum extent, and then the swap space. When swappiness=100, it means actively using the swap partition and transferring the data on the memory in a timely manner. Move it to the swap space.
The larger the value, the more likely it is to use swap. It can be set to 0, which does not prohibit the use of swap, but only minimizes the possibility of using swap.
通常情况下:swap分区设置建议是内存的两倍 (内存小于等于4G时),如果内存大于4G,swap只要比内存大就行。另外尽量的将swappiness调低,这样系统的性能会更好。
B.修改swappiness参数
临时性修改: [root@wenwen ~]# sysctl vm.swappiness=10 vm.swappiness = 10 [root@wenwen ~]# cat /proc/sys/vm/swappiness 10
永久性修改:
[root@wenwen ~]# vim /etc/sysctl.conf 加入参数: vm.swappiness = 35 然后在直接: [root@wenwen ~]# sysctl -p /etc/sysctl.conf 查看是否生效: cat /proc/sys/vm/swappiness 35
立即生效,重启也可以生效。
三、怎么释放内存?
一般系统是不会自动释放内存的关键的配置文件/proc/sys/vm/drop_caches。这个文件中记录了缓存释放的参数,默认值为0,也就是不释放缓存。他的值可以为0~3之间的任意数字,代表着不同的含义:
0 – 不释放1 – 释放页缓存2 – 释放dentries和inodes3 – 释放所有缓存
实操:
很明显多出来很多空闲的内存了吧
4. How to release swap?
Premise: First of all, make sure that the remaining memory is greater than or equal to the swap usage, otherwise it will crash! According to the memory mechanism, once the swap partition is released, all files stored in the swap partition will be transferred to physical memory. Releasing swap is usually accomplished by remounting the swap partition.
a. Check where the current swap partition is mounted? b. Shut down this partition c. Check the status: d. Check whether the swap partition is shut down. The bottom line shows all e. Mount swap to /dev/sda5 f. Check whether the mounting is successful
The above is the detailed content of Linux memory mechanism and manual release of swap, buffer and cache. For more information, please follow other related articles on the PHP Chinese website!

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
