CentOS下SWAP分區創建及釋放內存詳解

方法一:html

1、查看系統當前的分區狀況:node

>free -mmysql

2、建立用於交換分區的文件:linux

>dd if=/dev/zero of=/whatever/swap bs=block_size (10M)count=number_of_block(3000)sql

3、設置交換分區文件:apache

>mkswap  /export/swap/swapfile緩存

4、當即啓用交換分區文件:this

>swapon /whateever/swapurl

5、若要想使開機時自啓用,則需修改文件/etc/fstab中的swap行:spa

/whatever/swap swap swap defaults 0 0

方法二

增長交換分區空間的方法:

1.查看一下/etc/fstab肯定目前的分區

2.swapoff /dev/hd**

3.free 看一下是否是停了.

4.fdisk 刪了停掉的swap分區

5.從新用FDISK建一個新的SWAP分區

6.mkswap /dev/hd**把新的分區作成swap

7.swapon /dev/hd**打開swap

8.修改/etc/fstab

操做實例:

1.查看系統Swap空間使用

# free

total used free shared buffers cached

Mem: 513980 493640 20340 0 143808 271780

-/+ buffers/cache: 78052 435928

Swap: 1052248 21256 1030992

2.在空間合適處建立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

建立爲swap文件

#mkswap swapfile

讓swap生效

#swapon swapfile

查看一下swap

#swapon -s

[root@cluster /]# swapon -sFilenameTypeSizeUsedPriority/dev/sda3                               partition10201161728-1/state/partition1/swap/swapfile         file307199920-2

加到fstab文件中讓系統引導時自動啓動

#vi /etc/fstab

/state/partition1/swap/swapfil   swap swap defaults 0 0

完畢。

二,LINUX釋放內存

細心的朋友會注意到,當你在linux下頻繁存取文件後,物理內存會很快被用光,當程序結束後,內存不會被正常釋放,而是一直做爲caching.這個問題,貌似有很多人在問,不過都沒有看到有什麼很好解決的辦法.那麼我來談談這個問題.

先來講說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 內存總數

used 已經使用的內存數

free 空閒的內存數

shared 多個進程共享的內存總額

buffers Buffer Cache和cached Page Cache 磁盤緩存的大小

-buffers/cache 的內存數:used - buffers - cached

+buffers/cache 的內存數:free + buffers + cached

可用的memory=free memory+buffers+cached

有了這個基礎後,能夠得知,我如今used爲163MB,free爲86,buffer和cached分別爲10,94

那麼咱們來看看,若是我執行復制文件,內存會發生什麼變化.

[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

在我命令執行結束後,used爲244MB,free爲4MB,buffers爲8MB,cached爲174MB,天吶都被cached吃掉了.別緊張,這是爲了提升文件讀取效率的作法.

引用[url]http://www.2qyou.com/thread-591-1-1.html[/url] 爲了提升磁盤存取效率, Linux作了一些精心的設計, 除了對dentry進行緩存(用於VFS,加速文件路徑名到inode的轉換), 還採起了兩種主要Cache方式:Buffer Cache和Page Cache。前者針對磁盤塊的讀寫,後者針對文件inode的讀寫。這些Cache有效縮短了 I/O系統調用(好比read,write,getdents)的時間。"

那麼有人說過段時間,linux會自動釋放掉所用的內存,咱們使用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

MS沒有任何變化,那麼我可否手動釋放掉這些內存呢???回答是能夠的!

/proc是一個虛擬文件系統,咱們能夠經過對它的讀寫操做作爲與kernel實體間進行通訊的一種手段.也就是說能夠經過修改/proc中的文件,來對當前kernel的行爲作出調整.那麼咱們能夠經過調整/proc/sys/vm/drop_caches來釋放內存.操做以下:

[root@cluster /]# cat /proc/sys/vm/drop_caches

0

首先,/proc/sys/vm/drop_caches的值,默認爲0

[root@cluster /]# sync

手動執行sync命令(描述:sync 命令運行 sync 子例程。若是必須中止系統,則運行 sync 命令以確保文件系統的完整性。sync 命令將全部未寫的系統緩衝區寫到磁盤中,包含已修改的 i-node、已延遲的塊 I/O 和讀寫映射文件)

[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值設爲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

再來運行free命令,發現如今的used爲66MB,free爲182MB,buffers爲0MB,cached爲11MB.那麼有效的釋放了buffer和cache.

有關/proc/sys/vm/drop_caches的用法在下面進行了說明

/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

這幾天發現linux系統內存一直漲,即便把apache和mysql關閉了,內存也不釋放,可使用如下腳原本釋放內存:

腳本內容:

#!/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

利用系統crontab實現天天自動運行:

crontab -e

輸入如下內容:

00 00 * * * /root/Cached.sh  

天天0點釋放一次內存,這個時間能夠根據本身須要修改設置

在運行./Cached.sh時若是提示錯誤:Permission denied 權限的問題,能夠運行

come from: http://www.jb51.net/os/RedHat/518871.html

相關文章
相關標籤/搜索