歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux 增加虛擬內存

Linux 增加虛擬內存

日期:2017/2/28 14:38:05   编辑:Linux教程

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]#

推薦閱讀:

Linux內存管理之高端內存 http://www.linuxidc.com/Linux/2013-06/85693.htm

Linux內存管理之分段機制 http://www.linuxidc.com/Linux/2012-11/74480.htm

Linux內存管理伙伴算法 http://www.linuxidc.com/Linux/2012-09/70711.htm

Linux內存管理-淺談物理內存與虛擬內存 http://www.linuxidc.com/Linux/2013-08/88290.htm

Copyright © Linux教程網 All Rights Reserved