当前位置: 技术问答>linux和unix
linux为什么非要用swap分区,而不能象windows那样用交换文件
来源: 互联网 发布时间:2015-05-27
本文导语: 我知道系统很大的情况下,硬盘交换空间是必须的,但是浪费一个分区,总感觉不爽,为什么linux不用交换文件呢,有没有方法改造呢? | 不用改造,linux支持用文件做swap,当然用文件的效率...
我知道系统很大的情况下,硬盘交换空间是必须的,但是浪费一个分区,总感觉不爽,为什么linux不用交换文件呢,有没有方法改造呢?
|
不用改造,linux支持用文件做swap,当然用文件的效率比分区低。
下面是RH手册里的:
To add a swap file:
1. Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.
2. At a shell prompt as root, type the following command with count being equal to the desired block size:
dd if=/dev/zero of=/swapfile bs=1024 count=65536
3. Setup the swap file with the command:
mkswap /swapfile
4. To enable the swap file immediately but not automatically at boot time:
swapon /swapfile
5. To enable it at boot time, edit /etc/fstab to include:
/swapfile swap swap defaults 0 0
The next time the system boots, it will enable the new swap file.
6. After adding the new swap file and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.
下面是RH手册里的:
To add a swap file:
1. Determine the size of the new swap file and multiple by 1024 to determine the block size. For example, the block size of a 64 MB swap file is 65536.
2. At a shell prompt as root, type the following command with count being equal to the desired block size:
dd if=/dev/zero of=/swapfile bs=1024 count=65536
3. Setup the swap file with the command:
mkswap /swapfile
4. To enable the swap file immediately but not automatically at boot time:
swapon /swapfile
5. To enable it at boot time, edit /etc/fstab to include:
/swapfile swap swap defaults 0 0
The next time the system boots, it will enable the new swap file.
6. After adding the new swap file and enabling it, make sure it is enabled by viewing the output of the command cat /proc/swaps or free.
|
这是为什么linxu用swap而不是交换文件的原因(摘自IBM《Linux 用户技术 FAQ》)
Like Windows, Linux uses a certain amount of space for holding programs temporarily, when
there is not enough available RAM (random access memory) to hold all the programs that are
running concurrently. Generally, the least recently used program (or part of a program) is
copied from memory to a file on your hard drive until it is needed again, at which time the
current least recently used program is swapped out in its place and the first program is loaded
back into memory. (This is an over-simplified explanation; there is much more to it, but this will
do for this question.) This file is called a swap file in Windows or OS/2 and “swap space” in
Linux, but in either case it is a form of data file that is read from and written to off and on as
long as your system is running.
Windows puts the swap file (a hidden system file with different names for different versions of
Windows) in the bootable data partition by default. OS/2 does the same, but by changing the
CONFIG.SYS file a user can put the swap file in any directory in any partition on any drive
they like. Linux, by default, requires a special swap partition in which to store the swap file.
(Actually, Linux does allow swap files to be put in data partitions, with caveats—see below for
more on this.)
下面是改造方法:(摘自IBM《Linux 用户技术 FAQ》)
If for some reason you can’t, or do not wish to, create Linux swap partitions—or if you have no
more available disk space to use for a new swap partition—it is possible to create swap files
within partitions that currently hold programs and/or data files. However, your first choice
should be for one or more Linux swap partitions.
__________________________________________________________________________________
Important Note: There are two disadvantages to locating swap files outside of dedicated swap partitions: 1) The performance of swap files that are located in data partitions is slower than that of swap files in swap partitions (due to file system overhead and noncontiguous data blocks), and 2) It is also possible that if the swap file is ever damaged it could corrupt the filesystem for that partition, resulting in lost data. For these reasons it is strongly recommended that you use swap partitions whenever possible, leaving swap files in data partitions as a last resort. Swap files might be appropriate if you temporarily need some extra swap space.
________________________________________________________________________________
You can create as many as eight swap files, if necessary. Linux will apportion the swapped
data between these files as appropriate.
To create a swap file in a data partition, use a command similar to the following:
dd if=/dev/zero of= bs= count=
For example:
dd if=/dev/zero of=/Swapdir/Swapfile bs=1024 count=65536
(Note: If /Swapdir does not yet exist, it must be created before running the dd command. For
example: mkdir /Swapdir or mkdir /home/Swapdir. In order to create the directory, you
must be running as the root operator.)
The dd command (sometimes called “data duplicator,” although the original meaning of the dd
acronym is lost in the mists of time) is used to create the swap file. The if= parameter
identifies the input file source for creating the swap file (device “zero”), of= is where you
specify the path to the output (swap) file you want to create, bs= is the block size to use and
count= is where you define how large to make the swap file, in increments of the block size.
In the preceding example, we are creating a 64MB swap file (65,536 * 1024 [1K] equals
65,536K, or 64MB), called Swapfile (use whatever name you prefer), in a directory called
Swapdir.
Next, to initialize (or make ready for use) the swap file, use the following command:
Preparing Today for Linux Tomorrow
10
mkswap /Swapdir/Swapfile 65536
(Use the same path as in the dd command and the file size you specified in the count=
parameter. If you get a “No such file or directory” error, verify that you typed the upper/lower
case correctly and did not omit the leading /.)
After initializing the file, “synchronize” it to ensure that it is properly written to disk:
sync
And, finally, to tell Linux to start using this new swap file:
swapon /Swapdir/Swapfile
This will activate the swap file temporarily (until the next time you log out or reboot). To make
it permanently active, use an appropriate tool (such as kfstab [see the earlier question, How
can I speed up performance by using multiple swap partitions?, for details], which knows the
exact layout of the file that fstab requires) to open the file /etc/fstab.
When you open fstab, you might see something like (without the headings):
/dev/hda1 / ext2 defaults 0 1
/dev/hda2 /usr ext2 defaults 0 1
/dev/hda3 none swap sw 0 0
(There may be other disk partitions listed if you have other swap and/or data partitions.) To
activate your new swap file, add a line similar to the following immediately after the last
partition listing. Then save and close the /etc/fstab file.
/Swapdir/Swapfile none swap sw 0 0
Should you later decide that you no longer need the swap file, it is easy enough to remove.
First, use the swapoff command to disable the use of the file:
swapoff /Swapdir/Swapfile
Then, simply delete the file:
rm /Swapdir/Swapfile
Like Windows, Linux uses a certain amount of space for holding programs temporarily, when
there is not enough available RAM (random access memory) to hold all the programs that are
running concurrently. Generally, the least recently used program (or part of a program) is
copied from memory to a file on your hard drive until it is needed again, at which time the
current least recently used program is swapped out in its place and the first program is loaded
back into memory. (This is an over-simplified explanation; there is much more to it, but this will
do for this question.) This file is called a swap file in Windows or OS/2 and “swap space” in
Linux, but in either case it is a form of data file that is read from and written to off and on as
long as your system is running.
Windows puts the swap file (a hidden system file with different names for different versions of
Windows) in the bootable data partition by default. OS/2 does the same, but by changing the
CONFIG.SYS file a user can put the swap file in any directory in any partition on any drive
they like. Linux, by default, requires a special swap partition in which to store the swap file.
(Actually, Linux does allow swap files to be put in data partitions, with caveats—see below for
下面是改造方法:(摘自IBM《Linux 用户技术 FAQ》)
If for some reason you can’t, or do not wish to, create Linux swap partitions—or if you have no
more available disk space to use for a new swap partition—it is possible to create swap files
within partitions that currently hold programs and/or data files. However, your first choice
should be for one or more Linux swap partitions.
__________________________________________________________________________________
Important Note: There are two disadvantages to locating swap files outside of dedicated swap partitions: 1) The performance of swap files that are located in data partitions is slower than that of swap files in swap partitions (due to file system overhead and noncontiguous data blocks), and 2) It is also possible that if the swap file is ever damaged it could corrupt the filesystem for that partition, resulting in lost data. For these reasons it is strongly recommended that you use swap partitions whenever possible, leaving swap files in data partitions as a last resort. Swap files might be appropriate if you temporarily need some extra swap space.
________________________________________________________________________________
You can create as many as eight swap files, if necessary. Linux will apportion the swapped
data between these files as appropriate.
To create a swap file in a data partition, use a command similar to the following:
dd if=/dev/zero of= bs= count=
For example:
dd if=/dev/zero of=/Swapdir/Swapfile bs=1024 count=65536
(Note: If /Swapdir does not yet exist, it must be created before running the dd command. For
example: mkdir /Swapdir or mkdir /home/Swapdir. In order to create the directory, you
must be running as the root operator.)
The dd command (sometimes called “data duplicator,” although the original meaning of the dd
acronym is lost in the mists of time) is used to create the swap file. The if= parameter
identifies the input file source for creating the swap file (device “zero”), of= is where you
specify the path to the output (swap) file you want to create, bs= is the block size to use and
count= is where you define how large to make the swap file, in increments of the block size.
In the preceding example, we are creating a 64MB swap file (65,536 * 1024 [1K] equals
65,536K, or 64MB), called Swapfile (use whatever name you prefer), in a directory called
Swapdir.
Next, to initialize (or make ready for use) the swap file, use the following command:
Preparing Today for Linux Tomorrow
10
mkswap /Swapdir/Swapfile 65536
(Use the same path as in the dd command and the file size you specified in the count=
parameter. If you get a “No such file or directory” error, verify that you typed the upper/lower
case correctly and did not omit the leading /.)
After initializing the file, “synchronize” it to ensure that it is properly written to disk:
sync
And, finally, to tell Linux to start using this new swap file:
swapon /Swapdir/Swapfile
This will activate the swap file temporarily (until the next time you log out or reboot). To make
it permanently active, use an appropriate tool (such as kfstab [see the earlier question, How
can I speed up performance by using multiple swap partitions?, for details], which knows the
exact layout of the file that fstab requires) to open the file /etc/fstab.
When you open fstab, you might see something like (without the headings):
/dev/hda1 / ext2 defaults 0 1
/dev/hda2 /usr ext2 defaults 0 1
/dev/hda3 none swap sw 0 0
(There may be other disk partitions listed if you have other swap and/or data partitions.) To
activate your new swap file, add a line similar to the following immediately after the last
partition listing. Then save and close the /etc/fstab file.
/Swapdir/Swapfile none swap sw 0 0
Should you later decide that you no longer need the swap file, it is easy enough to remove.
First, use the swapoff command to disable the use of the file:
swapoff /Swapdir/Swapfile
Then, simply delete the file:
rm /Swapdir/Swapfile
|
gz
|
to fierygnu(va_list) :为什么说用文件的效率比分区低呢?
|
“but by changing the
CONFIG.SYS file a user can put the swap file in any directory in any partition on any drive
they like.”
在win的config.sys里怎么写了?
CONFIG.SYS file a user can put the swap file in any directory in any partition on any drive
they like.”
在win的config.sys里怎么写了?
|
用文件要通过文件系统。
|
文件系统的效率在于文件块的分布情况 而swap文件必然是凌乱不堪的 而且由于和别的文件放在一起也不能做有效的清理
|
可以把SWAP关掉,而且LINUX下的SWAP性能远远不如WINDOWS下。
|
这也和安全因素有关,交换分区比文件安全的多。