Linux 增加虚拟内存
1:创建文件:dd if=/dev/zero of=/data/swapfile bs=1024 count=65536
2:激活和使用:mkswap /data/swapfile
free
swapon /data/swapfile
free
3:移除:swapoff /data/swapfile
----------------------------------------------------------
上述操作开启重启之后交换空间会变得不可用,需要在/etc/fstab中增加自动挂载设置
/data/swapfile none swap sw 0 0
示例:
系统运行过程中一直报错如下
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000d92f0000, 264314880, 0) failed; error='无法分配内存' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 264314880 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /usr/local/Tomcat_Trans/bin/hs_err_pid10365.log
程序异常中止,查看内存,发现内存配置是一样的(系统迁移),但是竟然报内存不足,free看一下,发现在新系统上没有增加虚拟内存,增加一个2GB的虚拟内存再观察看下,具体如下:
[root@pacteralinux ~]# dd if=/dev/zero of=/root/swapfile bs=1024 count=2048000^C
[root@pacteralinux ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 29954008 21352544 7079852 76% /
tmpfs 794380 0 794380 0% /dev/shm
/dev/sdb1 72246600 4014596 64562092 6% /mnt/resource
/dev/sdc1 379509584 199164 360032424 1% /mnt/resource/mysqldataper
/dev/sdc2 136543368 192128 129415180 1% /mnt/resource/sdc3mnt
[root@pacteralinux ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 21G 6.8G 76% /
tmpfs 776M 0 776M 0% /dev/shm
/dev/sdb1 69G 3.9G 62G 6% /mnt/resource
/dev/sdc1 362G 195M 344G 1% /mnt/resource/mysqldataper
/dev/sdc2 131G 188M 124G 1% /mnt/resource/sdc3mnt
[root@pacteralinux ~]# touch /mnt/resource/sdc3mnt/swapfile
[root@pacteralinux ~]# dd id=/dev/zero of=/mnt/resource/sdc3mnt/swapfile bs=1024 count=2048000
dd: unrecognized operand `id=/dev/zero'
Try `dd --help' for more information.
[root@pacteralinux ~]# dd if=/dev/zero of=/mnt/resource/sdc3mnt/swapfile bs=1024 count=2048000
2048000+0 records in
2048000+0 records out
2097152000 bytes (2.1 GB) copied, 134.705 s, 15.6 MB/s
[root@pacteralinux ~]# cd /mnt/resource/sdc3mnt/
[root@pacteralinux sdc3mnt]# ll -h
total 2.0G
drwx------. 2 root root 16K Dec 2 14:20 lost+found
-rw-r--r--. 1 root root 2.0G Dec 7 23:19 swapfile
[root@pacteralinux sdc3mnt]# mkswap swapfile
mkswap: swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 2047996 KiB
no label, UUID=7e85b4b9-5473-4fad-afbe-5b989890e81b
[root@pacteralinux sdc3mnt]# free -m
total used free shared buffers cached
Mem: 1551 1452 99 0 4 635
-/+ buffers/cache: 812 739
Swap: 0 0 0
[root@pacteralinux sdc3mnt]# swapon swapfile
[root@pacteralinux sdc3mnt]# free -m
total used free shared buffers cached
Mem: 1551 1453 97 0 4 635
-/+ buffers/cache: 813 737
Swap: 1999 0 1999
[root@pacteralinux sdc3mnt]#