ubuntu swappiness 修改

本文介绍了如何通过调整Swappiness参数来改善Ubuntu系统的性能。Swappiness控制着内核将进程从物理内存移动到交换磁盘的倾向。文章详细解释了Swappiness的工作原理,并提供了检查、临时更改及永久设置Swappiness值的方法。

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

https://if_you_are_human_please_remove_help.ubuntu.com/community/SwapFaq

What is swappiness and how do I change it?

The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.

  • swappiness can have a value of between 0 and 100
  • swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
  • swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache

The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment. Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.

To check the swappiness value

cat /proc/sys/vm/swappiness

To change the swappiness value A temporary change (lost on reboot) with a swappiness value of 10 can be made with

sudo sysctl vm.swappiness=10

To make a change permanent, edit the configuration file with your favorite editor:

gksudo gedit /etc/sysctl.conf

Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:

vm.swappiness=10

Save the file and reboot.


if you want speed your ubuntu OS more,  please try

sudo apt-get install preload





### 修改 Ubuntu 中的虚拟内存 在 Ubuntu 系统中,可以通过调整交换空间(swap space)来间接修改虚拟内存。以下是具体方法: #### 方法一:增加现有交换分区大小 如果已经存在一个交换分区,则可以先禁用当前的交换分区,然后创建一个新的更大的交换文件。 ```bash sudo swapoff -a ``` 接着创建新的交换文件并设置其大小,例如要创建一个 4GB 的新交换文件: ```bash sudo dd if=/dev/zero of=/swapfile bs=1G count=4 ``` 赋予合适的权限给这个交换文件: ```bash sudo chmod 600 /swapfile ``` 建立交换区域并将该文件标记为交换空间: ```bash sudo mkswap /swapfile ``` 启用新建的交换文件: ```bash sudo swapon /swapfile ``` 为了使更改永久生效,在 `/etc/fstab` 文件末尾添加如下行[^1]: ```text /swapfile none swap sw 0 0 ``` #### 方法二:通过命令行工具管理交换文件 也可以利用 `fallocate` 命令快速分配磁盘空间用于创建交换文件而不必像之前那样填充零值数据。同样以创建 4 GB 大小为例: ```bash sudo fallocate -l 4G /swapfile ``` 后续操作同上,即配置权限、初始化交换区以及激活它。 #### 验证交换空间状态 无论采用哪种方式,都可以使用以下命令查看当前系统的交换情况: ```bash swapon --show free -h ``` 上述命令会显示有关已安装交换设备的信息及其总量和可用量等详情。 #### 调整内核参数优化性能 有时还需要调节一些内核参数以便更好地控制何时应该使用物理 RAM 或者转向交换存储器。这通常涉及到编辑 `/proc/sys/vm/swappiness` 参数。较低数值意味着更倾向于保持应用程序的数据驻留在实际内存里;较高则表示更容易把不活跃页面移至交换区。默认情况下通常是 60 左右,可以根据需求适当降低此值来减少对硬盘读写的频率从而提高响应速度。 ```bash echo vm.swappiness=10 | sudo tee -a /etc/sysctl.conf sudo sysctl -p ``` 以上就是关于如何在 Ubuntu修改虚拟内存的一些基本指导[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值