It all happens some time: you run out of your main memory and the system turns to the hard disk to use as a temporary memory. What if you come to the limits of your Linux server’s swap space but the system demands more? There are basically four options here:
1. Create a new swap file
2. Create a swap file on an existing logical volume
3. Extend a swap file on an existing logical volume
4. Create a new swap partition
If you are in need of swap space now and do not think you will need it later, then taking the system down for creating a new swap partition or extending the swap file on the logical volume will be the longer way to take; creating a new swap file and making it available will be the easiest and the quickest solution. So, in our list, creating a new swap file is the easiest, extending swap file on an existing logical volume is a little harder but recommended, and creating a new swap partition is the hardest.
Create a New Swap File
We need to know the size of the additional swap space we will need. Let’s assume that we will need a 512 Megabytes of swap space. To determine the number of blocks, we need to multiply the size with 1024. In our example this gives 512 x 1024 = 524288. Now we issue
dd if=/dev/zero of=/home/swapfile bs=1024 count=524888
to open up our free space (you may argue that making a swap space in the /home directory is questionable in terms of security, but if you’re tight on hard disk space on the server, /home is the directory that you can find some free space). Then setup the file as swap with
mkswap /home/swapfile
The swap space will not be enabled at boot time. You need to add the space to /etc/fstab. Add the following line to your /etc/fstab:
/home/swapfile swap swap defaults 0 0
Creating a Swap File on an Existing Logical Volume
In this case, let’s assume we want to add the swap space on /dev/VolGroup00 and /LogVol02 is the swap space we want to add. Let’s also assume that the swap space we need is 2 Gigabytes. First, we create the logical volume of size 2 GB by
lvm lvcreate VolGroup00 -n LogVol02 -L 2 G
Then we format it by
mkswap /dev/VolGroup00/LogVol02
and add it to /etc/fstab by adding this line:
/dev/VolGroup00/LogVol02 swap swap defaults 0 0
Now we activate the swap volume by
swapon -va
To check if the swap is working properly, you can issue cat /proc/swaps or simply free.
Read on for extending a swap file on an existing logical volume and creating a new partition.
Extending Swap File on an Existing Logical Volume
For this case, let’s assume that the swap space we will extend is /dev/VolGroup00/LogVol01 and we want to extend it by 1 Gigabyte. First, we disable the active swap file by
swapoff -v /dev/VolgGroup00/LogVol01
Then we add the swap space by
lvm lvresize /dev/Volgroup00/LogVol01 -L 1G
format it
mkswap /dev/VolGroup00/LogVol01
and finally enable the extended volume by swapon -va. As always, you can check the new space by cat /proc/swaps or free commands.
Create a New Swap Partition
We can go with the parted program here since it is easier than fdisk. Before adding a swap partition, we have to check which hard disk has sufficient space to be used as swap. We can do that by issuing print at the parted prompt. With the information we receive, we determine how much space we will use and at what partition. Assume that we will add 1 GB of swap space to /dev/hdc1. The syntax that we will use in parted is
mkpartfs partition-type filesystem-type start end
where start and end are the megabytes from the beginning of the disk (partition-type can be primary, extended or logical but extended and logical are used only for MS-DOS and MIPS labels). Further assuming that the free space on /dev/hdc1 starts at the 520th megabyte, the ending will be 520 + 1024 (1 Gigabyte of space) = 1544. So our command will be
mkpartfs primary linux-swap 520 1024
After reserving the space, we format it with swap partition type, using
mkswap /dev/hdc1
Then we enable it by swapon /dev/hdc1. And finally, to activate it in boot time, we add it to /etc/fstab by adding the following line:
/dev/hdc1 swap swap defaults 0 0
As we have seen, we check the swap by cat /proc/swaps or free.
Conclusion
With the falling prices of main memoriy (RAM), usually system administrators do not need the swap space in daily normal operations. But times can be tough and the system may run out of swap space, and then the administrator can have no chance but to increase swap size in order to cope with the peak load. The quickest way could be to add a swap file and the long-term solution is to add a swap partition.
Related Reading
How to Build a Linux Server
http://www.brighthub.com/computing/linux/articles/29541.aspx - In this series we look at building and configuring a Linux server from scratch. We will look at configuration in detail and we will determine ways to keep our server as secure as possible.
分享到:
相关推荐
本篇内容主要介绍了如何解决Linux交换空间不足的问题,包括删除交换分区和交换文件的方法。 首先,我们来看如何删除交换分区。删除交换分区的前提是该分区没有被使用,即未被挂载,且交换分区已经关闭。以下是一步...
页面交换是Linux内存管理的关键技术,它允许系统将不常用的数据页从物理内存移至磁盘上的交换空间,从而释放内存供其他进程使用。然而,在实时系统中,这种技术可能导致不可预测的延迟,因为它增加了程序执行时间的...
添加交换空间是常见的需求,特别是在系统升级或内存需求增加时。有添加交换分区和创建交换文件两种方法。推荐添加交换分区,因为它通常比交换文件性能更好,但如果硬盘空间有限,创建交换文件也是可行的选择。添加...
- 交换空间 swpd 逐渐增加,说明系统为了保持一定的最小可用内存,不断将数据从 RAM 写入交换空间。 通过以上分析,我们可以了解到 Linux 系统如何管理和优化其内存资源,以及如何使用 vmstat 等工具来监控系统的...
### Linux裁剪掉页面交换机制的研究与实现 #### 摘要 页面交换技术作为Linux存储管理系统中的核心组件,在提高内存利用率及支持多任务运行方面起到了重要作用。然而,在某些具有实时性需求的应用场景中(例如...
其中包括交换空间、/proc的信息以及各种日志文件和进程记账等。 第12章:图形界面。本章主要介绍了X Window以及它的配置,并且介绍了在KDE环境下汉化的基本思想,用实例来讲解了Linux汉化的基础知识。 第13章:网络...
3. **内存管理**:早期的Linux内核对物理内存进行了基本的管理和分配,确保每个进程都有自己的地址空间,并且可以安全地交换进出内存。 4. **文件系统**:虽然0.01版本的Linux没有实现完整的现代文件系统,但已经...
内存至少要达到1GB,而交换空间应为内存的2倍,例如,如果内存是1GB,那么交换分区应该设置为3GB。对于更大的内存系统,交换空间的需求可能会有所不同。硬盘空间至少需要5GB,同时,/tmp目录至少要有400MB的可用空间...
在Linux操作系统中,`swap`交换空间是一种至关重要的功能,特别是在物理内存有限的情况下。当系统的物理内存不足以满足应用程序的需求时,Linux会利用`swap`分区作为扩展内存来存储数据。`swap`空间允许系统在硬盘上...
- 修改`/etc/security/limits.conf`文件,增加以下内容: ```conf oracle soft nproc 2047 oracle hard nproc 16384 oracle soft nofile 1024 oracle hard nofile 65536 ``` - 在`/etc/pam.d/login`文件...
如果发现交换空间不足,可以通过创建一个临时的交换文件来增加交换空间。创建交换文件的步骤包括: - 使用`su - root`切换到root用户权限。 - 创建一个交换文件,例如`touch tmpswap`。 - 设置文件权限`chmod 600 ...
512MB较为合适,以确保足够的交换空间。 7. 在vim/vi编辑器中,撤销上一步的错误操作使用的是C. u命令。 8. 要实现数据库的长久连接,即保持连接状态,应该使用带_p参数的连接函数,即D. mysql_pconnect。 9. 在...
- 添加到`/etc/fstab`以确保系统重启后仍启用交换空间:`/swapfile none swap sw 0 0`。 #### 二、用户管理与文件权限管理 **1. 创建用户** - 使用`useradd -u 9911 alice`创建用户`alice`,指定UID为9911。 - ...
本章深入探讨了Linux内核如何管理每个进程的虚拟地址空间,包括用户空间和内核空间之间的映射关系。通过这些机制,Linux能够有效地支持大规模的应用程序运行。 #### 五、锁机制与进程间通信(Chapter 5) 在多...
- 内存:Oracle 11g R2建议至少1GB的物理内存,而交换空间应是内存的两倍,即至少3GB。 - 硬盘:至少需要5GB的可用空间。 2. **修改操作系统核心参数**: - **限制配置**:为了适应Oracle的需求,需要调整用户...
### Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序 #### 概述 在Android操作系统中,硬件抽象层(HAL)起到了桥梁的作用,它连接了Linux内核中的驱动程序与用户空间的应用程序,使得应用程序能够以统一...
交换空间用于在物理内存不足时,将一部分不常用的进程移到磁盘上,释放物理内存供其他进程使用,从而提高系统的响应速度。 **5.2 交换分区的创建与管理** - **创建交换分区**: - 使用`fdisk`等工具创建一个新的...
- **交换空间**:交换分区应为物理内存的2倍,例如1GB内存建议设置为3GB的交换空间。 - **硬盘空间**:至少需要5GB以上的硬盘空间,实际需求可能因数据库大小而异。 2. **修改操作系统核心参数** - **用户限制**...
- **修改环境变量**:更改环境变量可能会影响正在运行的进程,因此需谨慎操作。 - **释放环境变量**:不再需要的环境变量应及时释放,以减少内存占用。 #### 2.1.5. ELF文件 可执行和链接格式(ELF)是Linux系统中可...