Cải thiện bộ nhớ trao đổi (swap) trong hệ thống Linux

Bộ nhớ trao đổi (swap) là một phần quan trọng trong hệ thống Linux, cho phép sử dụng không gian trên ổ cứng như một phần mở rộng của RAM vật lý khi cần thiết. Tuy nhiên, nếu nhu cầu sử dụng RAM vượt quá swap, việc tăng dung lượng RAM là giải pháp lâu dài hơn.

  1. Cách thức hoạt động Swap分区 được sử dụng để lưu trữ các phần dữ liệu không được sử dụng thường xuyên từ RAM sang ổ cứng. Điều này giúp hệ thống tránh bị treo khi thiếu bộ nhớ vật lý.

  2. Các phương pháp mở rộng swap

Phương pháp 1: Tạo và thêm swap分区 từ分区 mới

[root@xuexi ~]# fdisk /dev/vda  // Tạo một分区 mới
Welcome to fdisk (util-linux 2.23.2)!

Changes will remain in memory until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-4194270, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-4194270, default 4194270): +2G
Created new partition 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@xuexi ~]# mkswap /dev/vda1  // Định dạng分区 làm swap
mkswap: /dev/vda1: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 2097148 KiB
No label, UUID=23456789-1234-5678-9abc-def4567890cd
[root@xuexi ~]# free -m  // Kiểm tra tình trạng bộ nhớ
              total        used        free      shared  buff/cache   available
Mem:           2048         153        1895          0         390        1900
Swap:           2047           0        2047
[root@xuexi ~]# swapon /dev/vda1  // Kích hoạt swap分区
[root@xuexi ~]# free -m  // Kiểm tra lại
              total        used        free      shared  buff/cache   available
Mem:           2048         154        1894          0         390        1899
Swap:           4095           0        4095
[root@xuexi ~]# swapoff /dev/vda1  // Tắt swap分区
[root@xuexi ~]# free -m  // Kiểm tra
              total        used        free      shared  buff/cache   available
Mem:           2048         154        1894          0         390        1899
Swap:           2047           0        2047
[root@xuexi ~]# swapon --show  // Hiển thị các swap分区 hoạt động
NAME      TYPE      SIZE      USED PRIO
/dev/sda3 partition   2G        0    -1

Phương pháp 2: Tạo và thêm swap từ file

[root@xuexi ~]# dd if=/dev/zero of=swapfile bs=1M count=1000  // Tạo file swap với kích thước 1GB
1000+0 records in
1000+0 records out
1073741824 bytes (1.1 GB) copied, 0.573979 s, 1.86 GB/s
[root@xuexi ~]# mkswap swapfile   // Định dạng file làm swap
Setting up swapspace version 1, size = 1047996 KiB
No label, UUID=87654321-4321-6543-2165-4321654321ab
[root@xuexi ~]# free -m  // Kiểm tra
              total        used        free      shared  buff/cache   available
Mem:           2048         151        1900          0         397        1900
Swap:          2047           0        2047
[root@xuexi ~]# swapon swapfile  // Kích hoạt swapfile
swapon: /root/swapfile: giao diện không an toàn 0644, đề xuất 0600.
[root@xuexi ~]# free -m  // Kiểm tra lại
              total        used        free      shared  buff/cache   available
Mem:           2048         152        1900          0         396        1900
Swap:          3071           0        3071
[root@xuexi ~]# chmod 600 swapfile  // Cập nhật权限

Thẻ: linux swap filesystem memory management

Đăng vào ngày 27 tháng 7 lúc 14:14