Not enough memory? Configure swap space on Linux using a swapfile
Foreword A few months ago, I excitedly wrote an article about upgrading my laptop's memory from the basic 4GB to a 16GB setup. However, over the past few months, I’ve been plagued by constant computer crashes. Now, I have reason to suspect that the memory stick I bought might be fake. With no other options, I had to switch back to the original 4GB memory stick, ~~(and then plan to buy a desktop computer)~~, not a big deal. After switching back to the 4GB memory stick, the effect was noticeable—just opening one Google Chrome browser already brought memory usage to 80%. I definitely can’t risk using JB家的IDE anymore. Yet, even this setup barely meets the system’s memory needs.
目录10 个章节
Foreword
(This article was originally written in 2017)
A few months ago, I was still happily writing an article about upgrading my laptop's memory configuration, jumping from a beggarly 4G to a 16G "high, rich, and handsome" setup. However, over the past few months, I've been tormented by frequent computer freezes. Now I have reason to suspect I might have bought counterfeit memory modules. So, with no other choice, I had to switch back to the original 4G memory stick, (and then plan to save up for a desktop), no big deal.
After switching to the 4G memory stick, the effect was significant. With just one Google Chrome browser open, memory usage had already reached 80%. I dared not use any of JB's IDEs anymore. Yet, this still could barely satisfy the system's thirst for memory space. At this point, configuring swap space was the only option. * (i.e., virtual memory)*
Swap Concept
Swap partition (also known as exchange partition) is an area on the hard drive designated for the operating system to temporarily store data that can no longer be held in RAM. Essentially, this allows you to increase the amount of information your server can retain in working "memory," with the caveat that when there is insufficient space in RAM for application data in use, the swap space on the hard drive will be utilized.
Information written to the disk will be much slower than data stored in RAM, but the operating system prefers to keep application data in memory and uses swap for older data. Overall, when a system's RAM is exhausted, using swap space as a fallback can serve as an effective safety net to prevent out-of-memory issues in non-SSD storage systems.
Check System Information
Before starting, we can check if the system already has some available swap space. There might be multiple swap files or swap partitions, but usually, it should be sufficient. We can use the following command to see if the system has a swap partition:
sudo swapon --show
If there is no result or nothing is displayed, it means the system currently has no available swap space. The free command is used to check free memory space, including the swap partition space.
free -h
Check Available Space on Hard Drive Partition
The most common way to allocate space for swap is to use a dedicated partition for that specific task. However, changing the partition scheme is not always feasible, and we can simply create a swap file that resides on an existing partition.
Before starting, we should check the current disk usage by entering the following command:
df -h
Important Note
(Updated 2024-02-18)
The method used in this article for creating swap files is suitable for the ext4 filesystem. If you are using journaling filesystems like ZFS/btrfs, please exercise caution with the method described here. I tested that this approach does not work on ZFS—it will show the error swapfile has holes and refuse to function.
Switching to the fallocate command to create the swapfile also does not work.
References
- https://askubuntu.com/questions/1198903/can-not-use-swap-file-on-zfs-files-with-holes
- https://askubuntu.com/questions/1327500/swap-file-hard-disk-it-appears-to-have-holes
Creating a swap file
Since I've been using this system for a long time and there are no free partitions available on the disk, I've chosen to manually create a swapfile to act as swap space.
It's often recommended that the swap space should be about twice the size of the memory. Since I currently have only 4GB of memory, I'll naturally create an 8GB swap space.
Use the following command to create the swapfile:
sudo fallocate -l 8G /swapfile
After testing, it appears that on OpenSuSE systems, the following command must be used to successfully create a swapfile.
(Added on 2024-02-18) This is likely because the Btrfs file system was in use at the time, requiring the
ddcommand for creation. In theory, thefallocatecommand should also work, but remember to set the swapfile to be untracked, as it may otherwise affect performance.
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
Use the following command to verify that it was created correctly.
ls -lh /swapfile
The result should look something like this:
-rw-r--r-- 1 root root 8.0G Apr 26 17:04 /swapfile
Modify swapfile permissions
sudo chmod 600 /swapfile
Check the effect
ls -lh /swapfile
The result should look something like this:
-rw------- 1 root root 8.0G Apr 26 17:04 /swapfile
Enable Swap Space
sudo mkswap /swapfile
sudo swapon /swapfile
After that, use the following command to verify that the swap space has been successfully enabled:
sudo swapon --show
The result should look similar to the following:
NAME TYPE SIZE USED PRIO
/swapfile file 8G 0B -1
Add to fstab
This way, the system will automatically mount the swapfile as swap space every time it boots.
First, please back up the fstab file yourself.
Then add the following configuration to the end of the fstab file.
/swapfile none swap sw 0 0
Or directly use the following command:
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
_EOF_
Not enough memory? Configure swap space on Linux using a swapfile
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Debian8-stable-升级Python版本到3-6
前言 最近把一个Python项目部署到服务器上,然而服务器上的Python版本实在是太旧了,于是着手进行更新。 互联网上搜索到的方法都是下载Python3.6的代码之后手动编译,然而在服务器上编译安装出了点问题,于是想到了Ubuntu的ppa方式,debian和Ubuntu本属同源,理论上Ubuntu的ppa,debian也是可以用的。 操作步骤如下: 添加软件源 add-apt-reposito
2025-10-27 · 系统配置 · 约 1 分钟
在Manjaro上配置国内源
前言 之前我一直用官方源的,但是最近发现官方源变慢了,于是想要切换国内源,顺手做个笔记。 操作 使用以下命令,生成可用中国镜像站列表: sudo pacman-mirrors -i -c China -m rank 刷新完列表之后会弹出来一个框让你选择软件源,这里的就都是国内源了,选择一个喜欢的就行(当然要选择两个以上也没问题。) 日常桌面截图
2025-10-27 · 系统配置 · 约 1 分钟
Ubuntu-16-04-安装配置Docker
安装相关包 $ sudo apt-get update # 先更新一下软件源库信息 $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common 添加软件仓库 官方仓库 # 添加 Docker 官方的 GPG 密钥(为了确认所下载
2025-10-27 · 系统配置 · 约 1 分钟
Debian配置默认不开启图形界面
前言 Linux的图形界面实在是太耗资源了,同时开几个虚拟机感觉已经hold不住了,赶紧把这些Linux服务器的图形界面都关掉。 CLI:command Line Interface,命令行界面。 现在的Linux发行版默认登陆界面几乎都是默认为GUI界面,如果按照旧版本的修改inittab文件,在一些比较新的版本是没有这个文件的(Linux的启动服务机制发生改变) 修改Grub配置文件 打开gr
2025-10-27 · 系统配置 · 约 1 分钟
讨论
共 0 条加载评论中…