linux ps命令

語法注意:html

ps(process status)命令帶有2種不同的風格,分別是BSD和UNIX。新用戶常常會混淆和錯誤地解釋這兩種風格。因此要弄清楚他們,繼續操做以前這裏是一些基本的信息。linux

注意:"ps aux"和"ps -aux"不相同。例如"-u"用來顯示該用戶的進程。可是"u"則是顯示詳細的信息。express

BSD風格:在BSD風格的語法選項前不帶連字符。apache

  1. ps aux 

UNIX/LINUX的風格:在linux風格的語法選項前面有一個破折號如常。…app

  1. ps -ef 

混合使用兩種Linux系統上的語法風格是好事兒。例如「ps ax -f」。但在這篇文章中,咱們將主要集中在UNIX風格的語法。less

如何使用ps命令呢?ssh

一、顯示全部進程:ide

下面的命令將列出全部的進程:工具

  1. $ ps ax 
  2. $ ps -ef 

加上管道輸出給less,來滾動顯示post

"u"或者"-f"參數來顯示全部進程的詳細信息

  1. $ ps aux 
  2. $ ps -ef -f 

注意:爲何用戶列不顯示個人用戶名,但顯示其餘用戶,如root、www等,對於全部的用戶名(包括你)若是長度大於8個字符,而後ps將只顯示UID,而不是用戶名。

三、經過名字和進程ID顯示進程:

經過名字或命令搜索進程,使用「-C」選項後面加搜索詞。

  1. $ ps -C apache2 
  2.   PID TTY          TIME CMD 
  3.  2359 ?        00:00:00 apache2 
  4.  4524 ?        00:00:00 apache2 
  5.  4525 ?        00:00:00 apache2 

------------------------

要對進程進行監測和控制,首先必需要了解當前進程的狀況,也就是須要查看當前進程,而 ps 命令就是最基本同時也是很是強大的進程查看命令。使用該命令能夠肯定有哪些進程正在運行和運行的狀態、進程是否結束、進程有沒有僵死、哪些進程佔用了過多的資源等等。總之大部分信息都是能夠經過執行該命令獲得的。

ps 爲咱們提供了進程的一次性的查看,它所提供的查看結果並不動態連續的;若是想對進程時間監控,應該用 top 工具

kill 命令用於殺死進程。

 

linux上進程有5種狀態: 

1. 運行(正在運行或在運行隊列中等待) 

2. 中斷(休眠中, 受阻, 在等待某個條件的造成或接受到信號) 

3. 不可中斷(收到信號不喚醒和不可運行, 進程必須等待直到有中斷髮生) 

4. 僵死(進程已終止, 但進程描述符存在, 直到父進程調用wait4()系統調用後釋放) 

5. 中止(進程收到SIGSTOP, SIGSTP, SIGTIN, SIGTOU信號後中止運行運行) 

 

ps工具標識進程的5種狀態碼: 

D 不可中斷 uninterruptible sleep (usually IO) 

R 運行 runnable (on run queue) 

S 中斷 sleeping 

T 中止 traced or stopped 

Z 僵死 a defunct (」zombie」) process 

ps 與grep 經常使用組合用法,查找特定進程

命令:

ps -ef|grep ssh

 

Terms

 

  • VSS- Virtual Set Size 虛擬耗用內存(包含共享庫佔用的內存)
  • RSS- Resident Set Size 實際使用物理內存(包含共享庫佔用的內存)
  • PSS- Proportional Set Size 實際使用的物理內存(比例分配共享庫佔用的內存)
  • USS- Unique Set Size 進程獨自佔用的物理內存(不包含共享庫佔用的內存)

通常來講內存佔用大小有以下規律:VSS >= RSS >= PSS >= USS

 

Overview

The aim of this post is to provide information that will assist in interpreting memory reports from various tools so the true memory usage for Linux processes and the system can be determined.

Android has a tool called procrank (/system/xbin/procrank), which lists out the memory usage of Linux processes in order from highest to lowest usage. The sizes reported per process are VSS, RSS, PSS, and USS.

For the sake of simplicity in this description, memory will be expressed in terms of pages, rather than bytes. Linux systems like ours manage memory in 4096 byte pages at the lowest level.

VSS (reported as VSZ from ps) is the total accessible address space of a process.This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.

RSS is the total memory actually held in RAM for a process.RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.

PSS differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.

USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaksin a process.

For systems that have Python available, there is also a nice tool calledsmem that will report memory statistics including all of these categories.

 

ps命令查出來的rss/vsz/size分別是何含義呢

 

rss RSS resident set size, the non-swapped physical memory that a task has used (in kiloBytes). (alias rssize, rsz).
vsz VSZ virtual memory size of the process in KiB (1024-byte units). Device mappings are currently excluded; this is subject
  to change. (alias vsize).
size SZ approximate amount of swap space that would be required if the process were to dirty all writable pages and then be
  swapped out. This number is very rough!
sz SZ size in physical pages of the core image of the process. This includes text, data, and stack space. Device mappings
  are currently excluded; this is subject to change. See vsz and rss.

 

 

SIZE: 進程使用的地址空間, 若是進程映射了100M的內存, 進程的地址空間將報告爲100M內存. 事實上, 這個大小不是一個程序實際使用的內存數.
RSS: "Resident Set Size", 實際駐留"在內存中"的內存數. 不包括已經交換出去的代碼. 舉一個例子: 若是你有一個程序使用了100K內存, 操做系統交換出40K內存, 那麼RSS爲60K. RSS還包括了與其它進程共享的內存區域. 這些區域一般用於libc庫等. SHARE: RSS中與其它進程共享的內存部分大小. VMSIZE: 一個進程佔用的總的地址空間大小. 它包括了沒有映射到內存中的頁面。 sz(Private RSS): 映射到內存中的頁面, 這些頁面僅由進程單獨使用. 這也是咱們最關心地方: 進程實際佔用的內存數。
相關文章
相關標籤/搜索