A tutorial on setting up and releasing memory in the CentOS swap section

Source: Internet
Author: User
Tags centos

Method One:

First, view the current system partition situation:

>free-m

Create a file for the swap partition:

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

Third, set up swap partition files:

>mkswap/export/swap/swapfile

Four, immediately enable the swap partition file:

>swapon/whateever/swap

V. To change the swap line in the file/etc/fstab if you want to enable it at boot time:

/whatever/swap Swap Defaults 0 0

Method Two

Ways to increase swap space:

1. Check the/etc/fstab to determine the current partition

2.swapoff/dev/hd**

3.free see if it's stopped.

4.fdisk deleted the stopped swap partition

5. Re-use Fdisk to build a new swap partition

6.mkswap/dev/hd** the new partition into swap.

7.swapon/dev/hd** Open Swap

8. Modify/etc/fstab

Action Example:

1. View System swap space usage

# free

Total used free shared buffers Cached

mem:513980 493640 20340 0 143808 271780

-/+ buffers/cache:78052 435928

swap:1052248 21256 1030992

2. Create a swap file at the appropriate place in space

# 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 4096 July 28 14:58.

Drwxr-xr-x root 4096 July 28 14:57.

-rw-r--r--1 root root 10240000 July 14:58 swapfile

# Mkswap Swapfile

Setting up Swapspace version 1, size = 9996 KiB

3. Activate swap file

# Swapon Swapfile

# ls-l

Total 10016

-rw-r--r--1 root root 10240000 July 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

Generate 1G of files

# dd If=/dev/zero of=swapfile bs=10m count=3000

Create as swap file

#mkswap Swapfile

Let swap take effect

#swapon Swapfile

Check out Swap

#swapon-S

[Root@cluster/]# Swapon-sfilenametypesizeusedpriority/dev/sda3 partition10201161728-1/state/partition1/swap/ Swapfile file307199920-2

Add to Fstab file to allow system to boot automatically when booting

#vi/etc/fstab

/state/partition1/swap/swapfil Swap Defaults 0 0

Complete.

Second, Linux frees up memory

Careful friends will notice that when you frequently access files under Linux, physical memory will soon be used up, and when the program is finished, memory will not be released normally, but as a caching. This question seems to be a lot of people asking, But I don't see any good solutions. Then let me talk about this.

Let's say free command.

[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

which

Total Memory

used the number of memory already in use

Free amount of memory

Total memory shared by multiple processes

Buffers buffer cache and cached Page cache disk size

Number of-buffers/cache Memory: used-buffers-cached

+buffers/cache Memory: Free + buffers + Cached

Available Memory=free memory+buffers+cached

With this foundation, it can be learned that I now used for 163mb,free for 86,buffer and cached respectively for 10,94

So let's see what happens to memory if I execute the copy file.

[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

At the end of my command, used for 244mb,free for the 4mb,buffers 8mb,cached for 174MB, the days are cached eaten. Don't be nervous, this is to improve the efficiency of the file reading practice.

reference [Url]http://www.2qyou.com/thread-591-1-1.html[/url] to improve disk access efficiency, Linux has done some careful design, in addition to caching dentry (for VFS, Accelerates file path name to Inode conversion), also takes two main cache methods: Buffer cache and Page cache. The former is read and write to disk block, the latter is read and write to the file inode. These cache effectively shorten the time of the I/O system call (such as read,write,getdents). "

So someone said that some time, Linux will automatically release the memory used, we use free to try again to see if there is release?

[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

There is no change in MS, so can I manually release the memory??? The answer is OK!

/proc is a virtual file system that we can use to communicate with kernel entities through its read and write operations. That is, you can make adjustments to the current kernel behavior by modifying the files in/proc. Then we can adjust the/PROC/SYS/VM /drop_caches to free memory. The operation is as follows:

[Root@cluster/]# Cat/proc/sys/vm/drop_caches

0

First, the/proc/sys/vm/drop_caches value defaults to 0.

[Root@cluster/]# Sync

Manually perform the sync command (description: The sync command runs the Sync subroutine.) If the system must be stopped, run the sync command to ensure the integrity of the file system. The Sync command writes all of the unused system buffers to disk, including modified I-node, deferred block I/O, and read-write mapping files.

[Root@server test]# echo 3 >/proc/sys/vm/drop_caches

[Root@server test]# Cat/proc/sys/vm/drop_caches

3

Set the/proc/sys/vm/drop_caches value to 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

Then run the free command and find that the used is now 66mb,free for 182mb,buffers 0mb,cached 11MB. So effectively released buffer and cache.

The usage of/proc/sys/vm/drop_caches is described below

/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 which 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

These days found that Linux system memory has been up, even if the Apache and MySQL shut down, Memory is not released, you can use the following script to free memory:

Script content:

#!/bin/sh

# Cache Release:

# 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

Use System crontab to achieve automatic daily operation:

Crontab-e

Enter the following content:

* * * */root/cached.sh

Free memory at 0 per day, which can be modified to suit your needs.

If you are prompted for an error when running./cached.sh: Permission denied permissions, you can run

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.