天天看點

linux程序記憶體實際使用,linux – 如何衡量應用程式或程序的實際記憶體使用情況?...

這個問題非常詳細地介紹了here.

如何衡量Linux中應用程式或程序的記憶體使用情況?

Why ps is “wrong”

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely wrong.

解決方法:

使用ps或類似工具,您将隻獲得該程序配置設定的記憶體頁數.這個數字是正确的,但是:

>不反映應用程式使用的實際記憶體量,僅反映為其保留的記憶體量

如果頁面是共享的,則可能會産生誤導,例如,通過多個線程或使用動态連結庫

如果您真的想知道應用程式實際使用的記憶體量,則需要在分析器中運作它.例如,valgrind可以讓您了解所使用的記憶體量,更重要的是,可以了解程式中可能存在的記憶體洩漏情況. valgrind的堆分析器工具稱為“massif”:

Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program’s heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

如valgrind documentation中所述,您需要通過valgrind運作程式:

valgrind --tool=massif

Massif寫了一個記憶體使用快照轉儲(例如massif.out.12345).它們提供(1)記憶體使用的時間線,(2)每個快照,記錄程式記憶體的配置設定位置.

用于分析這些檔案的一個很棒的圖形工具是massif-visualizer.但我發現ms_print是一個随valgrind一起提供的簡單的基于文本的工具,已經給了它很大的幫助.

要查找記憶體洩漏,請使用valgrind的(預設)memcheck工具.

标簽:linux,memory,process

來源: https://codeday.me/bug/20190911/1803493.html