search
HomeOperation and MaintenanceLinux Operation and MaintenanceLinux memory mechanism and manual release of swap, buffer and cache

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 – 释放所有缓存

实操:

Linux memory mechanism and manual release of swap, buffer and cache

很明显多出来很多空闲的内存了吧

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

Linux memory mechanism and manual release of swap, buffer and cache


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!

Statement
This article is reproduced at:Linux中文社区. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software