CentOS下SWAP分區建立與釋放記憶體的教程

來源:互聯網
上載者:User

方法一:

一、查看系統當前的分區情況:

>free -m

二、建立用於交換分區的檔案:

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

三、設定交換分區檔案:

>mkswap /export/swap/swapfile

四、立即啟用交換分區檔案:

>swapon /whateever/swap

五、若要想使開機時自啟用,則需修改檔案/etc/fstab中的swap行:

/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 許可權的問題,可以運行

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.