About swap space

Swap空间(swap space)也叫做虚拟存储分区(virtual memory partition),主要用于存储由主内存(main memory)所包含的非经常性需要的程序和程序数据,以允许更多的并发进程的执行。

Swap空间可由一个或多个逻辑分区、一个或多个物理磁盘、swap文件(swap file)实现。在Ubuntu的安装过程中
会提示需要创建一个swap空间,但是你也可以选择安装Ubuntu后创建,或根本不建如果你有足够的物理内存:-)

1. 如何选择swap空间的实现方式,是逻辑分区还是swap文件?
逻辑分区的实现方式较于swap文件具有更快、一定的物理容错能力的优点,但也具有日后调整空间大小存在一定的风险性的缺点。

2. 如何选择swap空间的大小?
按比较老的规则通常会将swap空间的大小定为物理内存RAM的2到3倍,这是老规则也是比较简单的规则,但也不失为一个好的开始:) 选择swap空间的大小主要取决于使用Ubuntu的主要用途、机器的物理内存和可用的磁盘空间3个因素。

问题不在于选择的结果,而在于如何选择。我的机器有1GB的RAM,也做了一个1GB的swap空间,但我发现除了在Suspend的时候会使用小部分(不足200MB)的swap空间。所以,我选择以512MBswap文件来实现swap空间,如果有人有和我一样的选择,以下以swap文件实现swap空间的方法可供参考。

Step-1 创建swap文件:
$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=524288
以上bs * count = swap空间(Bytes),这里是在/路径下创建一个名为swapfile的512MB的swap文件,swap文件在哪里并不重要。

Step-2 设置swap空间:
$ sudo mkswap /swapfile

Step-3 配置crontab启动文件:
首先,以
$ sudo crontab -e
将crontab文件设为编辑状态; 其次,在crontab文件中添加以下的一行文本
@reboot sudo swapon /swapfile

Step-4 重启

Step-5 检测swap空间的使用情况:
可以使用
$ free -m
或gkrellm (一个gui检测工具)检测swap空间的使用率。
如果对swap空间的使用率作出检测后不满意,可以按照Step-6和Step-7首先禁用swap空间,然后重新按照Step-1至Step4重新调整swap空间的大小。可以方便地调整swap空间的大小是使用swap文件实现swap空间的一个优势。

Step-6 禁用swap空间:
首先,
$ sudo swapoff /swapfile
其次,删除Step-3中添加的那一行文本。



 
### Linux `free -h` Command Usage and Explanation The `free` command provides a snapshot of the system's memory usage, including both RAM and swap space. When using `-h`, it displays all values in human-readable format (Kilobytes, Megabytes, Gigabytes). #### Syntax To use this command with human-readable output: ```bash free -h ``` This will display information about total, used, and available memory along with buffers/cache details. #### Output Columns Explained - **total**: Total installed physical or swap memory. - **used**: Memory currently being utilized by active processes. - **free**: Unused memory that is completely free to allocate immediately without needing any page eviction or swapping activity. - **shared**: Memory used by tmpfs (since version 3.18). - **buff/cache**: Sum of buffers and cache which can be quickly reclaimed when more memory is needed. - **available**: Estimation of how much memory is available for starting new applications without swapping out others; calculated from MemFree, Buffers, Cached, SReclaimable etc[^1]. For instance, running `free -h` might produce an output similar to below: ```plaintext total used free shared buff/cache available Mem: 7.7G 2.0G 4.9G 156M 804M 5.2G Swap: 2.0G 0B 2.0G ``` In this example, there are approximately 7.7 gigabytes of total RAM on the machine where around 2 gigabytes have been allocated while nearly 5 gigabytes remain unused but may still contain cached data ready for rapid reuse. --related questions-- 1. What does each column represent in the output of the `free -h` command? 2. How do buffer caches affect the reported amount of usable memory according to `free -h`'s results? 3. Can you explain what happens if my application requires more memory than shown under 'available'? 4. Is it normal for swap usage to increase even though plenty of RAM appears unallocated based on `free -h`? 5. Why would someone prefer checking memory status via `top` over simply using `free -h`?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值