Linux view CPU and memory usage

Source: Internet
Author: User
Tags cpu usage

During system maintenance, it may be necessary to review the CPU usage at any time, and to analyze the system condition according to the corresponding information. In CentOS, you can view CPU usage by using the top command. After running the top command, the CPU usage status is displayed in full-screen mode, and will be in the dialog-using top-based commands, you can control how the display is displayed, and so on. The command to exit Top is Q (the Q key is struck once in the top run).

The top command is a common performance analysis tool under Linux that shows the resource usage of individual processes in the system in real time, similar to the Task Manager for Windows

You can view the contents of the%mem directly after you use the top command. You can choose to view by process or by user, if you want to see the process memory usage of the Oracle user, you can use the following command:
$ top-u Oracle

Content Explanation:

PID: ID of the process
USER: Process Owner
PR: The priority level of the process, the smaller the higher the priority is executed
Ninice: Value
VIRT: Virtual memory consumed by the process
RES: The physical memory occupied by the process
SHR: Shared memory used by the process
S: The state of the process. s for hibernation, R for running, Z for Zombie, n for the process priority value is negative
%CPU: Process Consuming CPU utilization
%MEM: The percentage of physical memory and total memory used by the process
Time+: The total CPU time that is consumed after the process is started, which is the cumulative value of the CPU usage time.
Command: Process start name

Operation Example:

Enter "Top" on the command line

You can start the top

Top Full Screen Dialog mode can be divided into 3 parts: System Information Bar, command input field, process list bar.

The first part-the top System Information Bar:

First line (top):

"00:11:04" is the current time of the system;

"3:35" is the operating time of the system after it has been started;

"2 Users" is the user who is currently logged on to the system, more specifically the number of terminals logged on to the user-the same user at the same time to the system of multiple terminal connections will be treated as multiple users connected to the system, here the number of users will also be displayed as the number of terminals;

"Load average" is the average of the current system load, the following three values are 1 minutes ago, 5 minutes ago, 15 minutes ago The average process, the general can be thought that this value exceeds the number of CPUs, the CPU will be more strenuous load the current system contains the process;

Second line (Tasks):

"Total" is the current number of system processes;

"1 Running" is the number of processes currently in operation;

"Sleeping" is the number of processes currently in the waiting state;

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

"0 Zombie" for the number of processes restored;

Third line (Cpus):

The current utilization rate of CPU is indicated respectively;

Line Four (MEM):

Indicates the total amount of memory, current usage, idle memory, and amount of RAM in buffer usage;

Line Five (Swap):

Represents the category with the fourth line (MEM), but here reflects the use of the Swap partition (swap). Typically, swap partitions (swap) are frequently used, and will be seen as a result of insufficient physical memory.

The second part--the inner command prompt bar of the middle part:

In the top run, you can control how processes are displayed through the internal commands of top. The internal command is the following table:

S

-Change the screen update frequency

L-Turn off or turn on the representation of the first line top information in the first section

T-turn off or turn on the second line of the first section Cpus information representation of the Tasks and the third row

M-Turn off or turn on the first part of the four-line Mem and fifth line Swap information representation

N-the list of processes in the order of the PID size (the third part is described later)

P-List of processes in the Order of CPU Utilization (third part)

M-Arrange the list of processes in the Order of Memory occupancy (Part III)

H-Show help

N-Set the number of processes displayed in the process list

Q-Exit Top

S-

Change the screen update cycle

Part Three-the bottom part of the Process list bar:

A list of processes that are separated by PID will be updated periodically based on the Set screen update time. The top internal command lets you control how it is displayed here

Pmap

Depending on the process, you can view the memory used by the process-related information (the process number can be viewed by PS) as follows:
$ pmap-d 5647

Ps

As shown in the following example:
$ ps-e-O ' pid,comm,args,pcpu,rsz,vsz,stime,user,uid ' where Rsz is the actual memory
$ ps-e-o ' pid,comm,args,pcpu,rsz,vsz,stime,user,uid ' |  grep Oracle | Sort-nrk

Where Rsz is the actual memory, the above example is implemented by memory sort, from large to small

View memory under Linux We generally use the free command:
[[email protected] tmp]# free
Total used free shared buffers Cached
mem:3266180 3250004 16176 0 110652 2668236
-/+ buffers/cache:471116 2795064
swap:2048276 80160 1968116

Here is an explanation of these values:
Total: The amount of physical memory.
Used: How large is used.
Free: How much is available.
Shared: The total amount of memory shared by multiple processes.
Buffers/cached: The size of the disk cache.
Third line (-/+ buffers/cached):
Used: How large is used.
Free: How much is available.
There is not much to explain in line four.
Difference: The used/free of the second line (MEM) differs from the third row (-/+ Buffers/cache) used/free. The difference between the two is that the first line is from the OS point of view, because for the os,buffers/cached are all belong to be used, so his available memory is 16176KB, the used memory is 3250004KB, which includes, the kernel (OS) uses + Application (X, ORACLE,ETC) uses the +buffers+cached.
The third line refers to the application from the point of view, buffers/cached is equal to the availability of the application, because buffer/cached is to improve the performance of the file read, when the application needs to use memory, buffer/cached will be quickly recycled.
So from the application's point of view, available memory = System Free memory+buffers+cached.
As in the above example:
2795064=16176+110652+2668236

Next, explain when the memory will be exchanged, and by what side. When the available memory is less than the rated value, a meeting is exchanged.
How to see the rating:
Cat/proc/meminfo

[Email protected] 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

Results viewed with free-m:
[[email protected] tmp]# free-m
              total       used       Free      shared    buffers     Cached
mem:           3189       3173          16          0         107       2605
-/+ buffers/cache:         460       2729
swap:          2000         78       1921


To view the size of the/proc/kcore file (memory image):
[Email protected] tmp]# ll-h/proc/kcore
-R--------1 root root 4.1G June 12:04/proc/kcore

Note:

Memory-intensive measurements

To measure how much memory a process consumes, Linux provides a convenient way for us to provide all the information we have in the/proc directory, and in fact the top tools are also available here to obtain the appropriate information.

Memory usage information for/proc/meminfo machines

The/proc/pid/maps PID is the process number that displays the virtual address occupied by the current process.

Memory occupied by the/PROC/PID/STATM process

[Email protected] ~]# CAT/PROC/SELF/STATM

654 57 44 0 0 334 0

Output interpretation

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

Parameter Interpretation/proc//status

Size (pages) task virtual address space VMSIZE/4

Resident (pages) the size of the physical memory that the application is using VMRSS/4

Shared (pages) pages 0

The size of the executable virtual memory owned by the TRS (pages) program VMEXE/4

The size of the library in which Lrs (pages) is imaged into 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 Machine available 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 look at the idle memory of the machine with the free command, we find that the value of the. This is mainly because, in Linux there is such a thought, the memory is not white, so it as far as possible cache and buffer some data to facilitate the next use. But in fact, the memory is also available for immediate use.

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

The top command is a common performance analysis tool under Linux that shows the resource usage of individual processes in the system in real time, similar to the Task Manager for Windows. Here is a detailed description of how it is used.

Top-02:53:32 up + days, 6:34, 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:4042764k Total, 4001096k used, 41668k free, 383536k buffers
swap:2104472k Total, 7900k used, 2096572k free, 1557040k cached

PID USER PR NI VIRT RES SHR S%cpu%MEM time+ COMMAND
32497 Jacky 0 669m 222m 31m R 5.6 29:27.62 Firefox
4788 yiuwing 0 257m 18m 13m S 5 0.5 5:42.44 Konsole
5657 liuxiaof 0 585m 159m 30m S 4 4.0 5:25.06 Firefox
4455 XIEFC 0 542m 124m 30m R 4 3.1 7:23.03 Firefox
6188 liuxiaof 0 191m 17m 13m S 4 0.5 0:01.16 Konsole


The first five elements of the statistical information area are the statistical information of the whole system. The first line is the task queue information, with the execution result of the uptime command. The contents are as follows:

01:06:48 Current Time
Up 1:22 system run time, format last: minutes
1 user Current number of users logged on
Load average:0.06, 0.60, 0.48 system load, which is the average length of the task queue.
The three values were 1 minutes, 5 minutes, and 15 minutes ago to the present average.


The second to third behavior process and CPU information. When there are multiple CPUs, the content may be more than two lines. The contents are as follows:

tasks:29 Total Process Totals
1 running number of running processes
Sleeping the number of sleep processes
0 Number of processes stopped stopped
0 Zombie Number of zombie processes
CPU (s): 0.3% US user space consumes CPU percentage
1.0% SY core space CPU percent occupied
0.0% CPU Percentage of processes that have changed priority within NI user process space
98.7% ID Idle CPU percent
0.0% wa wait for the input output CPU time percentage
0.0% hi
0.0% si


The last two behavior memory information. The contents are as follows:

MEM:191272K Total Physical Memory
Total physical memory used by 173656k used
17616k free Memory Total
The amount of memory that 22052k buffers uses as the kernel cache
Total swap area of swap:192772k
Total swap area used by 0k used
192772k Free Swap Area total
123988k cached Buffers The total number of swap areas.
The in-memory content is swapped out to the swap area and then swapped in to memory, but the used swap area has not been overwritten.
This value is the size of the swap area where the content already exists in memory.
When the corresponding memory is swapped out again, it is no longer necessary to write to the swap area.


The details of each process are shown below the process information area statistics area. Let's start by understanding the meaning of the columns.

Ordinal column name meaning
A PID process ID
b PPID Parent Process ID
C ruser Real User name
D UID Process Owner's user ID
e username of user process Owner
Group name of the F group Process owner
The terminal name of the G TTY boot process. Processes that are not started from the terminal are displayed as?
H PR-Priority
I NI nice value. Negative values indicate high priority, positive values indicate low priority
The last CPU used by J P is meaningful only in a multi-CPU environment
K%cpu CPU time consumption percentage last updated to current
The total CPU time, in seconds, used by the timing process
The total CPU time used by the M time+ process, in units 1/100 seconds
n the percentage of physical memory used by the%MEM process
o The total amount of virtual memory used by the VIRT process, in kilobytes. Virt=swap+res
The P swap process uses the size of the virtual memory, which is swapped out, in kilobytes.
The size of the physical memory, in kilobytes, that the Q RES process uses and has not been swapped out. Res=code+data
R code executable code occupies the physical memory size, in kilobytes
The amount of physical memory that is used outside of the S data executable code (data segment + stack), in kilobytes
T SHR shared memory size, in kilobytes
U Nflt page Error count
V NDRT the number of pages that have been modified in the last write to now.
W S Process state.
D = Non-interruptible sleep state
R = Run
S = Sleep
T = trace/Stop
Z = Zombie Process
x command name/command line
Y Wchan If the process is sleeping, the system function name in sleep is displayed
Z Flags task Flag, reference sched.h


By default, only the more important PID, USER, PR, NI, VIRT, RES, SHR, S,%cpu,%MEM, time+, and COMMAND columns are displayed. You can change the display by using the following shortcut keys.
Change the display by using the F key to select what to display. Press the F key to display a list of columns, press A-Z to show or hide the corresponding column, and then press ENTER to confirm.
Press the O key to change the order in which the columns are displayed. A-Z in the lower case moves the corresponding column to the right, while the uppercase A-Z moves the corresponding column to the left. Finally, press ENTER to confirm.
Press the uppercase F or O key, and then press A-Z to sort the process by the appropriate column. The uppercase R key can reverse the current sort.

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

While the top command is in use, you can also use some interactive commands to complete the functionality of other parameters. These commands are started by using shortcut keys.
Space: Refresh now.
P: Sort According to CPU usage size.
T: Sort by time, cumulative time.
Q: Exit the top command.
M: Toggles display memory information.
T: Toggles display of process and CPU status information.
C: Toggle display command name and full command line.
M: Sorts according to the size of the memory used.
W: Writes the current settings to the ~/.TOPRC file. This is the recommended way to write top configuration files.

As you can see, the top command is a very powerful tool for monitoring systems and is especially important for system administrators. However, its disadvantage is that it consumes a lot of system resources.

Application examples
You can use the top command to monitor a specified user, and the default is to monitor processes for all users. If you want to view the situation of the specified user, press the "U" key in the terminal and enter the user name, the system will switch to the user's process run interface.
A. Role
The free command is used to display memory usage, and the use of permissions is for all users.
B. Format
Free [-b-k-M] [-O] [-s delay] [-t] [-v]
C. Main parameters
-b-k-M: Displays memory usage in bytes (KB, MB), respectively.
-S delay: Displays the number of seconds per second to show memory usage.
-T: Displays the memory sum column.
-O: Do not display buffer throttling columns.
D. Application examples
The free command is the primary command used to view memory usage. Compared to the top command, it has the advantage of being simple to use and consuming very little system resources. With the-s parameter, you can use the free command to continuously monitor how much memory is in use, which can be used as a handy real-time monitor.
#free-b-S5
With this command, the terminal continuously reports memory usage (in bytes), updated every 5 seconds.

Linux view CPU and memory usage

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.