Centos7使用swap文件增加虚拟内存

1、查看原有内存情况

# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        1.3G        1.9G        1.0M        432M        2.1G
Swap:            0B          0B          0B

2、选择目录并创建内存文件

#cd /usr/ && mkdir swap
#dd if=/dev/zero of=swapfile bs=100M count=50

创建swap文件命令:dd if=/dev/zero of=swapfile bs=100M count=50
这条命令从硬盘里分出一个100M ×50 = 5G 大小的空间,挂在swapfile上
注意:这里我们bs(buff size)给的100M, bs大小可以根据free -h命令查看的buff/cache的大小来决定,如果给大了可能会报dd: memory exhausted by input buffer of size 1073741824 bytes (1.0 GiB)

3、稍等一会swap文件创建完成之后使用命令:ll -h 查看

# ll -h
total 4.9G
-rw-r--r-- 1 root root 4.9G Nov 26 14:03 swapfile

4、构建swap格式于/usr/swap/swapfile 上

# mkswap /usr/swap/swapfile
Setting up swapspace version 1, size = 5119996 KiB
no label, UUID=33305af2-d6ff-42f8-a999-48cbd0dfe351

5、立即挂载虚拟内存

# swapon /usr/swap/swapfile
swapon: /usr/swap/swapfile: insecure permissions 0644, 0600 suggested.

6、重启也生效

vim /etc/fstab
/usr/swap/swapfile swap swap defaults 0 0

新增一行 /usr/swap/swapfile swap swap defaults 0 0

7、结果展示

# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        1.3G        136M        1.0M        2.2G        2.1G
Swap:          4.9G          0B        4.9G

8、解决问题

现象:yarn build时,出现error Command failed with exit code 1. 莫名退出
问题:内存不够
解决:
1、扩展物理内存
2、扩展虚拟内存

yarn build
yarn run v1.22.17
$ cross-env  NODE_ENV=production NODE_OPTIONS=--max_old_space_size=40240  vite build && esno ./build/script/postBuild.ts
vite v2.4.0-beta.2 building for production...
transforming (963) node_modules/@vue/devtools-api/lib/esm/api/api.js
warn - You have enabled the JIT engine which is currently in preview.
warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
transforming (1200) node_modules/ant-design-vue/dist/antd.less[@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead.

transforming (1555) src/assets/images/img-qiehuan.png[@vue/compiler-sfc] ::v-deep usage as a combinator has been deprecated. Use :deep(<inner-selector>) instead.

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

9、常见操作

1、检查 Swap 空间,先检查一下系统里有没有既存的 Swap 文件
swapon -s
如果返回的信息概要是空的,则表示 Swap 文件不存在。

2、确定swap文件的大小,单位为M。将该值乘以1024得到块大小。例如,64MB的swap文件的块大小是65536。

3、创建 Swap 文件,下面使用 dd 命令来创建 Swap 文件。
dd if=/dev/zero of=/swapfile bs=1024 count=4194304

【参数说明】
if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input file >
of=文件名:输出文件名,缺省为标准输出。即指定目的文件。< of=output file >
bs=bytes:同时设置读入/输出的块大小为bytes个字节
count=blocks:仅拷贝blocks个块,块大小等于bs指定的字节数。

4、创建好Swap文件,还需要格式化后才能使用。运行命令:
mkswap /swapfile

5、激活 Swap ,运行命令:
swapon /swapfile

6、如果要机器重启的时候自动挂载 Swap ,那么还需要修改 fstab 配置。
用 vim 打开 /etc/fstab 文件,在其最后添加如下一行:
/swapfile   swap   swap    defaults 0 0

当下一次系统启动时,新的swap文件就打开了。

7、添加新的swap文件并开启后,检查 cat /proc/swaps 或者free命令的输出来查看swap是否已打开。

8、最后,赋予 Swap 文件适当的权限:
chown root:root /swapfile 
chmod 0600 /swapfile

9、删除SWAP分区
swapoff  /swapfile  #卸载swap文件
并修改/etc/fstab文件 #从配置总删除
rm -rf /swapfile  #删除文件

定时清理linux缓存crontab -e
0 4 * * * sh /data/corefile/sywn/ice/free_cache.sh >> /dev/null 2>&1

#!/bin/bash
TIME=`date "+%Y-%m-%d"`
echo ""
echo ""
echo "======${TIME}开始清除缓存======"

sync;sync;sync; #写入硬盘,防止数据丢失
sleep 10; #延迟10秒

echo 1 >/proc/sys/vm/drop_caches;
echo 2 >/proc/sys/vm/drop_caches;
echo 3 >/proc/sys/vm/drop_caches;

echo "======${TIME}清除缓存完成======"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容