Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does linux expand virtual memory

Shulou Source: shulou.com Published: 2022-06-01 14:31:32 10月22日 Update

This article mainly explains "how to expand virtual memory by linux". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how linux expands virtual memory".

When the linux disk is first partitioned, we will partition a swap partition, which acts as a piece of virtual memory and plays the role of expanding memory. However, in our work, we will encounter the situation that the virtual memory is not enough, at this time we can not re-partition the disk, we can only add a swap file to expand our virtual memory. The following editor will bring you a tutorial on how to expand virtual memory. Let's go and have a look!

1. Check the size of the existing swap space

If you want to check the size of your existing virtual memory, use the command 'free-m'

[root@chenfancentos] # free-m

Total used free shared buffers cached

Mem: 988 164 823 0 26 48

-/ + buffers/cache: 90898

Swap: 1999 0 1999

According to the size of the existing swap space, choose the appropriate expansion size.

two。 Create a suitable swapfile

We can often use the "dd" command to create a file, use "if" to specify the source, basically can not write anything other than "/ dev/zero", and / dev/zero is a file unique to the UNIX system. "of" specifies the target file, "bs" defines the size of blocks, and "count" defines the number of blocks, the number of these two parameters determines the size of the target file.

[root@chenfancentos ~] # dd if=/dev/zero of=/tmp/swapfile bs=300M count=1

The 1: 0 reading was recorded.

Recorded the write-out of 1: 0

314572800 bytes (315 MB) replicated, 13.9027 seconds, 22.6 MB/ seconds

[root@chenfancentos] # du-h / tmp/swapfile

300M / tmp/swapfile

You can see that a new swapfile file is generated with a size of 300m.

3. Format the swapfile you just created

[root@chenfancentos] # mkswap-f / tmp/swapfile

Setting up swapspace version 1, size = 307196 KiB

No label, UUID=c4e2a8b1-fa09-41ab-bcbf-63c273ed9598

You can see the successful UUID message after formatting

4. Realize automatic mounting of exchange files when booting

If we add the entry / tmp/swapfile swap swap defaults 00 to / etc/fstab, we will be able to boot and mount automatically.

[root@chenfancentos ~] # cat / etc/fstab

# / etc/fstab

# Created by anaconda on Sat Apr 2 19:37:01 2016

#

# Accessible filesystems, by reference, are maintained under'/ dev/disk'

# See man pages fstab (5), findfs (8), mount (8) and/or blkid (8) for more info

#

UUID=301b2f9f-91d3-47dd-8a43-d377cba1cd5c / ext4 defaults 1 1

UUID=812455f9-a63f-4b09-9336-05e3ada6f9a7 / boot ext4 defaults 12

UUID=416480d0-662c-406c-a328-b4c2f370577b swap swap defaults 0 0

Tmpfs / dev/shm tmpfs defaults 0 0

Devpts / dev/pts devpts gid=5,mode=620 0 0

Sysfs / sys sysfs defaults 0 0

Proc / proc proc defaults 0 0

/ tmp/swapfile swap swap defaults 0 0

In this way, even if it is restarted later, it can be mounted automatically.

5. Enable swapfile to view virtual memory size

[root@chenfancentos ~] # swapon / tmp/swapfile

[root@chenfancentos] # free-m

Total used free shared buffers cached

Mem: 988 136 852 0 10 50

-/ + buffers/cache: 74 913

Swap: 2299 0 2299

It is exciting to see that compared to the initial virtual memory, there is now an increase of 300m. Aren't you a little excited.

At this point, I believe you have a deeper understanding of "linux how to expand virtual memory", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Memory size file content command method time format target disk space learning check appropriate practical deeper excited not enough two function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information vpn MariaDB Huawei