通过ssh远程查看服务器的内存使用情况

查看服务器的内存

Posted by VK on September 20, 2019

[转载]COMMANDS TO CHECK MEMORY USAGE ON LINUX VIA SSH

MEMORY USAGE

On linux, there are commands for almost everything, because the gui might not be always available. When working on servers only shell access is available and everything has to be done from these commands. So today we shall be checking the commands that can be used to check memory usage on a linux system. Memory include RAM and swap.

It is often important to check memory usage and memory used per process on servers so that resources do not fall short and users are able to access the server. For example a website. If you are running a webserver, then the server must have enough memory to serve the visitors to the site. If not, the site would become very slow or even go down when there is a traffic spike, simply because memory would fall short. Its just like what happens on your desktop PC.

1. ‘FREE’ COMMAND

The free command is the most simple and easy to use command to check memory usage on linux. Here is a quick example:

Check RAM Usage

Check RAM Usage

The m (-m) option displays all data in MBs. The total os 7976 MB is the total amount of RAM installed on the system, that is 8GB. The used column shows the amount of RAM that has been used by linux, in this case around 6.4 GB. The output is pretty self explanatory. The catch over here is the cached and buffers column. The second line tells that 4.6 GB is free. This is the free memory in first line added with the buffers and cached amount of memory.

Linux has the habit of caching lots of things for faster performance, so that memory can be freed and used if needed. The last line is the swap memory, which in this case is lying entirely free.

2. /PROC/MEMINFO

Please read more here: http://www.redhat.com/advice/tips/meminfo.html

3. VMSTAT

The vmstat command with the s option, lays out the memory usage statistics much like the proc command. Here is an example:

$ vmstat -s
      8167848 K total memory
      7449376 K used memory
      3423872 K active memory
      3140312 K inactive memory
       718472 K free memory
      1154464 K buffer memory
      2422876 K swap cache
      1998844 K total swap
            0 K used swap
      1998844 K free swap
       392650 non-nice user cpu ticks
         8073 nice user cpu ticks
        83959 system cpu ticks
     10448341 idle cpu ticks
        91904 IO-wait cpu ticks
            0 IRQ cpu ticks
         2189 softirq cpu ticks
            0 stolen cpu ticks
      2042603 pages paged in
      2614057 pages paged out
            0 pages swapped in
            0 pages swapped out
     42301605 interrupts
     94581566 CPU context switches
   1382755972 boot time
         8567 forks
$

The top few lines indicate total memory, free memory etc and so on.

4. ‘TOP’ COMMAND

The top command is generally used to check memory and cpu usage per process. However it also reports total memory usage and can be used to monitor the total RAM usage. The header on output has the required information. Here is a sample output:

Top Command

Top Command

Check the KiB Mem and KiB Swap lines on the header. They indicate total, used and free amounts of the memory. The buffer and cache information is present here too, like the free command.

5. HTOP

Similar to the top command, the htop command also shows memory usage along with various other details.

Htop Memory Ram Usage

Htop Memory Ram Usage

The header on top shows cpu usage along with RAM and swap usage with the corresponding figures.

RAM INFORMATION

To find out hardware information about the installed RAM, use the demidecode command. It reports lots of information about the installed RAM memory.

RAM Information

RAM Information

Provided information includes the size (2048MB), type (DDR2) , speed(667 Mhz) etc.

SUMMARY

All the above mentioned commands work from the terminal and do not have a gui. When working on a desktop with a gui, it is much easier to use a GUI tool with graphical output. The most common tools are gnome-system-monitor on gnome and ksysguard on KDE. Both provide resource usage information about cpu, ram, swap and network bandwidth in a graphical and easy to understand visual output.