Create and release memory for SWAP partitions in CentOS

Source: Internet
Author: User

Method 1:


1. view the current partition of the system:

> Free-m
2. Create a file for swap partition:

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

3. Set swap partition files:
> Mkswap/export/swap/swapfile

4. Enable swap partition file now:
> Swapon/whateever/swap

5. If you want to enable the function at startup, modify the swap lines in the file/etc/fstab:

/Whatever/swap defaults 0 0


Method 2
To increase the swap partition space:
1. Check/etc/fstab to confirm the current partition.
2. swapoff/dev/hd **
3. check whether it is stopped.
4. fdisk deleted the stopped swap partition.
5. Use FDISK to create a new SWAP partition.
6. mkswap/dev/hd ** make the new partition into a swap
7. swapon/dev/hd ** open swap
8. Modify/etc/fstab



Operation instance:
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 in a suitable 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 Root 4096 July 28 14:58.
Drwxr-xr-x 19 root Root 4096 July 28 14:57 ..
-Rw-r -- 1 root Root 10240000 July 28 14:58 swapfile


# Mkswap swapfile
Setting up swapspace version 1, size = 9996 KiB

3. Activate the swap file
# Swapon swapfile
# Ls-l
Total 10016
-Rw-r -- 1 root Root 10240000 July 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

Generate 1 GB files

# Dd if =/dev/zero of = swapfile bs = 10 M count = 3000

Create as a swap file

# Mkswap swapfile

Make swap effective

# Swapon swapfile
View swap
# Swapon-s
[Root @ cluster/] # swapon-sFilenameTypeSizeUsedPriority/dev/sda3 partition10201161728-1/state/partition1/swap/swapfile file307199920-2
Added to the fstab file to enable automatic startup during system boot.
# Vi /Etc/fstab
/State/partition1/swap/swapfil swap Swap Ults 0 0



Complete.

Ii. release memory in LINUX


Careful friends will notice that when you areAfter files are frequently accessed in linux, the physical memory will soon be used up. When the program ends, the memory will not be normally released, but will always be used as caching. it seems that many people are asking this question, but they have not seen any good solutions. let me talk about this.

Let's talk about the free command first.


[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


Where:

Total memory

Used memory used

Free idle memory

Total memory shared by multiple processes

Buffers Buffer Cache and cached Page Cache disk Cache size

-Buffers/cache memory: used-buffers-cached

+ Buffers/cache memory: free + buffers + cached

Available memory = free memory + buffers + cached

With this foundation, we can know that I now used is 163 MB, free is 86, buffer and cached are respectively 10, 94

Let's take a look at the memory changes if I copy the 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


After I run the command, used is 244 MB, free is 4 MB, buffers is 8 MB, and cached is 174 MB. don't be nervous. This is to improve the efficiency of File Reading.

Reference [url] http://www.2qyou.com/thread-591-1-1.html#/url] in order to improve disk access efficiency, Linux has made some elaborate designs, in addition to caching dentry (for VFS, accelerating the conversion of file pathnames to inode ), two major Cache methods are also adopted: Buffer Cache and Page Cache. The former is used to read and write disk blocks, and the latter is used to read and write inode files. These caches effectively shorten the time for I/O system calls (such as read, write, getdents. "

So some people have said that linux will automatically release the memory used for a while. Let's try again with free to see if there is any 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


MS remains unchanged. Can I manually release the memory ??? The answer is yes!

/Proc is a virtual file system. We can use its read/write operations as a means to communicate with the kernel object. in other words, you can modify the file in/proc to adjust the current kernel behavior. then we can release the memory by adjusting/proc/sys/vm/drop_caches. the procedure is as follows:

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

0

First, the value of/proc/sys/vm/drop_caches. The default value is 0.

[Root @ cluster/] # sync

Run the sync command manually (Description: The sync command runs the sync subroutine. If you must stop the system, run the sync command to ensure the integrity of the file system. The sync command writes all unwritten system buffers to the disk, including modified I-nodes, delayed block I/O, and read/write ing files)

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

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

3

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

Run the free command and find that the current used is 66 MB, free is 182 MB, buffers is 0 MB, and cached is 11 MB. This effectively releases the 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 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



In the past few days, we have found that the linux system memory has been increasing. Even if apache and mysql are disabled, the memory will not be released. You can use the following script to release the memory:

Script content:



#! /Bin/sh


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


Use the system crontab to run automatically every day:

crontab -e

Enter the following content:

  1. 00 00 ***/root/Cached. sh

The memory is released at every day. You can modify the settings as needed.

When you run./Cached. sh, you can run


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.