CentOS7 How do I set up a swap file?

Source: Internet
Author: User
Tags deprecated vps root directory server memory

CentOS7 How do I set up a swap file?

Source of the problem

Want to buy a VPS on a dot blog service, a preference for dynamic blogs and the like, the selection of Nodejs, the scope of the reduction, a first fancy n-blog. You need to install bower, and if you have been prompted about the NPM version issue at installation time:

?

1 2 3 4 5 6 7 8 9 A NPM WARN deprecated This version of NPM lacks support for important features, NPM W ARN deprecated such as scoped packages, offered by the primary NPM npm WARN deprecated. Consider upgrading to on least npm@2, if not the NPM WARN deprecated, latest stable version. To upgrade to npm@2, run:npm WARN deprecated npm WARN deprecated npm-g Install npm@latest-2 npm WARN deprecated NPM WARN Deprecated to upgrade to the latest stable version, RUN:NPM WARN deprecated npm WARN deprecated npm-g Install npm@lates T npm WARN deprecated NPM WARN deprecated (depending on how node.js is installed on your system, your NPM WARN deprecated may need to prefix the preceding commands with ' sudo ', or if NPM WARN deprecated on Windows, run them from a Administrato R prompt.) NPM WARN deprecated npm WARN deprecated If You ' re running the VersioN of NPM bundled with NPM WARN deprecated node.js 0.10 LTS, being aware that next version of 0.10 LTS NPM WARN 'll is bundled with a version of Npm@2, which has some small npm WARN deprecated backwards-incompatible changes to ' NPM run-script ' and npm WARN deprecated semver behavior.

The hint is actually very clear, is that the NPM version is too low, some features do not support. Reference hints, performing

?

1 Npm-g Install Npm@latest

Also first appeared a screen prompts, after all, npm in this moment has not upgraded, after a few 10 seconds, there are the following

?

1 2 3 4 ...... The above omitted npm WARN deprecated backwards-incompatible changes made to ' NPM run-script ' and npm WARN deprecated. Killed

If you are not careful, you will find that the inexplicable end, and NPM has not been updated. Looking closely, you will find a killed prompt in the last line. A search on the Internet, you can find that this is because the memory is not enough, the server memory is not enough, so the program is directly over. In fact, if the VPS memory is small, not only in the installation of Nodejs library when there will be problems, the direct use of Yum installation package, will also prompt cannot allocate memory error.

Solution

Upgrade VPS Configuration

The easiest way of course is to upgrade the VPS configuration, this go to the Web page click on the button can be, as long as the money on the line. For individual players, but with a VPS on a personal site, toss some gadgets, not much flow, often do not need too good configuration. As a result, the following method is used to increase the exchange of documents.

SWAP FILE

We usually listen to more may be the swap partition, is directly on the hard disk separately divided into a zone to use as a swap space. If it is not convenient to create a new partition after the system has been installed, you can use a file instead. Swap space is the essence of the hard disk, in the system as a large cache of memory, operating system memory is not enough time, will replace some of the less common pages into the swap space inside. Therefore, the swap space will improve the performance of some systems. But the larger the swap space is, the better, because if the application is just using the data in the swap space, there will be a larger delay, and a large swap partition can cause a lot of high application latency. Therefore, if the program has large memory requirements, the best way is to increase physical memory. To buy memory is the king, swap space is only a slightly better way to improve performance.

CentOS7 Settings swap file

Check system conditions

First, check whether the system has set up swap space, in principle, swap space as far as possible to set only one. In addition, you need to check that the system has enough remaining hard disk space.

?

1 Swapon-s

You can view the swap space of the system through the Swapon command, or you can view it by using the free command.

?

1 Free-h

Check the remaining hard disk space of the system through the DF command.

?

1 Df-h

Create a swap file

The fallocate command to create a new Exchange file, because my system memory is 512M, the swap partition is generally set to memory 2~4 times, so I set to 1G, directly mounted to the root directory.

?

1 sudo fallocate-l 1g/swapfile

Can be viewed through the LS command

?

1 Ls-lh/swapfile

The results are as follows

?

1 -rw-r--r--1 root root 1.0G Oct 11:00/swapfile

Enable swap files

First, modify the/swapfile permissions, allowing only root users to access them.

?

1 sudo chmod 600/swapfile

The results of LS are as follows

?

1 -RW-------1 root 1.0G Oct 11:00/swapfile

Next, notify the system to create swap space

?

1 sudo mkswap/swapfile

Finally, enable swap space

?

1 sudo swapon/swapfile

You can view the results by swapon-s or df-h commands, and the swap space should be displayed.

Persistent configuration

After the reboot, the system will no longer set the/swapfile to swap space. You can add the following in/etc/fstab

?

1 /swapfile Swap SW 0 0

In this way, the system automatically maps the/swapfile to swap space after the reboot.

At last

It can be imagined that if the actual memory requirements far exceed the physical memory size, there will be frequent memory and swap space data exchange, the damage to the hard disk is relatively large. Also, the application experience will be poor.

Therefore, the swap space is only a temporary solution, to buy the memory bar is the kingly way.

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.