Solution to excessive memory usage on linux servers

Source: Internet
Author: User
Tags pkill

Today, a friend asked me why the linux server's memory usage is too high. Is there a way to solve this problem? Next I will introduce to you how to solve this problem, I hope this method will help you.

Today, the company has checked the status of all servers and found that memory usage of one server is much larger than normal, such

Qq immediately asked if the program development had changed something in the last two days. When I learned that nothing had been changed, I immediately restarted php-fpm and nginx, after the restart, check that the memory has been released for 1 GB, but this is not enough, because this server is 8 GB of memory and usually uses a maximum of 4 GB memory, I want to use more than 7 GB of server memory today, but it is obviously abnormal. Continue to check.


During the check, I used the top command to check which process occupies the largest memory. I did not find anything in the last minute. Then I used the command to check the top 10 processes with the largest memory usage:

The Code is as follows: Copy code

Ps-aux | sort-k4nr | head-n 10

It only shows that nginx and php-fpm occupy the largest memory, but I restarted these two processes. It seems that they have nothing to do with them. I began to wonder if the server was hacked, check the ps aux process immediately. This check shows the problem. There are many scripts generated in the process. Run the following command to kill them immediately:

The Code is as follows: Copy code
Kill-9 $ (ps aux | grep process name | grep-v grep | awk '{print $2 }')

Now we can see the memory usage, and the memory is still working properly. Now, let's take a few minutes and exit the server. Check the script again.

Ps: the things to be emphasized here should not be simply thought of as what the problem is based on your own intuition. It is best to investigate and execute it again, for example, I thought it was a problem with nginx and php-fpm based on my intuition. Fortunately, I had to say hello to the boss and restart it. Otherwise, the consequences would be very serious.

Run the following command to check the process with the largest memory usage:

The Code is as follows: Copy code
Ps aux | grep-v "USER" | sort-n-r-k 4 | awk 'nr = 1 {print $0 }'

I am using a php script. Here we wrote a linux script for restarting php when the linux load is too high.

You can restart php based on your server load. I only set the server load to 1 and restart php.


System: centos 5.9 64-bit
Script content:
Vi check-php.sh

The Code is as follows: Copy code

#! /Bin/sh
TOP_SYS_LOAD_NUM = 1
SYS_LOAD_NUM = 'uptime | awk' {print $ (NF-2)} '| sed's /,//''

Echo $ (date + "% y-% m-% d") 'uptime'
If ['echo "$ TOP_SYS_LOAD_NUM <$ SYS_LOAD_NUM" | bc '-eq 1]
Then
Echo "#0 #" $ (date + "% y-% m-% d % H: % M: % S ") "pkill php-fpm" 'ps-ef | grep php-fpm | wc-l'
/Etc/init. d/php-fpm stop
Sleep 8
Pkill php-fpm
Sleep 8
For I in 1 2 3
Do
If ['pgrep php-fpm | wc-l'-le 0]
Then
/Etc/init. d/php-fpm start
Sleep 30
Echo "#1 #" $ (date + "% y-% m-% d % H: % M: % S ") "start php-fpm" 'ps-ef | grep php-fpm | wc-l'
Fi
Done
Else
If ['pgrep php-fpm | wc-l'-le 0]
Then
/Etc/init. d/php-fpm start
Sleep 30
Echo "#2 #" $ (date + "% y-% m-% d % H: % M: % S ") "start php-fpm" 'ps-ef | grep php-fpm | wc-l'
Fi
Fi

Scheduled execution

The Code is as follows: Copy code
*/3 */root/shell/check-php.sh>/var/log/check-php.log

If you run the script:

The Code is as follows: Copy code
: Line 6: bc: command not found
: Line 6: [:-eq: unary operator expected

Indicates that no bc command is available. centos can be installed using yum-y install bc.

Related Article

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.