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 许可协议。转载请注明出处!
相关文章
服务器时间漂移,如何开启Linux NTP自动同步
本文记录了作者在服务器运维中发现时间漂移问题,并通过启用NTP同步、使用Chrony等工具进行排查与解决的全过程。文章强调了NTP同步的重要性,介绍了排查思路和高效工具的使用,以及如何同步硬件时钟(RTC),为服务器时间精准同步提供了解决方案。
2025-10-27 · 系统配置 · 约 1 分钟
修改Linux系统的鼠标滚轮的速度
Linux默认的鼠标滚轮实在太慢了,总是让我有一种Linux很卡的错觉,而且还不像Windows那样可以直接修改所有程序中的鼠标滚轮速度,不过没事,可以用imwheel这个工具来修改~ 安装 Debian系: sudo apt install imwheel Arch: sudo pacman -S imwheel 配置 创建~/.imwheelrc文件 nano ~/.imwheelrc 编辑内
2025-10-27 · 系统配置 · 约 1 分钟
在Linux系统上一键配置DoH,解决DNS解析被污染
DNS解析被污染?换DoH不就得了。云flare那破工具简单得可笑,国内DNS上游直接用就行。一行配置搞定,谁还不会啊?
2025-10-02 · 系统配置 · 约 1 分钟
在Nginx上配置https加密服务器
前言 最近开发微信小程序需要使用Https,于是折腾了一番。 超简单步骤 1. 申请SSL证书 一般云服务器提供商就会提供这种服务,我在腾讯云上申请了,十分钟就完成了。 2. 上传证书 上传到一个文件夹里面 3. 配置nginx 网络上的资料都是直接修改 /etc/nginx/nginx.conf 这个文件。 不过我的服务器上这个文件里头没有 server 这个节点可以配置。 因此需要修改 /et
2025-10-27 · 系统配置 · 约 1 分钟
讨论
共 0 条加载评论中…