方法一:
一、查看系統(tǒng)當(dāng)前的分區(qū)情況:
>free -m
二、創(chuàng)建用于交換分區(qū)的文件:
>dd if=/dev/zero of=/whatever/swap bs=block_size (10M)count=number_of_block(3000)
三、設(shè)置交換分區(qū)文件:
>mkswap /export/swap/swapfile
四、立即啟用交換分區(qū)文件:
>swapon /whateever/swap
五、若要想使開(kāi)機(jī)時(shí)自啟用,則需修改文件/etc/fstab中的swap行:
/whatever/swap swap swap defaults 0 0
方法二
增加交換分區(qū)空間的方法:
1.查看一下/etc/fstab確定目前的分區(qū)
2.swapoff /dev/hd**
3.free 看一下是不是停了.
4.fdisk 刪了停掉的swap分區(qū)
5.重新用FDISK建一個(gè)新的SWAP分區(qū)
6.mkswap /dev/hd**把新的分區(qū)做成swap
7.swapon /dev/hd**打開(kāi)swap
8.修改/etc/fstab
操作實(shí)例:
1.查看系統(tǒng)Swap空間使用
# free
total used free shared buffers cached
Mem: 513980 493640 20340 0 143808 271780
-/+ buffers/cache: 78052 435928
Swap: 1052248 21256 1030992
2.在空間合適處創(chuàng)建swap文件
# mkdir swap
# cd swap
# dd if=/dev/zero of=swapfile bs=1024 count=10000
10000+0 records in
10000+0 records out
# ls -al
total 10024
drwxr-xr-x 2 root root 4096 7月 28 14:58 .
drwxr-xr-x 19 root root 4096 7月 28 14:57 ..
-rw-r--r-- 1 root root 10240000 7月 28 14:58 swapfile
# mkswap swapfile
Setting up swapspace version 1, size = 9996 KiB
3.激活swap文件
# swapon swapfile
# ls -l
total 10016
-rw-r--r-- 1 root root 10240000 7月 28 14:58 swapfile
# free
total used free shared buffers cached
Mem: 513980 505052 8928 0 143900 282288
-/+ buffers/cache: 78864 435116
Swap: 1062240 21256 1040984
生成1G的文件
# dd if=/dev/zero of=swapfile bs=10M count=3000
創(chuàng)建為swap文件
#mkswap swapfile
讓swap生效
#swapon swapfile
查看一下swap
#swapon -s
[root@cluster /]# swapon -sFilenameTypeSizeUsedPriority/dev/sda3 partition10201161728-1/state/partition1/swap/swapfile file307199920-2
加到fstab文件中讓系統(tǒng)引導(dǎo)時(shí)自動(dòng)啟動(dòng)
#vi /etc/fstab
/state/partition1/swap/swapfil swap swap defaults 0 0
完畢。
二,LINUX釋放內(nèi)存
細(xì)心的朋友會(huì)注意到,當(dāng)你在linux下頻繁存取文件后,物理內(nèi)存會(huì)很快被用光,當(dāng)程序結(jié)束后,內(nèi)存不會(huì)被正常釋放,而是一直作為caching.這個(gè)問(wèn)題,貌似有不少人在問(wèn),不過(guò)都沒(méi)有看到有什么很好解決的辦法.那么我來(lái)談?wù)勥@個(gè)問(wèn)題.
先來(lái)說(shuō)說(shuō)free命令
[root@cluster /]# free -m
total used free shared buffers cached
Mem: 31730 31590 139 0 37 27537
-/+ buffers/cache: 4015 27714
Swap: 30996 1 30994
其中:
total 內(nèi)存總數(shù)
used 已經(jīng)使用的內(nèi)存數(shù)
free 空閑的內(nèi)存數(shù)
shared 多個(gè)進(jìn)程共享的內(nèi)存總額
buffers Buffer Cache和cached Page Cache 磁盤(pán)緩存的大小
-buffers/cache 的內(nèi)存數(shù):used - buffers - cached
+buffers/cache 的內(nèi)存數(shù):free + buffers + cached
可用的memory=free memory+buffers+cached
有了這個(gè)基礎(chǔ)后,可以得知,我現(xiàn)在used為163MB,free為86,buffer和cached分別為10,94
那么我們來(lái)看看,如果我執(zhí)行復(fù)制文件,內(nèi)存會(huì)發(fā)生什么變化.
[root@cluster /]# cp -r /etc ~/test/
[root@cluster /]# free -m
total used free shared buffers cached
Mem: 31730 31590 139 0 37 27537
-/+ buffers/cache: 4015 27714
Swap: 30996 1 30994
在我命令執(zhí)行結(jié)束后,used為244MB,free為4MB,buffers為8MB,cached為174MB,天吶都被cached吃掉了.別緊張,這是為了提高文件讀取效率的做法.
引用[url]http://www.2qyou.com/thread-591-1-1.html[/url] 為了提高磁盤(pán)存取效率, Linux做了一些精心的設(shè)計(jì), 除了對(duì)dentry進(jìn)行緩存(用于VFS,加速文件路徑名到inode的轉(zhuǎn)換), 還采取了兩種主要Cache方式:Buffer Cache和Page Cache。前者針對(duì)磁盤(pán)塊的讀寫(xiě),后者針對(duì)文件inode的讀寫(xiě)。這些Cache有效縮短了 I/O系統(tǒng)調(diào)用(比如read,write,getdents)的時(shí)間。"
那么有人說(shuō)過(guò)段時(shí)間,linux會(huì)自動(dòng)釋放掉所用的內(nèi)存,我們使用free再來(lái)試試,看看是否有釋放>?
[root@cluster /]# free -m
total used free shared buffers cached
Mem: 31730 31590 139 0 37 27537
-/+ buffers/cache: 4015 27714
Swap: 30996 1 30994
MS沒(méi)有任何變化,那么我能否手動(dòng)釋放掉這些內(nèi)存呢???回答是可以的!
/proc是一個(gè)虛擬文件系統(tǒng),我們可以通過(guò)對(duì)它的讀寫(xiě)操作做為與kernel實(shí)體間進(jìn)行通信的一種手段.也就是說(shuō)可以通過(guò)修改/proc中的文件,來(lái)對(duì)當(dāng)前kernel的行為做出調(diào)整.那么我們可以通過(guò)調(diào)整/proc/sys/vm/drop_caches來(lái)釋放內(nèi)存.操作如下:
[root@cluster /]# cat /proc/sys/vm/drop_caches
0
首先,/proc/sys/vm/drop_caches的值,默認(rèn)為0
[root@cluster /]# sync
手動(dòng)執(zhí)行sync命令(描述:sync 命令運(yùn)行 sync 子例程。如果必須停止系統(tǒng),則運(yùn)行 sync 命令以確保文件系統(tǒng)的完整性。sync 命令將所有未寫(xiě)的系統(tǒng)緩沖區(qū)寫(xiě)到磁盤(pán)中,包含已修改的 i-node、已延遲的塊 I/O 和讀寫(xiě)映射文件)
[root@server test]# echo 3 > /proc/sys/vm/drop_caches
[root@server test]# cat /proc/sys/vm/drop_caches
3
將/proc/sys/vm/drop_caches值設(shè)為3
[root@server test]# free -m
total used free shared buffers cached
Mem: 249 66 182 0 0 11
-/+ buffers/cache: 55 194
Swap: 511 0 511
再來(lái)運(yùn)行free命令,發(fā)現(xiàn)現(xiàn)在的used為66MB,free為182MB,buffers為0MB,cached為11MB.那么有效的釋放了buffer和cache.
有關(guān)/proc/sys/vm/drop_caches的用法在下面進(jìn)行了說(shuō)明
/proc/sys/vm/drop_caches (since Linux 2.6.16)
Writing to this file causes the kernel to drop clean caches,
dentries and inodes from memory, causing that memory to become free.
To free pagecache, use echo 1 > /proc/sys/vm/drop_caches;
to free dentries and inodes, use echo 2 > /proc/sys/vm/drop_caches;
to free pagecache, dentries and inodes, use echo 3 > /proc/sys/vm/drop_caches.
Because this is a non-destructive operation and dirty objects
這幾天發(fā)現(xiàn)linux系統(tǒng)內(nèi)存一直漲,即使把a(bǔ)pache和mysql關(guān)閉了,內(nèi)存也不釋放,可以使用以下腳本來(lái)釋放內(nèi)存:
腳本內(nèi)容:
#!/bin/sh
# cache釋放:
# To free pagecache:
/bin/sync
/bin/sync
#echo 1 > /proc/sys/vm/drop_caches
# To free dentries and inodes:
#echo 2 > /proc/sys/vm/drop_caches
# To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
利用系統(tǒng)crontab實(shí)現(xiàn)每天自動(dòng)運(yùn)行:
crontab -e
輸入以下內(nèi)容:
00 00 * * * /root/Cached.sh
每天0點(diǎn)釋放一次內(nèi)存,這個(gè)時(shí)間可以根據(jù)自己需要修改設(shè)置
在運(yùn)行./Cached.sh時(shí)如果提示錯(cuò)誤:Permission denied 權(quán)限的問(wèn)題,可以運(yùn)行