1. 程式人生 > >《linux效能及調優指南》 3.3 記憶體瓶頸

《linux效能及調優指南》 3.3 記憶體瓶頸

    • 摘要:3.3記憶體瓶頸OnaLinuxsystem,manyprogramsrunatthesametime.Theseprogramssupportmultipleusers,andsomeprocessesaremoreusedthanothers.Someoftheseprogramsuseaportionofmemorywhiletherestare“sleeping.”Whenanapplicationaccessescache,theperformanceincrease
    • 3.3 記憶體瓶頸
      On a Linux system, many programs run at the same time.
      These programs support multiple users, and some processes are more used than others.
      Some of these programs use a portion of memory while the rest are “sleeping.”
      When an application accesses cache, the performance increases because an in-memory access retrieves data,
      thereby eliminating the need to access slower disks.
      在linux系統中,在同一時間有支援多個使用者的多個程式在執行,它們對記憶體使用有多有少;
      應該程式訪問記憶體資料的效率要比磁碟高;


      The OS uses an algorithm to control which programs will use physical memory and which are paged out.
      This is transparent to user programs. Page space is a file created by the OS on a disk partition
      to store user programs that are not currently in use. Typically, page sizes are 4 KB or 8 KB.
      In Linux, the page size is defined by using the variable EXEC_PAGESIZE in the include/asm-<architecture>/param.h
      kernel header file. The process used to page a process out to disk is called pageout.
      作業系統使用一定的策略來決定哪些程式將使用實體記憶體,哪些程式將被從記憶體頁面中替出;
      這些操作對使用者程式來說是透明的;
      頁面空間是由作業系統在磁碟劃分上建立的檔案,用來儲存當前未在使用的使用者程式;
      通常,頁面大小為 4KB 或 8KB;
      在Linux系統中,頁面大小由核心的標頭檔案:
      include/asm-<architecture>/param.h
      中的巨集 EXEC_PAGESIZE 來定義;
      程序使用的頁面被替換到磁碟稱為換頁 或 頁面溢位;


      3.3.1 Finding memory bottlenecks
      尋找記憶體瓶頸
      文章的這部分使用了KDE的一個工具KDE System Guard來做記憶體檢測;

      KDE,K桌面環境(Kool Desktop Environment)的縮寫。
      一種著名的運行於 Linux、Unix 以及FreeBSD 等作業系統上面自由圖形工作環境,
      整個系統採用的都是 TrollTech 公司所開發的Qt程式庫(現在屬於Digia公司)。
      KDE 和 Gnome 都是 Linux 作業系統上最流行的桌面環境系統。

      Start your analysis by listing the applications that are running on the server.
      Determine how much physical memory and swap each application needs to run.
      Figure 3-1 on page 83 shows KDE System Guard monitoring memory usage.

      ? 《linux效能及調優指南》3.3記憶體瓶頸

      圖 3.1 KDE System Guard記憶體監測

      ?The indicators in Table 3-1 can also help you define a problem with memory.
      《linux效能及調優指南》3.3記憶體瓶頸
      表3-1

      Paging and swapping indicators
      換頁和交換的意義
      In Linux, as with all UNIX-based operating systems, there are differences between paging and swapping.
      Paging moves inpidual pages to swap space on the disk;
      swapping is a bigger operation that moves the entire address space of a process to swap space in one operation.
      在Linux系統中,分佈和交換是不同的;
      換頁是將獨立的頁面移到磁碟的交換空間;
      交換是更大的操作,它將整個程序的地址空間一次性移到磁碟的交換空間;


      Swapping can have one of two causes:
      導致交換的原因有兩個
      . A process enters sleep mode.
      程序進入 sleep 模式
      This usually happens because the process depends on interactive action and editors, shells,
      and data entry applications spend most of their time waiting for user input. During this time,
      they are inactive.
      這種情況通常是因為程序需要和編輯器,shell等互動,以及應用程式需要等待使用者的資料輸入;
      在這是,它是非活動的;


      . A process behaves poorly.
      程序的異常行為
      Paging can be a serious performance problem when the amount of free memory pages falls
      below the minimum amount specified, because the paging mechanism is not able to handle
      the requests for physical memory pages and the swap mechanism is called to free more pages.
      This significantly increases I/O to disk and will quickly degrade a server’s performance.
      當空閒記憶體頁面過小時,換頁會導致嚴重的效能問題,
      因為換頁機制不能處理實體記憶體頁面的請求,而交換機制將會呼叫更多的空閒記憶體頁面。
      這會急劇增加磁碟的I/O, 並快速地拉低伺服器的效能

      If your server is always paging to disk (a high page-out rate), consider adding more memory.
      However, for systems with a low page-out rate, it might not affect performance.
      如果伺服器總是在換頁(一個很高的換頁率),就需要考慮增加記憶體;
      當然,系統換頁率低時,它不會影響效能;

      3.3.2 Performance tuning options
      效能調整選項
      It you believe there is a memory bottleneck,
      consider performing one or more of these actions:
      如果你確認是記憶體的瓶頸,可以考慮用下面的辦法來解決
      . Tune the swap space using bigpages, hugetlb, shared memory.
      調整交換空間,使用大頁面,大塊記憶體,共享記憶體

      . Increase or decrease the size of pages.
      增加或降低頁面的大小;
      ?
      . Improve the handling of active and inactive memory.
      改進對活動和非活動記憶體的處理;

      . Adjust the page-out rate.
      調整換頁率;
      ?
      . Limit the resources used for each user on the server.
      限制伺服器上每個使用者的可用資源;

      . Stop the services that are not needed, as discussed in “Daemons” on page 97.
      停掉不需要的服務

      . Add memory.
      增加記憶體;