Linux memory viewing and management

巴扎黑
Release: 2017-08-02 16:07:21
Original
1719 people have browsed it

During system maintenance, it may be necessary to check the CPU usage at any time and analyze the system status based on the corresponding information. In CentOS, you can use the top command to view CPU usage. After running the top command, the CPU usage status will be displayed in full screen and in conversation mode - using top-based commands, you can control the display mode and so on. The command to exit top is q (press the q key once while top is running).

The top command is a commonly used performance analysis tool under Linux. It can display the resource usage of each process in the system in real time. It is similar to the Windows Task Manager

You can directly use the top command to view Contents of %MEM. You can choose to view by process or by user. If you want to view the process memory usage of the oracle user, you can use the following command:
$ top -u oracle

Explanation of content:

PID: ID of the process
USER: Process owner
PR: Priority level of the process, the smaller the priority, the higher priority it will be executed
NInice: Value
VIRT: The virtual memory occupied by the process
RES: Physical memory occupied by the process
SHR: Shared memory used by the process
S: Status of the process. S means sleeping, R means running, Z means zombie state, N means the priority value of the process is negative
%CPU: CPU usage occupied by the process
%MEM: The percentage of physical memory and total memory used by the process
 TIME+: The total CPU time occupied by the process after it is started, that is, the accumulated value of the CPU usage time.
COMMAND: Process startup command name

Operation example:

Enter "top"

in the command line to start top

The full-screen dialogue mode of top can be divided into 3 parts: system information bar, command input bar, and process list bar.

The first part - the top system information bar:

The first line (top):

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

"3:35" is the operating time since the system was started;

"2 users" is the users currently logged in to the system, or more precisely, the number of terminals logged in to the user-- The same user's connections to multiple terminals of the system at the same time will be regarded as multiple users connecting to the system, and the number of users here will also be expressed as the number of terminals;

"Load average" is the current system load Average value. The next three values ​​are the average number of processes 1 minute ago, 5 minutes ago, and 15 minutes ago. Generally speaking, it can be considered that when this value exceeds the number of CPUs, the CPU will have a hard time loading the processes included in the current system;

The second line (Tasks):

"59 total" is the total number of current system processes;

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

"58 sleeping" is the number of processes currently in the waiting state;

"0 stopped" is the number of system processes that have been stopped;

"0 zombie" is the process that has been restored Number;

The third line (Cpus):

represents the current usage of the CPU;

The fourth line (Mem):

respectively Indicates the total amount of memory, current usage, free memory, and the amount of memory in buffer use;

The fifth line (Swap):

The representation category is the same as the fourth line (Mem), But this reflects the usage of swap partition (Swap). Usually, frequent use of the swap partition (Swap) will be regarded as caused by insufficient physical memory.

The second part - the internal command prompt bar in the middle part:

When top is running, you can control the display mode of the process through the internal command of top. The internal commands are as follows:

 s

 -Change the screen update frequency

 l-Turn off or enable the display of top information in the first line of the first part

 t - Turn off or enable the display of the Tasks information in the second line of the first part and the Cpus information in the third line

m - Turn off or enable the Mem and Swap information in the fourth line of the first part

N - Arrange the process list in order of PID size (described later in the third part)

P - Arrange the process list in the order of CPU usage (described later in the third part)

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

h - Display help

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

q - Exit top

s -

Change the screen update cycle

The third part - the process list column at the bottom:

Distinguished by PID The process list will be updated regularly according to the set screen update time. The display mode here can be controlled through the top internal command

pmap

You can view the memory occupied by process-related information according to the process (the process number can be viewed through ps) As shown below:
$ 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 implements sorting by memory, from large to small

To check the memory under Linux, we usually use the free command:
[root@scs-2 tmp]# free
                                                                                                                         0   110652   2668236
-/+ buffers/cache: 471116 2795064
Swap: 2048276 80160 1968116

The following is an explanation of these values:

total: The total size of physical memory.
used: How big has been used.
free: How many are available.
Shared: The total amount of memory shared by multiple processes.
Buffers/cached: The size of the disk cache.
The third line (-/+ buffers/cached):
used: How big has been used.
free: How many are available.
The fourth line will not be explained much.
Difference: The difference between used/free in the second line (mem) and used/free in the third line (-/+ buffers/cache). The difference between the two lies in the perspective of use. The first line is from the perspective of the OS. Because for the OS, buffers/cached are all used, so its available memory is 16176KB and the used memory is 3250004KB. These include +buffers+cached used by the kernel (OS) using +Application (X, oracle, etc).
The third line refers to the application point of view. For the application, buffers/cached It is equal to available, because buffer/cached is to improve the performance of file reading. When the application needs to use memory, buffer/cached will be recycled quickly.
So from the perspective of the application, available memory = system free memory+buffers+cached.
Same as the above example:
2795064=16176+110652+2668236

Next, explain when the memory will be swapped, and in what way. When the available memory is less than the rated value, a swap will occur.

How to see the 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
Write:                  0 kB
Mapped:           345360 kB
Slab: 112344 kB
Committed_AS: 535292 kB
PageTables: 2340 kB
VmallocTotal: 536870911 kB
VmallocUsed: 272696 kB
VmallocChunk: 536598 175 kB
HugePages_Total: 0
HugePages_Free : 0
hugePageSize: 2048 kb

The result of viewing with free -m:

[root@SCS -2 TMP]#Free -m
Total used buffers cached
Mem: 3189 3173 16 0 107 2605
-/+ buffers/cache: 460 2729
Swap: 2 000 78 1921


View the size of the /proc/kcore file (memory image):
[root@scs-2 tmp]# ll -h /proc/kcore
-r------- - 1 root root 4.1G Jun 12 12:04 /proc/kcore

Remarks:

Measurement of occupied memory

Measuring how much memory a process occupies, linux does it for us Provides a very convenient method. The /proc directory provides us with all information. In fact, tools such as top also use this to obtain corresponding information.

/proc/meminfo Machine memory usage information

/proc/pid/maps pid is the process number, showing the virtual address occupied by the current process.

/proc/pid/statm Memory occupied by the process

[root@localhost ~]# cat /proc/self/statm

654 57 44 0 0 334 0

Output explanation

CPU and CPU0. . . The meaning of each parameter in each line (taking the first line as an example) is:

Parameter explanation/proc//status

Size (pages) The size of the task virtual address space VmSize/4

Resident(pages) The size of the physical memory being used by the application VmRSS/4

Shared(pages) The number of shared pages 0

Trs(pages) The available memory owned by the program The size of the execution virtual memory VmExe/4

Lrs(pages) The size of the library that is mapped to the virtual memory space of the task VmLib/4

Drs(pages) The program data segment and user mode The size of the stack (VmData+ VmStk) 4

dt(pages) 04

View the available memory of the machine

/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 check the free memory of the machine through the free command, we will find that the value of free is very small. This is mainly because there is a thought in Linux that memory is not used in vain, so it caches and buffers some data as much as possible to facilitate next use. But in fact, these memories can be used immediately.

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

The top command is a commonly used performance analysis tool under Linux, which can display the resource usage of each process in the system in real time, similar to Windows Task Manager. Its use is described in detail below.

top - 02:53:32 up 16 days, 6:34, 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: 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 20 0 669m 222m 31m R 10 5.6 29:27.62 firefox
4788 yiuwing 20 0 257m 18m 13m S 5 0.5 5:42.44 konsole
5657 Liuxiaof 20 0 585m 159m 30m S 4 4.0 5:25.06 firefox
4455 xiefc 20 0 542m 124m 30m R 4 3.1 7:23.03 firefox
6188 Liuxiaof 20 0 191m 17m 13m S 4 0.5 0:01.16 konsole


The first five lines of 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 1:22 System running time, in the format of hours:minutes
1 user Current number of logged in 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 now.


The second and third lines are process and CPU information. When there are multiple CPUs, these may take more than two lines. The content is as follows:

Tasks: 29 total total number of processes
1 running number of running processes
28 sleeping number of sleeping processes
0 stopped number of stopped processes
0 zombie zombie process Number
Cpu(s): 0.3% us Percentage of CPU occupied by user space
1.0% sy Percentage of CPU occupied by kernel space
0.0% ni Percentage of CPU occupied by processes that have changed priorities in the user process space
98.7% id idle CPU percentage
0.0% wa percentage of CPU time waiting for input and output
0.0% hi
0.0% si


The last two lines are memory information. The content is as follows:

Mem: 191272k total Total physical memory
173656k used Total physical memory used
17616k free Total free memory
22052k buffers Amount of memory used as kernel cache
Swap: 192772k total Total amount of swap area
0k used Total amount of swap area used
192772k free Total amount of free swap area
123988k cached Total amount of buffered swap area.
The content in the memory was replaced to the exchange area, and then it was replaced to the memory, but the exchanges used in the exchange area have not been covered.
This value is that these contents are size.
There is no need to write to the swap area when the corresponding memory is swapped out again.


The detailed information of each process is displayed below the statistical information area of ​​the process information area. First, let’s understand 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 process The owner's user name
f GROUP The group name of the process owner
g TTY The terminal name that starts the process. Processes that are not started from the terminal are displayed as ?
h PR priority
i NI nice value. Negative values ​​represent high priority, positive values ​​represent low priority
j P The last CPU used, only meaningful in a multi-CPU environment
k %CPU The percentage of CPU time occupied since the last update
l TIME Total CPU time used by the process, unit second
m TIME+ Total CPU time used by the process, unit 1/100 second
n %MEM Percentage of physical memory used by the process
o VIRT Virtual memory used by the process Total amount, unit kb. VIRT=SWAP+RES
p SWAP The size of the virtual memory used by the process to be swapped out, in kb.
q RES The size of the physical memory used by the process and not swapped out, in kb. RES=CODE+DATA
r CODE The size of the physical memory occupied by the executable code, in kb
s DATA The size of the physical memory occupied by parts other than the executable code (data segment + stack), in kb
t SHR Shared memory size, unit kb
u nFLT Number of page faults
v nDRT The number of pages that have been modified since the last write.
w S Process status.
           D = Uninterruptible sleep state
              R =                                                                                                                                                              
y WCHAN If the process is sleeping, the system function name in sleep
z Flags task flag, refer to sched.h


By default, only the more important PID, USER, PR are displayed , NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, COMMAND columns. The displayed content can be changed using the shortcut keys below.
Change the displayed content You can select the displayed content through the f key. After pressing the f key, a list of columns will be displayed. Press a-z to show or hide the corresponding columns. Finally, press the Enter key to confirm.

Press the o key to change the display order of columns. Pressing lowercase a-z moves the corresponding column to the right, while uppercase A-Z moves the corresponding column to the left. Finally press the Enter key to confirm.

Press the uppercase F or O key, then a-z to sort the processes by the corresponding column. The uppercase R key reverses the current sorting.



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

top command During use, you can also use some interactive commands to complete the functions of other parameters. These commands are launched via shortcut keys.

<Space>: Refresh immediately.

P: Sort according to CPU usage.

T: Sort according to time and accumulated time.

q: Exit the top command.
m: Switch display of memory information.
t: Switch to display process and CPU status information.
c: Switch between displaying command name and complete command line.
M: Sort according to the memory size used.
W: Write 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 the system, which is especially important for system administrators. However, its disadvantage is that it consumes a lot of system resources.

Application Example
Use the top command to monitor specified users. The default is to monitor the processes of all users. If you want to check the status of a specified user, press the "U" key in the terminal, then enter the user name, and the system will switch to the process running interface of the specified user.
a. Function
The free command is used to display memory usage, and the usage permission is for all users.
b.Format
free [-b -k -m] [-o] [-s delay] [-t] [-V]
c.Main parameters
-b -k - m: Display memory usage in bytes (KB, MB).
-s delay: Displays the number of seconds to display memory usage.
-t: Display the memory sum column.
-o: Do ​​not display the buffer adjustment column.
dd.Application Example
The free command is the main command used to check memory usage. Compared with the top command, its advantage is that it is simple to use and only takes up very few system resources. Through the -S parameter, you can use the free command to continuously monitor how much memory is in use, so that you can use it as a convenient real-time monitor.
#free -b -s5
After using this command, the terminal will continuously report memory usage (in bytes), updated every 5 seconds.

The above is the detailed content of Linux memory viewing and management. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!