Reprinted with attribution:
I. Overview
Memory Swap Space (Swap Space) is a supplement to a computer's memory, located on the hard disk drive. When physical memory (RAM) runs low, the system moves inactive pages (or memory pages) into swap space to free up physical memory for processes that need it more. This approach is slower than reading data directly from physical memory, but it is effective in preventing the system from crashing due to insufficient memory.
II. Configuration
1. Configuration principles
- The total size of the swap space is generally recommended to be the greater of twice the physical memory and 32MB, but no more than 2GB (different sources may have different recommendations, depending on actual needs and environment).
- Dedicated swap partitioning is the recommended method because of its better performance and ease of management.
2. Configuration method (Linux as an example)
Creating an exchange file
- Close the existing swap space (if it already exists).
- utilization
dd
command to create a swap file. For example, create a 10GB swap file:
sudo dd if=/dev/zero of=/swapfile bs=1G count=10
3. Set the permissions of the swap file to allow only the root user to read and write:
sudo chmod 600 /swapfile
4. Set the file to swap space:
sudo mkswap /swapfile
5. Activation of the exchange space:
sudo swapon /swapfile
Configure the switching partition
- Partitioning: using
fdisk
maybeparted
and other tools to carve out a partition on the disk for swapping. - Formatting: Use the
mkswap
command to format the partition as a swap partition. - Activation: Use
swapon
command to activate the swap partition.
III. Viewing
In Linux, you can use a variety of commands to see how the swap space is being used:
-
free command: displays the memory usage of the system, including physical memory, swap space, and so on.
free -m
-m
option indicates that the display is in MB.
2. swapon command: View the swap file or swap partition currently in use.
swapon --show
3. cat /proc/swaps command: to view the swap space currently in use and its related information.
cat /proc/swaps
4. top or htop commands: These interactive system monitoring tools provide a real-time display of system resource usage, including swap space, in the terminal.
IV. Role
- Memory Expansion: When physical memory is low, swap space provides additional storage to ensure that the system can continue to run.
- Performance optimization: By moving infrequently used memory pages to swap space, physical memory space can be freed up for programs that currently need to be executed, thus improving system performance.
- System Stability: Prevents system crashes or performance degradation due to insufficient memory.
V. Examples
1. Create an exchange file:
sudo dd if=/dev/zero of=/swapfile bs=1G count=8 # Create an 8GB swap file sudo chmod600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
2. Verify the configuration:
utilizationfree -m
command to view memory and swap space usage and confirm that swap space is properly configured and activated.
With the above steps, the Linux system successfully configures the swap space and improves the stability and performance of the system.