View CPU and memory usage in Linux

Source: Internet
Author: User

In Linux, you can view CPU and memory usage during system maintenance. You may need to view 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 example: Enter "top" in the command line to start the full-screen dialog mode of top, which can be divided into three parts: system information, command input, and process list. Part 1-system information column at the top: Line 1 (top): "00:11:04" indicates the current time of the system; "" indicates 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 one minute ago, five minutes ago, and 15 minutes ago. Generally, when this value exceeds the number of CPUs, the CPU will be hard to load the processes contained in the current system; the second line (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 state; "0 stoped" indicates the number of stopped system processes; "0 zombie" indicates the number of recovered processes; and the third line (Cpu S): indicates the current CPU usage. Row 4 (Mem) indicates the total memory, current usage, idle memory, and buffer memory; the fifth line (Swap) indicates that the category is the same as the fourth line (Mem), but this shows the usage of Swap. In general, Swap partitions (Swap) are frequently used and are considered to be caused by insufficient physical memory. The second part -- the Internal Command Prompt bar in the middle section: In top running, you can use the top internal command to control the display mode of the process. The internal commands are as follows: s-change the screen update frequency l-close or enable the first line of top information in the first line t-close or enable the first part of the second line of Tasks and the third line of Cpus information m-close or enable the expression N of the first part of the fourth line Mem and the fifth line Swap information. In the order of the PID size, the process list is displayed. (the third part is described later) p-process list in the order of CPU usage (Part 3 and later) M-process list in the order of memory usage (Part 3 and 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. Through the top Internal commands can control the display mode here in Linux to view the memory we generally use the free command: [root @ scs-2 tmp] # free total used free shared buffers cachedMem: 3266180 3250004 16176 0 110652-/+ buffers/cache: 2668236 471116 Swap: 2795064 2048276 80160 the following is an explanation of these values: 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. 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 line indicates that, from the application perspective, for applications, buffers/cached is equivalent to available, because buffer/cached is designed to improve file read performance, when the application needs to use the memory, buffer/cached will be quickly recycled. 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. View rating: cat/proc/meminfo [root @ scs-2 tmp] # cat/proc/meminfoMemTotal: 3266180 kBMemFree: 17456 kBBuffers: 111328 kBCached: 2664024 kBSwapCached: 0 kBActive: 467236 kBInactive: 2644928 kBHighTotal: 0 kBHighFree: 0 kBLowTotal: 3266180 kBLowFree: 17456 kBSwapTotal: 2048276 kBSwapFree: 1968116 kBDirty: 8 kBWriteback: 0 kBMapped: 345360 kBSlab: 112344 Bytes: 535292 kBPageTables: 2340 bytes Otal: 536870911 kBVmallocUsed: 272696 kBVmallocChunk: 536598175 kBHugePages_Total: 0HugePages_Free: 0 Hugepagesize: 2048 kB result viewed with free-m: [root @ scs-2 tmp] # free-m total used free shared buffers cachedMem: 3189 3173 16 0 107 2605-/+ buffers/cache: 460 2729 Swap: 2000 78 1921 view/proc/kcore file size (memory image ): [root @ scs-2 tmp] # ll-h/proc/kcore-r -------- 1 root 4.1G Jun 12/proc/kcore remarks: 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. The memory usage information of the/proc/meminfo machine/proc/pid/maps pid indicates the process number and displays the virtual address occupied by the current process. /Proc/pid/memory occupied by the statm process [root @ localhost ~] # Cat/proc/self/statm 654 57 44 0 0 0 0 output explanation CPU and CPU 0... The meaning of each parameter in each row (in the first behavior example) is: Parameter Interpretation/proc // status Size (pages) task virtual address space Size VmSize/4 Resident (pages) the physical memory size that the application is using. VmRSS/4 Shared (pages) Shared pages 0 Trs (pages) the executable virtual memory size of the program VmExe/4 Lrs (pages) size of the library 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 check 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. Therefore, the free memory = free + buffers + cached = total-used 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 zombieCpu (s ): 10.3% us, 1.8% sy, 0.0% ni, 86.6% id, 0.5% wa, 0.2% hi, 0.6% si, 0.0% stMem: 4042764 k total, 4001096 k used, 41668 k free, 383536 k buffersSwap: 2104472 k total, 7900 k used, 2096572 k free, 1557040 k cached pid user pr ni virt res shr s % CPU % mem time + COMMAND32497 jacky 20 0 669 m 222 m 31 m R 10 5.6. 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 The first five lines in the konsole 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. Content: Tasks: 29 total process count 1 running processes running 28 sleeping sleep processes 0 stopped processes 0 zombie processes Cpu (s ): 0.3% us user space CPU usage 1.0% sy kernel space CPU usage 0.0% ni user process space CPU usage of processes that have changed their priorities 98.7% id idle CPU usage 0.0% wa CPU waiting for Input and Output time percentage 0.0% hi 0.0% si last two behavior memory information. Content: Mem: 191272 k total physical memory total 173656 k used total physical memory used 17616 k free memory total 22052 k buffers used as kernel cache memory Swap: 192772 k total swap areas total 0 k used swap areas total 192772 k free swap areas total 123988 k cached buffer swap areas total. 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 is the size of the SWAp zone where the content already exists. 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 process owner USER id e user process owner USER name f GROUP process owner GROUP name g TTY Start Process terminal name. Processes not started from the terminal are displayed? H PR priority I NI nice value. A negative value indicates a high priority. A positive value indicates the CPU used at the low priority j P, only meaningful k % CPU usage percentage of cpu time last updated to the present in multi-CPU environment l total cpu time used by the TIME process, in seconds m TIME + total cpu time used by the process, unit: 1/100 seconds n % MEM percentage of physical memory used by the process o total virtual memory used by the VIRT process, unit: kb. VIRT = SWAP + RES p in the virtual memory used by the SWAP process, measured in kb. The size of the physical memory used by the q RES process, Not swapped out, in kb. RES = CODE + DATA r CODE: the physical memory occupied by executable CODE. Unit: kb. The physical memory occupied by parts other than the executable CODE (DATA Segment + stack, unit: kb t SHR shared memory, unit: kb u nFLT page error count v. The number of modified pages that have been written to the present last time. W S Process status. D = non-disruptive sleep state R = running S = sleep T = tracking/stopping Z = zombie process x COMMAND name/COMMAND line y WCHAN if the process is sleeping, displays the system function name z Flags task identifier in sleep. For more information, see 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. ====================================== Top Command in use, you can also use interactive commands to complete other parameter functions. 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. The application instance can use the top command 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. The free command is used to display the memory usage. The permission is applied to all users. B. format: free [-B-k-m] [-o] [-s delay] [-t] [-V] c. main Parameter-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. The application instance 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 after using this command, the slave will continuously report memory usage (in bytes), updated every 5 seconds.

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.