swappiness 修改_什么是Linux上的Swappiness? (以及如何更改)

Linux的Swappiness设置并不控制内存使用阈值,而是影响内存在交换到磁盘前回收的优先级。文章揭示了Swappiness的误解,解释了RAM的分区、页面大小、节点和匿名页面的概念,以及交换的实际切入条件。Swappiness值设置不当可能导致硬盘活动的转移,而非真正减少交换。理想的Swappiness值取决于硬件、工作负载等因素,调整需谨慎并监控系统表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swappiness 修改

swappiness 修改

Sticks of random access memory (RAM) for a computer.
subin-ch/Shutterstock.com subin-ch / Shutterstock.com

The Linux swappiness value has nothing to do with how much RAM is used before swapping starts. That’s a widely reported and widely believed mistake. We explain what it really is.

Linux swappiness值与交换开始之前使用了多少内存无关。 这是一个被广泛报道并被广泛认为的错误。 我们解释它的真正含义。

关于Swapiness的破坏神话 (Busting Myths About Swapiness)

Swapping is a technique where data in Random Access Memory (RAM) is written to a special location on your hard disk—either a swap partition or a swap file—to free up RAM.

交换是一种将随机存取存储器 (RAM)中的数据写入硬盘上特殊位置(交换分区或交换文件)以释放RAM的技术。

Linux has a setting called the swappiness value. There’s a lot of confusion about what this setting controls. The most common incorrect description of swappiness is that it sets a threshold for RAM usage, and when the amount of used RAM hits that threshold, swapping starts.

Linux有一个称为swappiness值的设置。 此设置所控制的内容令人困惑。 交换性最常见的错误描述是它为RAM使用率设置了阈值,并且当已用RAM的数量达到该阈值时,交换就开始了。

This is a misconception that has been repeated so often that it is now received wisdom. If (almost) everyone else tells you that’s exactly how swappiness works, why should you believe us when we say it isn’t?

这是一种误解,这种误解已屡屡发生,以至于现在被人们所接受。 如果(几乎)其他所有人都告诉您交换性确实是如何工作的,那么当我们说不行时您为什么要相信我们?

Simple. We’re going to prove it.

简单。 我们将证明这一点。

您的RAM被分成区域 (Your RAM is Split Into Zones)

Linux doesn’t think of your RAM as one big homogenous pool of memory. It considers it to be divided into a number of different regions called zones. Which zones are present on your computer depends on whether it is 32-bit or 64-bit. Here’s a simplified description of the possible zones on an x86 architecture computer.

Linux不会将您的RAM视为一大同质的内存池。 它认为它被分为许多不同的区域,称为区域。 您的计算机上存在哪些区域取决于它是32位还是64位 。 这是x86架构计算机上可能区域的简化描述。

  • Direct Memory Access (DMA): This is the low 16 MB of memory. The zone gets its name because, a long time ago, there were computers that could only do direct memory access into this area of physical memory.

    直接内存访问(DMA) :这是低16 MB的内存。 该区域之所以得名,是因为很久以前,有些计算机只能直接对物理内存区域进行内存访问。

  • Direct Memory Access 32: Despite its name, Direct Memory Access 32 (DMA32) is a zone only found in 64-bit Linux. It’s the low 4 GB of memory. Linux running on 32-bit computers can only do DMA to this amount of RAM (unless they are using the physical address extension (PAE) kernel), which is how the zone got its name. Although, on 32-bit computers, it is called HighMem.

    直接内存访问32 :尽管有直接内存访问32(DMA32)的名称,但它是仅在64位Linux中找到的区域。 这是低4 GB的内存。 在32位计算机上运行Linux只能对此数量的RAM进行DMA(除非它们使用的是物理地址扩展 (PAE)内核),这就是该区域的名称。 尽管在32位计算机上,它称为HighMem。

  • Normal: On 64-bit computers, normal memory is all of the RAM above 4GB (roughly). On 32-bit machines, it is RAM between 16 MB and 896 MB.

    普通 :在64位计算机上,普通内存是4GB以上(大约)的所有RAM。 在32位计算机上,它是16 MB到896 MB之间的RAM。

  • HighMem: This only exists on 32-bit Linux computers. It is all RAM above 896 MB, including RAM above 4 GB on sufficiently large machines.

    HighMem :仅在32位Linux计算机上存在。 它是所有大于896 MB的RAM,包括足够大的计算机上大于4 GB的RAM。

PAGESIZE值 (The PAGESIZE Value)

RAM is allocated in pages, which are of a fixed size. That size is determined by the kernel at boot time by detecting the architecture of the computer. Typically the page size on a Linux computer is 4 Kbytes.

RAM以固定大小的页面分配。 该大小由内核在启动时通过检测计算机的体系结构确定。 通常,Linux计算机上的页面大小为4 KB。

You can see your page size using the getconf command:

您可以使用getconf命令查看页面大小:

getconf PAGESIZE
getconf PAGESIZE

区域附加到节点 (Zones Are Attached to Nodes)

Zones are attached to nodes. Nodes are associated with a Central Processing Unit (CPU). The kernel will try to allocate memory for a process running on a CPU from the node associated with that CPU.

区域连接到节点。 节点与中央处理器(CPU)关联。 内核将尝试从与该CPU关联的节点为该CPU上运行的进程分配内存。

The concept of nodes being tied to CPUs allows mixed memory types to be installed in specialist multi-CPU computers, using the Non-Uniform Memory Access architecture.

节点绑定到CPU的概念允许使用非统一内存访问体系结构在专业的多CPU计算机中安装混合内存类型。

That’s all very high-end. The average Linux computer will have a single node, called node zero. All zones will belong to that node. To see the nodes and zones in your computer, look inside the /proc/buddyinfo file. We’ll use less to do so:

那都是非常高端的。 一般Linux计算机将只有一个节点,称为零节点。 所有区域都将属于该节点。 要查看计算机中的节点和区域,请查看/proc/buddyinfo文件内部。 我们会less使用:

less /proc/buddyinfo
less /proc/buddyinfo in a terminal window

This is the output from the 64-bit computer this article was researched on:

这是本文研究过的64位计算机的输出:

Node 0, zone DMA   1  1  1  0  2  1  1  0  1  1  3
Node 0, zone DMA32 2 67 58 19  8  3  3  1  1  1 17

There is a single node, node zero. This computer only has 2 GB of RAM, so there is no “Normal” zone. There are only two zones, DMA and DMA32.

只有一个节点,节点为零。 此计算机只有2 GB的RAM,因此没有“正常”区域。 只有两个区域,DMA和DMA32。

Each column represents the number of available pages of a certain size. For example, for the DMA32 zone, reading from the left:

每列代表一定大小的可用页面数。 例如,对于DMA32区域,从左侧读取:

  • 2: There are 2 of 2^(0*PAGESIZE) chunks of memory.

    2 :内存中有2个2 ^( 0 * PAGESIZE)个块。

  • 67: There are 67 of 2^(1*PAGE_SIZE) chunks of memory.

    67 :2个( 1 * PAGE_SIZE)内存块中有67个。

  • 58: There are 58 of 2^(2*PAGESIZE) chunks of memory available.

    58 :有58个2 ^( 2 * PAGESIZE)个内存块可用。

  • And so on, all the way up to…

    依此类推,一直到……
  • 17: There are 17 of 2^(512*PAGESIZE) chunks.

    17 :总共2 ^( 512 * PAGESIZE)个块中有17

But really, the only reason we’re looking at this information is to see the relationship between nodes and zones.

但是,实际上,我们查看此信息的唯一原因是查看节点与区域之间的关系。

文件页面和匿名页面 (File Pages and Anonymous Pages)

Memory mapping uses sets of page table entries to record which memory pages are used, and for what.

内存映射使用页表条目集来记录使用了哪些内存页以及使用了哪些内存页。

Memory mappings can be:

内存映射可以是:

  • File backed: File backed mappings contain data that has been read from a file. It can be any kind of file. The important thing to note is that if the system freed this memory and needed to obtain that data again, it can be read from the file once more. But, if the data has been changed in memory, those changes will need to be written to the file on the hard drive before the memory can be freed. If that didn’t happen, the changes would be lost.

    支持文件支持文件的映射包含已从文件读取的数据。 它可以是任何类型的文件。 需要注意的重要一点是,如果系统释放了该内存并需要再次获取该数据,则可以再次从文件中读取该数据。 但是,如果内存中的数据已更改,则需要先将这些更改写入硬盘驱动器上的文件,然后才能释放内存。 如果没有发生,更改将丢失。

  • Anonymous: Anonymous memory is a memory mapping with no file or device backing it. These pages may contain memory requested on-the-fly by programs to hold data, or for such things as the stack and the heap. Because there is no file behind this type of data, a special place must be set aside for the storage of anonymous data. That place is the swap partition or swap file. Anonymous data is written to swap before anonymous pages are freed.

    匿名 :匿名内存是没有文件或设备支持的内存映射。 这些页面可能包含程序为存储数据或用于堆栈堆之类的程序动态请求的内存。 由于这种类型的数据后面没有文件,因此必须在特殊位置保留用于存储匿名数据的位置。 那个地方是交换分区或交换文件。 在释放匿名页面之前,将写入匿名数据以进行交换。

  • Device backed: Devices are addressed through block device files that can be treated as though they were files. Data can be read from them and written to them. A device backed memory mapping has data from a device stored in it.

    支持设备的设备 :通过块设备文件对设备进行寻址,这些设备文件可以视为文件 。 可以从它们读取数据并将其写入。 设备支持的内存映射中存储有来自设备的数据。

  • Shared: Multiple page table entries can map to the same page of RAM. Accessing the memory locations through any of the mappings will show the same data. Different processes can communicate with one another in a very efficient way by changing the data in these jointly-watched memory locations. Shared writable mappings are a common means of achieving high-performance inter-process communications.

    共享 :多个页表条目可以映射到RAM的同一页。 通过任何映射访问内存位置将显示相同的数据。 通过更改这些共同监视的内存位置中的数据,不同的进程可以以非常有效的方式相互通信。 共享的可写映射是实现高性能进程间通信的常用方法。

  • Copy on write: Copy on write is a lazy allocation technique. If a copy of a resource already in memory is requested, the request is satisfied by returning a mapping to the original resource. If one of the processes “sharing” the resource tries to write to it, the resource must be truly replicated in memory to allow the changes to be made to the new copy. So the memory allocation only takes place on the first write command.

    写时复制: 写时复制是一种惰性分配技术。 如果请求已经在内存中的资源的副本,则通过返回到原始资源的映射来满足该请求。 如果其中一个进程“共享”资源试图写入资源,则必须在内存中真正复制该资源,以允许对新副本进行更改。 因此,内存分配仅发生在第一个写命令上。

For swappiness, we need only concern ourselves with the first two in the list: file pages and anonymous pages.

为了交换,我们只需要关心列表中的前两个:文件页面和匿名页面。

轻浮 (Swappiness)

Here’s the description of swappiness from the Linux documentation on GitHub:

这是GitHub上 Linux文档中对swappiness的描述:

"This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

"This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

The default value is 60."

The default value is 60."

That sounds like swappiness turns swap up or down in intensity. Interestingly, it states that setting swappiness to zero doesn’t turn off swap. It instructs the kernel not to swap until certain conditions are met. But swapping can still occur.

听起来好像交换会增加或降低交换强度。 有趣的是,它指出将swappiness设置为零不会关闭交换。 它指示内核在满足某些条件之前不要交换。 但是交换仍然可能发生。

Let’s dig deeper. Here’s the definition and default value of vm_swappiness in the kernel source code file vmscan.c:

让我们深入研究。 这是内核源代码文件vmscan.cvm_swappiness的定义和默认值:

/*
* From 0 .. 100. Higher means more swappy.
*/
int vm_swappiness = 60;

/*
* From 0 .. 100. Higher means more swappy.
*/
int vm_swappiness = 60;

The swappiness value can range from 0 to 100. Again, the comment certainly sounds like the swappiness value has a bearing on how much swapping takes place, with a higher figure leading to more swapping.

swappiness值的范围可以从0到100。同样,注释肯定听起来像swappiness值与发生多少交换有关,较高的数字会导致更多的交换。

Further on in the source code file, we can see that a new variable called swappiness is assigned a value that is returned by the function mem_cgroup_swappiness(). Some more tracing through the source code will show that the value returned by this function is vm_swappiness. So now, the variable swappiness is set to equal whatever value vm_swappiness was set to.

进一步在源代码文件中,我们可以看到一个名为swappiness的新变量被分配了一个值,该值由函数mem_cgroup_swappiness()返回。 对源代码进行更多的跟踪将显示此函数返回的值为vm_swappiness 。 因此,现在将变量swappiness设置为等于vm_swappiness设置的任何值。

int swappiness = mem_cgroup_swappiness(memcg);

int swappiness = mem_cgroup_swappiness(memcg);

And a little further down in the same source code file, we see this:

在同一个源代码文件中 ,我们看到了以下内容:

/*
* With swappiness at 100, anonymous and file have the same priority.
* This scanning priority is essentially the inverse of IO cost.
*/
anon_prio = swappiness;
file_prio = 200 - anon_prio;

/*
* With swappiness at 100, anonymous and file have the same priority.
* This scanning priority is essentially the inverse of IO cost.
*/
anon_prio = swappiness;
file_prio = 200 - anon_prio;

That’s interesting. Two distinct values are derived from swappiness. The anon_prio and file_prio variables hold these values. As one increases, the other decreases, and vice versa.

那很有意思。 swappiness派生出两个不同的值。 anon_priofile_prio变量保存这些值。 随着一个增加,另一个减少, 反之亦然

The Linux swappiness value actually sets the ratio between two values.

Linux swappiness值实际上设置两个值之间的比率

黄金分割 (The Golden Ratio)

File pages hold data that can be easily retrieved if that memory is freed. Linux can just read the file again. As we’ve seen, if the file data has been changed in RAM, those changes must be written to the file before the file page can be freed. But, either way, the file page in RAM can be repopulated by reading data from the file. So why bother adding these pages to the swap partition or swap file? If you need that data again, you might as well read it back from the original file instead of a redundant copy in the swap space. So file pages are not stored in swap. They’re “stored” back in the original file.

如果释放了该内存,文件页面上的数据可以很容易地检索到。 Linux可以再次读取文件。 如我们所见,如果在RAM中更改了文件数据,则必须先将这些更改写入文件,然后才能释放文件页面。 但是,无论哪种方式,都可以通过从文件中读取数据来重新填充RAM中的文件页面。 那么,为什么还要麻烦将这些页面添加到交换分区或交换文件呢? 如果再次需要该数据,则最好从原始文件读回,而不是从交换空间中读取冗余副本。 因此文件页面不会存储在交换中。 它们被“存储”回原始文件中。

With anonymous pages, there is no underlying file associated with the values in memory. The values in those pages have been dynamically arrived at. You can’t simply read them back in from a file. The only way anonymous page memory values can be recovered is to store the data somewhere before freeing the memory. And that’s what swap holds. Anonymous pages that you are going to need to reference again.

对于匿名页面,没有与内存中的值相关联的基础文件。 这些页面中的值已动态到达。 您不能简单地从文件中读回它们。 可以恢复匿名页面内存值的唯一方法是在释放内存之前将数据存储在某处。 这就是交换的内容。 您将需要再次引用的匿名页面。

But note that for both file pages and for anonymous pages, freeing up the memory may require a hard drive write. If the file page data or the anonymous page data has changed since it was last written to the file or to swap, a file system write is required. To retrieve the data will require a file system read. Both types of page reclaim are costly. Trying to reduce hard drive input and output by minimizing the swapping of anonymous pages only increases the amount of hard drive input and output that is required to deal with file pages being written to, and read from, files.

但是请注意,对于文件页面和匿名页面而言,释放内存可能都需要对硬盘进行写操作。 如果文件页面数据或匿名页面数据自上次写入文件或交换以来已更改,则需要写入文件系统。 要检索数据,将需要读取文件系统。 两种类型的页面回收都是昂贵的。 试图通过最小化匿名页面的交换来减少硬盘驱动器的输入和输出,只会增加处理写入文件和从文件读取的文件页面所需的硬盘驱动器输入和输出的数量。

As you can see from the last code snippet, there are two variables. One called file_prio for “file priority”, and one called anon_prio for “anonymous priority”.

从最后的代码片段中可以看到,有两个变量。 一位名为file_prio为“文件优先级”和一个叫anon_prio为“匿名优先”。

  • The anon_prio variable is set to the Linux swappiness value.

    anon_prio变量设置为Linux swappiness值。

  • The file_prio value is set to 200 minus the anon_prio value.

    file_prio值设置为200减去anon_prio值。

These variables hold values that work in tandem. If they are both set to 100, they are equal. For any other values, anon_prio will decrease from 100 towards 0, and file_prio will increase from 100 towards 200. The two values feed into a complicated algorithm that determines whether the Linux kernel runs with a preference for reclaiming (freeing up)  file pages or anonymous pages.

这些变量包含可协同工作的值。 如果它们都设置为100,则它们相等。 对于其他任何值, anon_prio将从100减小到0,而file_prio将从100增大到200。这两个值将输入到一个复杂的算法中,该算法确定Linux内核运行时优先使用回收(释放)文件页面还是匿名文件页面。

You can think of file_prio as the system’s willingness to free up file pages and anon_prio as the system’s willingness to free anonymous pages. What these values don’t do is set any kind of trigger or threshold for when swap is going to be used. That’s decided elsewhere.

您可以将file_prio视为系统释放文件页面的意愿,将anon_prio视为系统释放匿名页面的意愿。 这些值不执行的操作是设置何时要使用交换的任何触发器或阈值。 那是在其他地方决定的。

But, when memory needs to be freed, these two variables—and the ratio between them—are taken into consideration by the reclamation and swap algorithms to determine which page types are preferentially considered for freeing up. And that dictates whether the associated hard drive activity will be processing files for file pages or swap space for anonymous pages.

但是,当需要释放内存时,回收和交换算法会考虑这两个变量以及它们之间的比率,以确定优先考虑释放的页面类型。 这决定了相关的硬盘驱动器活动是处理文件页面的文件还是交换匿名页面的空间。

何时实际切入掉期? (When Does Swap Actually Cut In?)

We’ve established that the Linux swappiness value sets a preference for the type of memory pages that will be scanned for potential reclamation. That’s fine, but something must decide when swap is going to cut in.

我们已经确定,Linux swappiness值为将要扫描潜在回收的内存页面类型设置了首选项。 这很好,但是交换计划削减的东西必须决定。

Each memory zone has a high water mark and a low water mark. These are system derived values. They are percentages of the RAM in each zone. It is these values that are used as the swap trigger thresholds.

每个存储区都有一个高水位线和一个低水位线。 这些是系统派生的值。 它们是每个区域中RAM的百分比。 这些值用作交换触发阈值。

To check what your high and low water marks are, look inside the /proc/zoneinfo file with this command:

要检查您的高水位线和低水位线,请使用以下命令在/proc/zoneinfo文件中查找:

less /proc/zoneinfo
less /proc/zoneinfo in a terminal window

Each of the zones will have a set of memory values measured in pages. Here are the values for the DMA32 zone on the test machine. The low water mark is 13966 pages, and the high water mark is 16759 pages:

每个区域将具有一组以页为单位测量的内存值。 这是测试计算机上DMA32区域的值。 低水位线是13966页,高水位线是16759页面:

DMA32 water mark values in a terminal window
  • In normal running conditions, when free memory in a zone drops below the zone’s low water mark, the swap algorithm starts scanning memory pages looking for memory that it can reclaim, taking into account the relative values of anon_prio and file_prio.

    在正常运行条件下,当区域中的可用内存下降到该区域的低水位以下时,交换算法会考虑anon_priofile_prio的相对值,开始扫描内存页面以寻找可以回收的内存。

  • If the Linux swappiness value is set to zero, swap occurs when the combined value of file pages and free pages are less than the high water mark.

    如果Linux swappiness值设置为零,则当文件页面和空闲页面的总值小于高水位线时发生交换。

So you can see that you cannot use the Linux swappiness value to influence swap’s behavior with respect to RAM usage. It just doesn’t work like that.

因此,您可以看到您无法使用Linux swappiness值来影响交换在RAM使用方面的行为。 就是那样行不通。

Swapiness应该设置为什么? (What Should Swapiness Be Set To?)

This depends on hardware, workload, hard drive type, and whether your computer is a desktop or a server. Obviously, this isn’t going to be a one size fits all type of setting.

这取决于硬件,工作负载,硬盘驱动器类型,以及您的计算机是台式机还是服务器。 显然,这不会是一种适合所有类型设置的尺寸。

And you have to bear in mind that swap isn’t just used as a mechanism to free up RAM when you’re running out of memory space. Swap is an important part of a well functioning system, and without it, sane memory management becomes very difficult for Linux to achieve.

而且您必须记住,交换不只是在内存空间不足时用作释放RAM的机制。 交换是运行良好的系统的重要组成部分,如果没有交换,Linux很难实现合理的内存管理。

Changing the Linux swappiness value has an instant effect; you don’t need to reboot. So you can make small adjustments and monitor the effects. Ideally, you’d do this over a period of days, with different types of activity on your computer, to try to find the closest to an ideal setting that you can.

更改Linux swappiness值具有立竿见影的效果。 您无需重启。 因此,您可以进行一些小的调整并监视效果。 理想情况下,您需要花几天时间,在计算机上进行不同类型的活动,以尝试找到最接近理想设置的位置。

These are some points to consider:

这些是要考虑的几点:

  • Trying to “disable swap” by setting the Linux swappiness value to zero simply shifts the swap-associated hard drive activity to file-associated hard drive activity.

    尝试通过将Linux swappiness值设置为零来“禁用交换”,只是将与交换相关的硬盘驱动器活动转换为与文件相关的硬盘驱动器活动。
  • If you have aging, mechanical hard drives, you might try reducing the Linux swappiness value to bias away from anonymous page reclamation and reduce swap partition churn. Of course, as you turn down one setting, the other setting increases. Reducing swap churn is likely to increase the file system churn. But your computer might be happier favoring one method over the other. Really, the only way to know for sure is to try and see.

    如果您有老化的机械硬盘驱动器,则可以尝试降低Linux swappiness值以偏向匿名页面回收并减少交换分区流失。 当然,当您调低一个设置时,另一设置会增加。 减少交换搅动可能会增加文件系统搅动。 但是您的计算机可能更乐于采用一种方法。 确实,唯一可以确定的方法就是尝试观察。
  • For single-purpose servers, such as database servers, you may get guidance from the suppliers of the database software. Very often, these applications have their own purpose-designed file cache and memory management routines that you’d be better to rely on. The software providers may suggest a Linux swappiness value according to machine specification and workload.

    对于单用途服务器,例如数据库服务器,您可以从数据库软件的供应商处获得指导。 通常,这些应用程序具有自己专用的文件缓存和内存管理例程,您最好依靠它们。 软件提供商可能会根据计算机规格和工作负载建议Linux交换值。
  • For the average desktop user with reasonably recent hardware? Leave it as it is.

    对于拥有合理最新硬件的普通台式机用户? 保持原状。

如何设置Linux Swappiness值 (How to Set the Linux Swappiness Value)

Before you change your swappiness value, you need to know what its current value is. If you want to reduce it a little bit, the question is a little bit less than what? You can find out with this command:

在更改交换值之前,您需要知道其当前值是多少。 如果您想减少一点,那么这个问题比什么还少吗? 您可以使用以下命令查找:

cat /proc/sys/vm/swappiness
cat /proc/sys/vm/swappiness

To configure the swappiness value, use the  sysctl  command:

要配置swappiness值,请使用sysctl命令

sudo sysctl vm.swappiness=45
sudo sysctl vm.swappiness=45 in a terminal window

The new value is used straight away, no reboot is required.

立即使用新值,无需重新启动。

In fact, if you do reboot, the swappiness value will return to its default value of 60. When you have finished experimenting and have decided on the new value you wish to use, you can make it persistent across reboots by adding it to the /etc/sysctl.conf file. You can use whichever editor you prefer. Use the following command to edit the file with the nano editor:

实际上,如果您重新启动,则swappiness值将恢复为默认值60。完成实验并确定要使用的新值后,可以通过将其添加到/etc/sysctl.conf来使其在重新启动后保持不变。 /etc/sysctl.conf文件。 您可以使用任何喜欢的编辑器。 使用以下命令通过nano编辑器编辑文件:

sudo nano /etc/sysctl.conf
sudo nano /etc/sysctl.conf  in a terminal window

When nano opens, scroll to the bottom of the file and add this line. We’re using 35 as the permanent swappiness value. You should substitute the value you wish to use.

nano打开时,滚动到文件底部并添加此行。 我们使用35作为永久交换值。 您应该替换您要使用的值。

vm.swappiness=35
The /etc/sysctl.conf  in the nano editor in a terminal window

To save your changes and exit from nano, press “Ctrl+O”, press “Enter”, and press “Ctrl+Z.”

要保存更改并退出nano ,请按“ Ctrl + O”,按“ Enter”,然后按“ Ctrl + Z”。

内存管理很复杂 (Memory Management is Complex)

Memory management is complicated. And that’s why, for the average user, it is usually better to leave it up to the kernel.

内存管理很复杂。 这就是为什么对于普通用户而言,通常最好将其留给内核。

It’s easy to think you’re using more RAM than you are. Utilities like top and free can give the wrong impression. Linux will use free RAM for a variety of its own purposes, such as disk caching. This artificially elevates the “used” memory figure and reduces the “free” memory figure. In actual fact, the RAM used as disk cache is flagged as both “used” and “available” because it can be reclaimed at any time, very quickly.

容易想到您使用的RAM比实际更多。 诸如topfree类的实用程序可能会给人留下错误的印象。 Linux会将空闲RAM用于多种用途,例如磁盘缓存。 这会人为地提高“已用”的存储容量并减少“空闲”的存储容量。 实际上,用作磁盘缓存的RAM被标记为“已使用”和“可用”,因为可以随时非常快地对其进行回收。

To the uninitiated that might look like swap isn’t working, or that the swappiness value needs changing.

对于初学者来说,似乎交换不起作用,或者交换价值需要更改。

As always, the devil is in the detail. Or, in this case, the daemon. The kernel swap daemon.

与往常一样,细节决定成败。 或者,在这种情况下,守护程序。 内核交换守护程序。

翻译自: https://www.howtogeek.com/449691/what-is-swapiness-on-linux-and-how-to-change-it/

swappiness 修改

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值