linux交换内存_Linux中的交换内存如何工作?

linux交换内存

To understand what Swap memory in Linux is, we will have a look at the concept of RAM and how the Linux system reacts when RAM reaches its maximum capacity.

为了了解Linux中的交换内存,我们将了解RAM的概念以及RAM达到最大容量时Linux系统的React。

With the topic of memory, it is fairly common to come across mentions of RAM and swap space. In this tutorial, we discuss RAM and swap memory to understand why swap memory in Linux is so essential.

关于内存,提到RAM和交换空间是相当普遍的。 在本教程中,我们讨论RAM和交换内存,以了解为什么Linux中的交换内存如此重要。

设置上下文... (Setting the Context…)

Sometimes, you can experience your system getting sluggish while you are working. This usually happens due to the physical memory, or RAM being used at its maximum capacity.

有时,您会在工作时遇到系统变慢的情况。 这通常是由于物理内存或RAM处于最大容量而导致的。

This is something we want to avoid, as unavailability of RAM space can cause high I/O wait times and reduced responsiveness for the system. To counter it, the system utilizes swap space. It helps ensure that the system does not exhaust the physical memory of your system. 

这是我们要避免的事情,因为RAM空间的不可用会导致高的I / O等待时间并降低系统的响应速度。 为了解决这个问题,系统利用交换空间。 它有助于确保系统不会耗尽系统的物理内存。

RAM如何使用? (How is the RAM used?)

RAM or the Random Access Memory is the most important part of any digital device. This piece of hardware is where all the data being currently used a processor is stored.

RAM或随机存取存储器是任何数字设备中最重要的部分。 该硬件是存储处理器当前使用的所有数据的位置。

This memory location can be read much faster by the processor compared to other locations such as an HDD or SSD.

与其他位置(例如HDD或SSD)相比,处理器可以更快地读取此存储位置。

Due to its volatile nature, the RAM only stores the data which is currently being used. As soon as a system is shut down, it loses all the memory which was stored on it. Hence, we use storage devices such as an HDD, SSD or optical drive to permanently store any required data. 

由于其易失性,RAM仅存储当前正在使用的数据。 一旦系统关闭,它将丢失所有存储在其上的内存。 因此,我们使用HDD,SSD或光盘驱动器之类的存储设备永久存储任何所需的数据。

But these storage devices also help us in other instances. One such instance is when the RAM is nearing full capacity and cannot hold all the required data at the same time.

但是这些存储设备还可以在其他情况下为我们提供帮助。 一种这样的情况是RAM接近满容量并且无法同时保存所有必需的数据时。

This brings us to the concept of swap memory in Linux.

这使我们想到了Linux中交换内存的概念。

了解Linux中的交换内存 (Understanding swap memory in Linux)

Swap memory in Linux is used to prevent data overflow in RAM in event of the system facing high load. This helps prevent the system to become unresponsive due to a lack of memory. It also ensures that applications or processes don’t encounter ‘out-of-memory’ errors.

Linux中的交换内存用于在系统面临高负载的情况下防止RAM中的数据溢出。 这有助于防止系统由于内存不足而变得无响应。 它还确保应用程序或进程不会遇到“内存不足”错误。

交换内存如何工作? (How does swap memory work?)

In terms of memory, a page is a fixed block of memory space. The data blocks occupying the RAM do so in the form of these pages.

就内存而言,页面是内存空间的固定块。 占用RAM的数据块以这些页的形式存储。

In the event of the RAM storage nearing its limit, the OS needs to prioritize processes that get to utilize this memory. Hence, the OS selects the old data or processes which may be dormant to free up the RAM. 

如果RAM存储空间接近其极限,则OS需要优先处理可以利用该存储空间的进程。 因此,操作系统选择了可能Hibernate的旧数据或进程以释放RAM。

This data is then written in a pre-defined section of the ROM storage device on the system. This section is known as the ‘swap space’.

然后,将这些数据写入系统上ROM存储设备的预定义部分。 此部分称为“交换空间”。

This function of the swap memory in Linux allows the OS to maintain sufficient free RAM space at all times. Later, these pages are ‘swapped’ with the ones stored in the RAM as and when needed.

Linux中交换内存的此功能允许OS始终保持足够的可用RAM空间。 以后,这些页面将根据需要与存储在RAM中的页面进行“交换”。

为什么交换内存很重要? (Why is swap memory important?)

RAM space is a precious resource. It is required for all processes. Swap memory in Linux allows the OS to balance available resources. This, in turn, results in the efficient working of your system.

RAM空间是宝贵的资源。 所有过程都需要它。 Linux中的交换内存使操作系统可以平衡可用资源。 反过来,这又可以提高系统的工作效率。

The data stored in the swap memory in Linux takes longer to access than that stored in RAM. However, this does not cause any issues. 

Linux中交换内存中存储的数据比RAM中存储的数据需要更长的访问时间。 但是,这不会引起任何问题。

All the data written in the swap memory is loaded back into the RAM as soon as it is free, or the data is required for a current process. This makes swap memory in Linux a reliable fail-safe for preventing any out-of-memory exception for the running processes on our system.

交换存储器中写入的所有数据一经释放就立即加载回RAM,或者当前进程需要该数据。 这使Linux中的交换内存成为可靠的故障保护,可防止系统中正在运行的进程出现内存不足异常。

Note: It is an essential detail to keep in mind that swap memory is only recommended for systems utilizing mechanical hard disk drives.

注意:请记住一个重要的细节,即仅对使用机械硬盘驱动器的系统建议使用交换内存。

In the case of systems using Solid State Drives, creating swap memory partitions has been observed to cause hardware degradation in the long run. This lowers the life of your SSD and may risk memory failure.

对于使用固态驱动器的系统,从长远来看,创建交换存储器分区会导致硬件性能下降。 这会缩短SSD的使用寿命,并可能会导致内存故障。

如何检查系统的交换内存分配? (How to check your system’s swap memory allocation?)

For most systems, it is common to come across a term known as ‘virtual memory’ in the memory properties. Virtual memory is the space on the system which is equal to the sum of the RAM and swap memory in Linux.

对于大多数系统,通常会在内存属性中遇到一个称为“虚拟内存”的术语。 虚拟内存是系统上的空间,等于Linux中RAM和交换内存的总和。

Almost all modern Linux systems declare a swap partition by default to prevent memory overflow situations.

默认情况下,几乎所有现代Linux系统都默认声明一个交换分区,以防止出现内存溢出情况。

Hence, it may be essential for a Linux user to check the virtual and swap memory allocation on their system. This can help in deciding whether you wish to increase or reduce the swap partition as per your requirements.

因此,对于Linux用户而言,检查其系统上的虚拟内存和交换内存分配可能至关重要。 这可以帮助您确定是否要根据需要增加或减少交换分区。

To check the total virtual memory allocation and utilisation on your system, type the following command in your terminal.

要检查系统上虚拟内存的总分配和利用率,请在终端中键入以下命令。


free -h
Free Memory Swap
Free Memory Swap
可用内存交换

As shown in the screenshot above, this shows you the total available RAM and swap memory.

如上面的屏幕快照所示,这显示了总的可用RAM和交换内存。

It also shows you the amount of memory being utilized at the moment and the amount of free memory available to the system.

它还显示了当前正在使用的内存量以及系统可用的可用内存量。

Side note: I have explicitly declared a 0% swap for my Linux server since it’s a test environment and has no need for the swap space.

旁注:我已经明确声明了Linux服务器的0%交换,因为它是一个测试环境,不需要交换空间。

If you simply wish to find the size of the swap partition on your system, you can use the command line to display the contents of the swap configuration file stored at /proc/swaps using the cat command:

如果只希望查找系统上交换分区的大小,则可以使用命令行显示以下交换命令的内容,该命令使用cat命令存储在/ proc / swaps中


cat /proc/swaps

As mentioned before, I do not have swap space configured on my system but you can run this command to view the swap space configured for your system.

如前所述,我的系统上没有配置交换空间,但是您可以运行此命令来查看为系统配置的交换空间。

结论 (Conclusion)

In any system, few resources are even close to the importance of RAM space. With the help of swap memory, systems can make the best out of available resources without compromising a lot on performance.

在任何系统中,几乎没有资源能够接近RAM空间的重要性。 借助交换内存,系统可以充分利用可用资源,而不会影响性能。

This mechanism silently works in the background to ensure a smooth experience for the users even in the event of high load sessions.

该机制在后台静默运行,即使在高负载会话的情况下也可以确保用户的流畅体验。

We hope that, through this tutorial, you were able to develop a good understanding of RAM, along with the concept of swap memory in Linux.

我们希望通过本教程,您能够对RAM以及Linux中交换内存的概念有一个很好的了解。

翻译自: https://www.journaldev.com/39237/swap-memory-in-linux

linux交换内存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值