View CPU and memory usage in Linux

Source: Internet
Author: User

During system maintenance, you may need to check the CPU usage at any time and analyze the system status based on the relevant information. In centos, you can use the top command to view CPU usage. After the TOP Command is run, the CPU usage status is displayed in full screen mode and in dialog mode. The top-based command can be used to control the display mode. The command to exit top is Q (Press Q once in top running ).

Operation instance:

Enter "TOP" in the command line"

You can start top

The top full-screen dialog mode can be divided into three parts: system information, command input, and process list.

Part 1-System Information bar at the top:

The first line (top ):

"00:11:04" indicates the current system time;

Is the current operating time after the system is started;

"2 users" is the user currently logged on to the system, more specifically, the number of terminals that log on to a user-connections to multiple terminals of the system from the same user at the same time are considered as connections from multiple users to the system, the number of users here will also be the number of terminals;

"Load average" is the average value of the current system load. The following three values are the average number of processes 1 minute ago, 5 minutes ago, and 15 minutes ago, respectively, generally, when this value exceeds the number of CPUs, the CPU will be overloaded with the processes contained in the current system;

Line 2 (tasks ):

"59 total" indicates the total number of processes in the current system;

"1 running" indicates the number of processes currently running;

"58 sleeping" indicates the number of processes in the waiting status;

"0 stoped" indicates the number of stopped system processes;

"0 zombie" indicates the number of recovered processes;

Row 3 (CPUs ):

Indicates the current CPU usage;

Row 4 (MEM ):

Total memory, current usage, idle memory, and buffer memory respectively;

The fifth line (SWAP ):

Indicates that the category is the same as the fourth row (MEM), but this shows the usage of swap partition (SWAp. In general, swap partitions (SWAP) are frequently used and are considered to be caused by insufficient physical memory.

The second part -- Internal Command Prompt bar in the middle part:

You can use top's Internal commands to control the display mode of processes. The internal commands are as follows:

S

-Change the screen update frequency

L-Disable or enable the first line of top information in the first part.

T-Disable or enable the expression of the first part of the second line of tasks and the third line of CPUs Information

M-Disable or enable the expression of the first part of the fourth line of MEM and the fifth line of swap information

N-process list in the order of PID size (Part 3 is described later)

P-sort the process list in the order of CPU usage (Part 3 is described later)

M-sort the process list in the order of memory usage (Part 3 is described later)

H-Show Help

N-set the number of processes displayed in the Process List

Q-exit top

S-

Change screen update cycle

Part 3-process list at the bottom:

The process list differentiated by PID is regularly updated based on the set screen update time. You can use the top internal command to control the display mode.

To view the memory in Linux, we generally use the free command:
[Root @ scs-2 TMP] # Free
Total used free shared buffers cached
Mem: 3266180 3250004 16176 0 110652 2668236
-/+ Buffers/cache: 471116 2795064
Swap: 2048276 80160 1968116

The following is an explanation of these values:
Total: total physical memory size.
Used: used.
Free: available.
Shared: The total memory shared by multiple processes.
Buffers/cached: disk cache size.
Row 3 (-/+ buffers/cached ):
Used: used.
Free: available.
The fourth line won't be explained much.
Difference: used/free of the second row (MEM) differs from used/free of the third row (-/+ buffers/cache. The difference between the two is that from the perspective of usage, the first line is from the OS perspective, because for OS, buffers/cached is used, so its available memory is 16176kb, the memory used is kb, including the + buffers + cached used by the kernel (OS) + application (x, Oracle, etc.
The third row refersProgramFrom the perspective, for applications, buffers/cached is equivalent to available, because buffer/cached is used to improve the file read performance. When the application needs to use the memory, buffer/cached will be recycled soon.
From the application perspective, the available memory = system free memory + buffers + cached.
For example:
2795064 = 16176 + 110652 + 2668236

Next, we will explain when the memory will be exchanged and by which side. When the available memory is less than the rated value, a meeting will be held for exchange.
Rating:
CAT/proc/meminfo

[Root @ scs-2 TMP] # Cat/proc/meminfo
Memtotal: 3266180 KB
Memfree: 17456 KB
Buffers': 111328 KB
Cached: 2664024 KB
Swapcached: 0 KB
Active: 467236 KB
Inactive: 2644928 KB
Hightotal: 0 KB
Highfree: 0 KB
Lowtotal: 3266180 KB
Lowfree: 17456 KB
Swaptotal: 2048276 KB
Swapfree: 1968116 KB
Dirty: 8 KB
Writeback: 0 KB
Mapped: 345360 KB
Slab: 112344 KB
Committed_as: 535292 KB
Pagetables: 2340 KB
Vmalloctotal: 536870911 KB
Vmallocused: 272696 KB
Vmallocchunk: 536598175 KB
Hugepages_total: 0
Hugepages_free: 0
Hugepagesize: 2048 KB

View results with free-M:
[root @ scs-2 TMP] # Free-M
total used free shared buffers cached
mem: 3189 3173 16 0 107 2605
-/+ buffers/cache: 460 2729
swap: 2000 78 1921

View the size of the/proc/kcore file (memory image ):
[Root @ scs-2 TMP] # ll-H/proc/kcore
-R -------- 1 Root 4.1g Jun 12 12:04/proc/kcore

Note:

Memory usage Measurement

Measure how much memory a process occupies. Linux provides us with a very convenient method. The/proc Directory provides us with all the information, in fact, the top tool also obtains the corresponding information here.

/Proc/meminfo memory usage information of the machine

/Proc/Pid/maps PID is the process number and displays the virtual address occupied by the current process.

Memory occupied by/proc/Pid/statm Process

[Root @ localhost ~] # Cat/proc/self/statm

654 57 44 0 0 334 0

Output description

CPU and CPU 0... The meaning of each parameter in each row (in the first behavior example) is:

Parameter description/proc // status

Size (pages) task virtual address space size vmsize/4

The size of the physical memory that the resident (pages) application is using vmrss/4

Shared (pages) shared pages 0

Size of executable virtual memory owned by the TRS (pages) program vmexe/4

The size of the library that the LRS (pages) is mapped to the virtual memory space of the task. vmlib/4

Drs (pages) Program Data Segment and user-state stack size (vmdata + vmstk) 4

DT (pages) 04

View available machine memory

/Proc/28248/> free

Total used free shared buffers cached

Mem: 1023788 926400 97388 0 134668 503688

-/+ Buffers/cache: 288044 735744

Swap: 1959920 89608 1870312

When we run the free command to view the idle memory of the machine, we will find that the free value is very small. This is mainly because there is such an idea in Linux, the memory does not need to be white, so it tries its best to cache and buffer some data for the next use. But in fact, these memories can also be used immediately.

So free memory = free + buffers + cached = total-used

The top command is a common performance analysis tool in Linux. It can display the resource usage of various processes in the system in real time, similar to the Windows Task Manager. The following describes how to use it.

Top-02:53:32 up 16 days, 17 users, load average: 0.24, 0.21, 0.24
Tasks: 481 Total, 3 running, 474 sleeping, 0 stopped, 4 zombie
CPU (s): 10.3% us, 1.8% Sy, 0.0% Ni, 86.6% ID, 0.5% wa, 0.2% hi, 0.6% Si, 0.0% St
Mem: 4042764 k total, 4001096 K used, 41668 K free, 383536 K Buffers
Swap: 2104472 k total, 7900 K used, 2096572 K free, 1557040 K cached

PID user PR Ni virt res shr s % CPU % mem time + command
32497 Jacky 20 0 669 M 222 m 31 M r 10 5.6 29:27. 62 Firefox
4788 yiuwing 20 0 257 M 18 m 13 m s 5 0.5. 44 konsole
5657 liuxiaof 20 0 585 m 159 M 30 m s 4 4.0. 06 Firefox
4455 xiefc 20 0 542 M 124 m 30 m r 4 3.1. 03 Firefox
6188 liuxiaof 20 0 191 M 17 m 13 m s 4 0.5. 16 konsole

The first five lines in the statistical information area are the overall statistical information of the system. The first line is the task queue information, which is the same as the execution result of the uptime command. The content is as follows:

01:06:48 current time
Up system running time, format: minute
1 user current number of logged-on users
Load average: 0.06, 0.60, 0.48 system load, that is, the average length of the task queue.
The three values are the average values from 1 minute, 5 minutes, and 15 minutes ago to the present.

Second and Third, information about the process and CPU. When multiple CPUs exist, the content may exceed two rows. The content is as follows:

Tasks: 29 total process count
1. Number of Running Processes of Running
28 sleeping sleep Processes
0 stopped process count
0 zombie botnets
CPU (s): 0.3% us CPU usage
1.0% Sy CPU usage in kernel space
0.0% percentage of CPU used by processes that have changed their priorities in Ni user process space
98.7% ID idle CPU percentage
0.0% wa CPU time percentage waiting for Input and Output
0.0% hi
0.0% Si

Memory information of the last two behaviors. The content is as follows:

Mem: 191272 k total physical memory
Total physical memory used by 173656 K used
17616 K free Free Memory Total
Memory usage of 22052 K buffers as kernel Cache
Swap: 192772 k total number of swap Areas
Total number of swap areas used by 0 K used
192772 K free swap zone total
The total number of swap areas of the 123988 K cached buffer.
The content in the memory is swapped out to the swap zone and then into the memory, but the used swap zone has not been overwritten,
This value indicates the size of the SWAp zone where the content already exists in the memory.
When the corresponding memory is swapped out again, you do not have to write data to the swap zone.

Detailed information about each process is displayed at the bottom of the process information area. First, let's take a look at the meaning of each column.

Serial Number Column name meaning
A pid process ID
B ppid parent process ID
C ruser real User Name
D uid: User ID of the process owner
E User: User Name of the process owner
Group name of the owner of the F group process
The terminal name of the G tty startup process. Processes not started from the terminal are displayed?
H PR priority
I ni nice value. A negative value indicates a high priority, and a positive value indicates a low priority.
The final CPU used by j p is only meaningful in multiple CPU Environments
K % percentage of CPU time used since the last CPU update to the present
L total CPU time used by the time process, in seconds
M Time + the total CPU time used by the process, in the unit of 1/100 seconds
N % mem percentage of physical memory used by the Process
O total virtual memory used by the virt process, in KB. Virt = swap + Res
The swap size in the virtual memory used by the P SWAp process, in KB.
The size of the physical memory used by the Q res process, Not swapped out, in KB. Res = code + Data
R code executable Code Physical memory occupied, in KB
Physical memory occupied by other parts (Data Segment + stack) except the executable code of S data, in KB
T SHR shared memory size, in KB
U nflt page error count
The number of modified pages that the last write to the present by V.
W s Process status.
D = uninterrupted sleep
R = run
S = sleep
T = tracking/stopping
Z = zombie Process
X command name/command line
Y wchan: if the process is sleep, the system function name in sleep is displayed.
Z flags task flag, refer to sched. h

By default, only important PID, user, PR, Ni, virt, res, SHR, S, % CPU, % MEM, time +, and command columns are displayed. You can use the shortcut keys below to change the display content.
You can use the f key to change the display content. Press F to display the column list. Press A-Z to display or hide the corresponding column, and press enter to confirm.
Press the O key to change the Column Display sequence. A lower-case a-Z can move the corresponding column to the right, while an upper-case A-Z can move the corresponding column to the left. Press enter.
Press the f or O key in upper case, and then press a-Z to sort the process according to the corresponding column. The uppercase r key can reverse the current sorting.

====================================

Some interactive commands can be used to complete other parameter functions during the use of the top command. These commands are started by using the shortcut key.
<Space>: refresh immediately.
P: sort by CPU usage.
T: sort by time and accumulated time.
Q: exit the TOP command.
M: Switch to Display memory information.
T: process and CPU status information is displayed during switchover.
C: Switch the display command name and the complete command line.
M: sort by memory size.
W: Write the current settings ~ /. Toprc file. This is a recommended method for writing top configuration files.

As you can see, the top command is a powerful tool for monitoring the system, especially for system administrators. However, it consumes a lot of system resources.

Application Instance
The top command can be used to monitor the process of a specified user. By default, the process of all users is monitored. If you want to view the situation of a specified user, press the "U" key in the terminal and enter the user name, the system will switch to the process running interface of the specified user.
A. Role
The free command is used to display memory usage. The permission is applied to all users.
B. Format
Free [-B-K-M] [-O] [-s delay] [-T] [-v]
C. Main Parameters
-B-K-M: memory usage is displayed in bytes (kb, MB.
-S delay: the number of seconds to display the memory usage.
-T: displays the total memory column.
-O: the buffer adjustment column is not displayed.
D. Application Instance
The free command is the main command used to view memory usage. Compared with the top command, it is easy to use and only occupies a small amount of system resources. The-S parameter allows you to use the free command to continuously monitor the number of inactive instances. This allows you to use it as a convenient real-time monitor.
# Free-B-S5
With this command, the slave node continuously reports memory usage (in bytes) and updates every 5 seconds.

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.