要查看 Linux 系統(tǒng) swap 使用情況,可使用 free -h 或 swapon --show 命令;設(shè)置 swap 大小時,桌面系統(tǒng)建議為物理內(nèi)存的 50%~100%,服務(wù)器建議為 100%~200%,大內(nèi)存系統(tǒng)可設(shè)為 4~8GB;添加 swap 可通過創(chuàng)建 swap 文件或分區(qū)實現(xiàn),步驟包括創(chuàng)建文件、設(shè)置權(quán)限、格式化并啟用,同時修改 /etc/fstab 以開機自動加載;若 swap 使用率過高,應(yīng)調(diào)整 swappiness 參數(shù)、關(guān)閉不必要的程序或升級硬件。
Linux 系統(tǒng)中,swap 空間是硬盤上用來作為虛擬內(nèi)存的一部分,當物理內(nèi)存(RAM)不足時,系統(tǒng)會將部分數(shù)據(jù)轉(zhuǎn)移到 swap 區(qū)域。合理管理 swap 可以提升系統(tǒng)穩(wěn)定性,尤其是在內(nèi)存吃緊的情況下。
如何查看當前 swap 使用情況
首先你得知道自己還有多少 swap 可用。最簡單的辦法是使用 free
命令:
free -h
這個命令會顯示當前內(nèi)存和 swap 的使用情況,加 -h
是為了讓人更容易看懂單位(比如 G 或 M)。
也可以用 swapon --show
來列出所有啟用的 swap 分區(qū)或文件。如果你發(fā)現(xiàn) swap 幾乎沒被使用,那說明你的內(nèi)存還夠用;如果經(jīng)常被使用,可能要考慮擴容或者優(yōu)化程序內(nèi)存占用。
怎么設(shè)置 swap 大小才合適?
這個問題沒有統(tǒng)一答案,但有幾個常見做法可以參考:
- 如果你的系統(tǒng)主要用于桌面或者輕量級服務(wù),swap 設(shè)置為物理內(nèi)存大小的 50%~100% 就差不多了。
- 對于運行大型應(yīng)用、數(shù)據(jù)庫或內(nèi)存密集型任務(wù)的服務(wù)器,swap 可以設(shè)為內(nèi)存的 100%~200%,甚至更高。
- 如果你有大量 RAM(比如 32GB 以上),swap 可以稍微少一點,比如 4~8GB,主要是為了應(yīng)對極端情況。
注意:swap 不是用來“擴展”內(nèi)存的工具,它只是臨時緩沖。頻繁使用 swap 會導(dǎo)致性能下降,因為硬盤比內(nèi)存慢多了。
如何添加 swap 文件或分區(qū)
如果你發(fā)現(xiàn) swap 不夠用了,可以考慮新增 swap。常用方法有兩種:使用 swap 分區(qū)或 swap 文件。
添加 swap 文件步驟如下:
創(chuàng)建一個指定大小的文件,例如創(chuàng)建 4G 的 swap 文件:
fallocate -l 4G /swapfile
設(shè)置合適的權(quán)限(只允許 root 讀寫):
chmod 600 /swapfile
格式化為 swap:
mkswap /swapfile
啟用這個 swap 文件:
swapon /swapfile
想讓它開機自動加載,記得編輯
/etc/fstab
文件,加上這行:/swapfile none swap sw 0 0
這樣就完成了。當然,如果你有空余的硬盤分區(qū),也可以直接格式化為 swap 分區(qū)來使用。
swap 使用率太高怎么辦?
如果你發(fā)現(xiàn)系統(tǒng)頻繁使用 swap,導(dǎo)致響應(yīng)變慢,可以嘗試以下幾種方式:
調(diào)整
swappiness
參數(shù)。這個值控制內(nèi)核使用 swap 的傾向,默認是 60,數(shù)值越低越傾向于保留內(nèi)存數(shù)據(jù)。可以通過下面命令臨時調(diào)整:sysctl vm.swappiness=30
想要永久生效,需要修改
/etc/sysctl.conf
文件。關(guān)閉不必要的后臺程序或服務(wù),減少內(nèi)存占用。
升級硬件,增加更多物理內(nèi)存。
如果是云服務(wù)器,可以考慮換配置更高的實例類型。
基本上就這些。 swap 管理不算復(fù)雜,但容易忽略細節(jié),比如權(quán)限設(shè)置、開機掛載等。只要平時多留意系統(tǒng)資源使用情況,就能避免很多問題。
The above is the detailed content of How to manage swap space. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Toremoveapackageusingyum,usethecommandsudoyumremovepackage_name,whichremovesthepackageandpotentiallyitsunuseddependencies.Beforedoingso,ensureyouhavethecorrectpackagenamewithyumlistorrpm-q,checkdependenciesviayumdeplist,andavoidremovingcriticalsystem

To view the system log, you need to select the corresponding tool according to the operating system. Windows uses event viewer, with the path to Win R, enter eventvwr.msc, to view the "System" classification under "Windows Log", and supports filtering and saving; Linux system logs are usually located in /var/log/ directory, such as syslog, messages, dmesg files, and can be viewed by tail-f or journalctl-u service name commands; Mac can view logs through the Console application or logshow command. When viewing, you should pay attention to error and warning level information, analyze problems based on timestamps and context, and pay attention to permission requirements.

When a file is occupied, you can search and end the occupied process by the following methods: 1. The Windows system can use the task manager to search for file handles with the resource monitor; 2. Use the handle.exe tool of Sysinternals to query accurately, requiring administrator permission; 3. Linux/macOS uses the lsof command to search for keywords, and grep can search for keywords; 4. Before ending the process, you must confirm the purpose. Windows can use the task manager or taskkill command, and Linux/macOS can use the kill command to avoid killing the system process by mistake.

To ensure that the system time synchronization is reliable, first make sure that the NTP service is installed and run, use systemctl to check the state of ntp or chronyd, and start and set up the power-on self-start if necessary. Secondly, configure a suitable NTP server, modify the /etc/ntp.conf or /etc/chrony/chrony.conf file, and recommend choosing a server with a similar geographical location such as Alibaba Cloud or Tencent Cloud. Finally, check the synchronization status and use ntpq-p or chronycsources to view the connection status. If the offset is too large, you can use ntpdate to manually calibrate it, but long-term synchronization should rely on background services to ensure stability.

The key to using parted partition disks is to master several steps: 1. Preparation: Confirm the target disk device name (such as /dev/sdb) and judge its partition table type. If it is less than 2TB, use MSDOS (MBR) and if it is greater than 2TB, use GPT; 2. Create a partition table: After entering parted operation mode, execute the mklabel command to select gpt or msdos, this step will clear the disk data; 3. Start partition: Use the mkpart command to specify the partition type, file system and start and end location, such as mkpartprimaryext40GB50GB, and continue to add other partitions; 4. Format and mount the partition: manually execute mkfs.ext4 and other commands to format,

Writing a simple Bash script is actually not that difficult. You can get started quickly by mastering a few basic structures and commands. 1. Each script should start with #!/bin/bash, and then run after adding execution permissions through chmod x; 2. The variables are assigned directly without declaration, and they are referenced by $ variable name or ${ variable name}, and use the read command to achieve input interaction; 3. The commonly used if judgment and for/while loops in the control process, pay attention to the difference between spaces before and after square brackets and comparison operators; 4. Practical techniques include debugging parameters - x, using absolute paths, adding comments to improve readability, and indicating successful ending by exit0. After mastering these core points, practice more to write Bash scripts proficiently.

Checking whether the command is successful in Bash can be achieved by exiting the status code. 0 means success, non-zero value means error; use $? to view the exit code of the previous command, such as ls/some/directory;echo$?; it can also be directly judged in the if statement, such as ifmycommand--option; thenecho"Success";elseecho"Failed";fi; common exit codes include 0 (success), 1 (general error), 2 (incorrect command usage), etc.; use set-e to enable the script to exit immediately when any command fails, but it should be used with caution to avoid misjudgment of non-serious errors.

To troubleshoot network interface problems, you need to follow the steps to check the hardware connection, interface enable status, driver and IP configuration. First, check whether the network cable is plugged in well, try to replace the cable or port; confirm whether Wi-Fi is turned on and the SSID is connected correctly; external network card can be replaced with USB port test; secondly, enable the disabled network interface through the control panel in Windows, and use the iplink command to view and enable it in Linux; then update or reinstall the driver, especially after the system is upgraded, and then make sure that the DHCP is turned on or the static IP is configured correctly, and check whether the firewall restricts network access.
