Linux Administrator Handbook (4) _unix Linux

Source: Internet
Author: User

This chapter describes the memory management features of Linux, namely virtual memory and disk caching. Describe what the system administrator should consider, work, and purpose.

What is virtual memory?

Linux supports virtual memory by using disk as an extension of RAM, allowing available memory to expand effectively. The core saves the memory blocks that are not currently in use to the hard drive, freeing up memory for other purposes. When the original content is to be used again, read back the memory. This is transparent to the user: A program running on Linux sees only a large amount of available memory and is unwilling to part with the disk. Of course, read and write hard drives are slower than real memory (slow-thousand), so programs run slower. This part of the hard disk used for virtual memory is called swap space.

Linux can use a normal file in a file system or a separate partition as a swap space. Swapping partitions is faster, but swapping files is easier to resize (without having to repartition the hard disk). If you know how much to swap space, you should use the swap zoning, if not confirmed, you can first use the swap file, after a period of time and then according to the required space to establish a swap division.

Linux allows you to use multiple swap partitions and/or swap files at the same time. That is, if you need more swap space occasionally, you can create an additional swap file at any time.

Create a Swap space

The swap file is a common document, there is nothing special about the core. The only difference is that it has no holes, ready with mkswap. Must be on a local disk, not in a file system that is Mount via NFS.

About the hole, is important. The swap file retains disk space so that the core can quickly swap out a page without having to go through all the things like a file's location disk sector. The core only uses all sectors assigned to this file. Because the holes in the file mean that there is no disk sector allocated for this location in the file, this is not good for core use.

A good way to produce a swap file without holes is to pass the following command:

$ dd If=/dev/zero Of=/extra-swap bs=1024 count=1024
1024+0 Records in
1024+0 Records out
$
/extra-swap is the swap filename, the size is given by count=. The best size is a multiple of 4, because the core writes out the memory page is 4KB. If it is not a multiple of 4, the last few KB will not be available.

There's nothing special about swapping partitions. The only difference is that it is used as the original partition, that is, there is no file system, it is best to mark the swap partition as type (Linux swap), although this has no effect on the core, but this makes the partition list clearer.

After you create a swap file or swap the partition, you need to write a tag to use it, which includes some of the management information to be used at the core. The command is Mkswap and is used as follows:

$ mkswap/extra-swap 1024
Setting up swapspace, size = 1044480 bytes
$

Note that the swap space is now useless, it exists, but the core is not available it provides virtual memory.

Be sure to use mkswap carefully, because it does not check whether the file or partition is used by something else. You can easily cover important files and partitions with Mkswap! Fortunately, you only need to use Mkswap when you install the system.

Linux memory management limits each swap space by about 127MB (the actual limit is 127.6875MB for technical reasons). You can use up to 16 swap space at the same time, a total of almost 2GB.

Use swap space

Use Swapon to make an initialized swap space available. This command tells the core to swap space can be used, the path of the space as a parameter, start a temporary swap file can use the following command:

$ swapon/extra-swap
$


If the swap space is included in the/etc/fstab, it can be used automatically.

/dev/hda8 None swap SW 0 0
/swapfile None swap SW 0 0

Start the Manuscript Run command swapon-a, which will start all the swap spaces listed in the/etc/fstab. Therefore, the Swapon command is only used when additional swap space is initiated.
You can use free to monitor swap space, it will give all the use of the swap space.

$ free
Total used free shared buffers
mem:15152 14896 256 12404 2528
-/+ buffers:12368 2784
swap:32452 6684 25768
$

The previous line of output (Mem:) shows physical memory. The total column does not show the physical memory used by the core (typically around 1MB). The Used column shows the total amount of memory being used (the second row does not count as buffering). The free column shows all unused memory. Shared columns show the total amount of memory shared by multiple processes. The Buffers column displays the current size of the disk cache.
The following line (Swap:) on the swap space, display information similar to the above. If this behavior is 0, then swap space is not used.

The same information can be obtained through top, or/proc/meminfo files using the proc file system. It is still difficult to get the use information of a swap space.

Swapoff can be used to cancel the swap space, generally do not have to do so, unless the temporary swap space. The page to be used in the swap space is swapped (Swap->ram), and if there is not enough physical memory, it is swapped out (Ram->swap, to other swap space). If there is not enough virtual memory to put in all the pages, Linux will start to oscillate (thrash); It should be able to recover after a long time, but the system is not available at this time. Before canceling a swap space, you should check to see if there is enough physical memory (for example, free).

All swap spaces automatically used by Swapon-a can be canceled with swapoff-a. It looks at the file/etc/fstab know what to cancel. Any hand-made swap space will still be used.

Even with a lot of free physical memory, there are times when many swap spaces are used. This situation is due to the need for swapping at some point, but later a large process that takes up a lot of physical memory terminates and frees up memory. They are not automatically swapped until the swapped out data is to be used. There is no need to worry about this, but it is more reassuring to know why this is happening.

Share swap space with other operating systems

Many operating systems have built-in virtual memory. Since they only need to be used at run time, that is, not at the same time, all other swap spaces are wasted except for the current operation. It would be more effective if they shared the same swap space. This is possible, but requires some hacking work. TIPS-HOWTO contains some advice on how to accomplish this task.

Allocate swap space

You may be told that you should allocate twice times the amount of physical memory swap space, but this is a false rule. Here's how to do it correctly:

Estimate all of your memory requirements. This is the maximum amount you may need, that is, the sum of memory requirements required by all programs that you want to run concurrently. You can try all the programs that you might run at the same time.

For example, if you want to run X, you have to allocate 8MB to him, GCC requires a few MB (some files may occasionally require a lot, dozens of MB, but generally 4MB almost), and so on. The core itself uses 1mb,shell and some gadgets may take hundreds of KB (or, in total, 1MB). Do not have to be too precise, rough estimate on the line, but can be more pessimistic to consider.

Remember that if more than one person uses the system at the same time, they will consume memory. If 2 people run the same program at the same time, total memory consumption is generally not doubled because the code page and shared library are single.

The free and PS commands are useful for estimating memory requirements.

The first step of the estimate plus some security amount. Because estimating the size of a program is probably wrong because you may have forgotten some of the programs you want to run and make sure you have some extra space. There should be a few MB. (Allocating too much space is better than allocating too little, but not excessive, because not using the swap space is wasteful; see article: About increasing the swap space.) ) Also,since It is nicer to deal with even numbers, you can round the ' value up to ' next full megabyte.

Based on the above calculations, you know how much memory you need in total. Minus your actual physical memory is the swap space. (in some versions of UNIX, you also need to allocate the image space of the physical memory, so the space you need to compute in the second cloth cannot be reduced)

If you calculate more space than your actual physical memory (more than several times above), you may need more physical memory, otherwise the system will be too low.

Even if the calculations show you don't have to swap space, it's best to have at least a few. Linux has some aggressive use of swap space, thus maintaining a certain amount of free physical memory. Even if the memory is not what the program needs, Linux will also swap out some unused memory pages, so that when necessary to avoid the exchange of waiting-that is, the swap can be in the hard disk idle time to complete early.

The swap space can be divided into several hard drives, which can sometimes improve performance, depending on the relative speed and access mode of these disks. You can try several programs, but it is difficult to know how to try it correctly. Do not believe that some scheme is better than others, because it is not always right.
Cache

Reading from disk is slow compared to accessing (real) memory and it is common to read the same parts of a hard disk multiple times over a relatively short period of time. For example, you might read an email first, then read it back to the editor, and then copy it to a folder and read it in a mail program. Or, consider how frequently the command LS may be used by many users on the system. Read the information only once from the disk and keep it on the hard drive, knowing no longer needed, except for the first reading, the others will be quicker. This is called disk cache diskette buffering, the memory used for this purpose is called buffer cache.

Unfortunately, because memory is a limited and scarce resource, the buffer cache is not usually large enough to fit all the data that can be used by everyone. When the cache is full, the maximum amount of unused data is discarded and the memory is released to the latest data.

Disk buffering is also used for write operations. The data to be written is often immediately read (for example, a source code file is saved to a file and then read by the compiler), so the data to be written is a good idea to buffer. In addition, the write program executes faster when the data is placed only as cache and not immediately written to disk. The write operation can then be done in the background without lowering the speed of the other programs.

Many operating systems have a buffer caches (even if the name is different), but not all based on the above principle. Some are through write Write-through: Data immediately write to disk (of course also write to cache) not immediately write cache called back to write Write-back. Write-back is more effective than penetrating, but it is also more error-prone: If the system crashes, or the power suddenly drops, or the floppy disk is taken out before the cache is written, the changed data in the cache will be lost. This may mean that the file system is isn't in the full working order, possibly because the write data contains significant changes to the system's record information.

Therefore, do not go through the normal shutdown process directly off the power (see Chapter 6), or remove the floppy disk (if it is mount), or what program is still using a floppy disk, or floppy light is still flashing. unmount The sync command flushes the buffer, which forces all of the unread data to be written back to disk, which you can use if you want to ensure that all data is written back safely. In a traditional UNIX system, an update program runs in the background, and it runs sync every 30 seconds, so there's usually no need to use sync. Linux has an additional bdflush that overcomes the problem that sync sometimes causes the system to suddenly stay stuck because the disk I/O load is too heavy (because of frequent operations).

Linux, Bdflush is started by update. There is no need to consider it, but if Bdflush occasionally dies for whatever reason, the core gives a warning, and it should be started manually (/sbin/update).

Cache does not really buffer files, but is block, is the disk I/O the smallest unit (under Linux, is generally 1kB). In this way, all directories, super blocks, other file system record data, and no file system disks can be buffered.

The effect of the cache depends on its size. Too small a cache is almost useless; it can only cache very little data and may be purged before being reused. Size depends on how much data is read and written, and how frequently the same data is accessed. The only way is to experiment.

If the cache is a fixed size, it should not be too large, otherwise the swap (and slow) will be used because the free memory space is too small. For the most efficient use of real memory, Linux automatically uses all the free memory as buffer cache, when the program needs more memory, automatically reduce cache.

Linux, for cache use does not need to do any work, it is completely automatic. In addition to normal shutdown system and remove floppy disk, do not care about cache.

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.