Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of establishing and releasing memory in SWAP partition under CentOS

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the methods of establishing and releasing memory for SWAP partitions under CentOS". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Method 1:

1. Check the current partition of the system:

> free-m

Create a file for swapping partitions:

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

3. Set up swap partition files:

> mkswap / export/swap/swapfile

Fourth, enable swapping partition files immediately:

> swapon / whateever/swap

5. To enable self-enabling at boot time, you need to modify the swap line in the file / etc/fstab:

/ whatever/swap swap swap defaults 0 0

Method two

Ways to increase swap partition space:

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

2.swapoff / dev/hd**

3.free, check to see if it has stopped.

4.fdisk deleted the stopped swap partition.

5. Re-create a new SWAP partition with FDISK

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

7.swapon / dev/hd** Open swap

8. Modify / etc/fstab

Operation 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

two。 Create a swap file in the right space

# mkdir swap

# cd swap

# dd if=/dev/zero of=swapfile bs=1024 count=10000

100000000 records in

100000000 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--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--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 1G files

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

Create as swap file

# mkswap swapfile

Let swap take effect

# swapon swapfile

Check the swap.

# swapon-s

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

Add it to the fstab file so that the system starts automatically when it boots.

# vi / etc/fstab

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

Over and out.

Second, LINUX frees memory

Careful friends will notice that when you access files frequently under linux, the physical memory will soon be used up, when the program is finished, the memory will not be released normally, but will always be used as caching. It'seems that many people are asking this question, but they don't see any good solution. Then let me talk about it.

Let's start with the 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

Where:

Total total memory

Number of memory already used by used

Number of free memory in free

Total memory shared by multiple processes in shared

Size of buffers Buffer Cache and cached Page Cache disk caches

-memory of buffers/cache: used-buffers-cached

Memory of + buffers/cache: free + buffers + cached

Available memory=free memory+buffers+cached

With this foundation, we can know that I now have a used of 163MB, a free buffer of 86MB, and a cached of 1094m.

So let's see what happens to memory 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 the execution of my command, the used was 244MB, free, 4MB, buffers, 8MB, cached, 174MB. Oh, my God, it was eaten by cached. Don't worry, 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 careful designs, in addition to caching dentry (for VFS to speed up the conversion of file pathnames to inode), but also adopts two main Cache methods: Buffer Cache and Page Cache. The former is for the read and write of disk blocks, and the latter is for the read and write of file inode. These Cache effectively shorten the time it takes to make system calls such as read,write,getdents. "

So someone said that for a while, linux will automatically release the memory used. Let's use free to try again 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

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

/ proc is a virtual file system, and we can read and write it as a means to communicate with kernel entities. In other words, the current kernel behavior can be adjusted by modifying the file in / proc. Then we can free up memory by adjusting / proc/sys/vm/drop_caches. Do the following:

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

0

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

[root@cluster /] # sync

Execute the sync command manually (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 unwritten system buffers to disk, including the modified i-node, deferred block Imax O, and read-write mapping file)

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

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

three

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 current used is 66MB, free, 182MB, buffers, 0MB, cached, 11MB. So effectively released buffer and cache.

The usage of / proc/sys/vm/drop_caches is explained 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

It has been found that the memory of the linux system has been rising these days. Even if apache and mysql are turned off, the memory will not be freed. You can use the following script to free the memory:

Content of the script:

#! / 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 the system crontab to run automatically every day:

Crontab-e

Enter the following:

00 00 * / root/Cached.sh

Free memory at 0 o'clock every day, at which time you can modify the settings according to your needs.

If you prompt an error when running. / Cached.sh: the problem with Permission denied permissions, you can run the

This is the end of the content of "what are the methods of establishing and releasing memory for SWAP partitions under CentOS". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report