首页 > 代码库 > virtual memory exhausted: Cannot allocate memory

virtual memory exhausted: Cannot allocate memory

~$free             total       used       free     shared    buffers     cachedMem:       1017832     784328     233504        356      12844      14692-/+ buffers/cache:     756792     261040Swap:            0          0          0~$dd if=/dev/zero of=swap bs=1024 count=10240001024000+0 records in1024000+0 records out1048576000 bytes (1.0 GB) copied, 6.30189 s, 166 MB/s~$ll swap-rw-rw-r-- 1 ubuntu ubuntu 1048576000 Oct 18 02:48 swap~$mkswap swap Setting up swapspace version 1, size = 1023996 KiBno label, UUID=1372d681-b265-469b-8d9d-d637cbedbef7~$sudo swapon swap~$free             total       used       free     shared    buffers     cachedMem:       1017832     947088      70744        356       1692     189112-/+ buffers/cache:     756284     261548Swap:      1023996          0    1023996~$sudo swapoff swap~$free             total       used       free     shared    buffers     cachedMem:       1017832     947844      69988        356       1744     189728-/+ buffers/cache:     756372     261460Swap:            0          0          0

创建一个名为swap的文件作为交换区.

首先要设置该文件的大小,使用dd文件复制命令从if复制到of,复制count次,每次复制bs大小的空间.

然后将swap文件格式化为交换区,使用"mkswap 文件名"命令,该命令会是swap文件腾出4*1024B(即为4页)的空间专门作为交换区头部.

最后"swapon 文件名"命令打开交换区,此时查看内存就能够看见swap区的大小了.

不想用交换区之后,用swapoff关闭交换区,关闭之后就可"rm swap"删除交换区了.

virtual memory exhausted: Cannot allocate memory