Location>code7788 >text

Memory Swap Space - Swap Space

Popularity:977 ℃/2024-08-05 10:29:37

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

  1. Close the existing swap space (if it already exists).
  2. utilizationddcommand 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

  1. Partitioning: usingfdiskmaybepartedand other tools to carve out a partition on the disk for swapping.
  2. Formatting: Use themkswapcommand to format the partition as a swap partition.
  3. Activation: Useswaponcommand 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:

  1. free command: displays the memory usage of the system, including physical memory, swap space, and so on.

free -m

-moption 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

  1. Memory Expansion: When physical memory is low, swap space provides additional storage to ensure that the system can continue to run.
  2. 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.
  3. 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 -mcommand 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.