How do you get how much memory a program uses?
Solution 1
Run the program in one shell. Open another shell and run 'top' command. it will list running processes and home much memory they consume. you can, i guess, poll /proc/yourprocessid/stat to see how much memory it is using over time.
Solution 2
On Linux, try valgrind
. It's an amazing tool with too many features for mere mortals to totally comprehend. Have a look at valgrind
's massif
.
Solution 3
On Windows you can use Performance Monitor.
Performance monitor usage
Start Performance Monitor from Start menu/ Administrative Tools/ Performance
If you want to start the logging:
Select performance log and alert>Current Log option in the left side of the browser.
Select New Log Settings.
Give an apropriate name to the log e.g. performance_Server for Server
It will prompt you one menu. In “general” tabs click on the add button and select the process you want monitor. (Change the performance object to process, for “select counters from list” select “private bytes”, for “select instances from list”, select the process you want to monitor.) After that click on Add and close. Now change the interval as per test case requirement. Now go to “log files” tab change the log file type to either csv or tsv format. Now apply and press OK.
If you want to start/stop the logging:
Select the particular log you want to start and stop.
In toolbar above you will see start and stop button.
If you want to check the content of a log file:
Click Options/Data From…
Select the log file to be viewed, click OK
Go to the chart screen (View/Chart)
Click Edit/Add to chart
Add the required items to the chart. (In case the memory leakage is to be checked, then you need to view the PrivateBytes of the processes and the _Total of them)
Read the values from the chart (Min and Max values are displayed at the bottom of the chart)
If you want to monitor network transfer:
Display the chart screen (View/Chart)
Click Edit/Add to log, and select the items Network Interface\Bytes Sent If you set it in dl
Or Network Inerface\ Bytes Received if you set it in the CRS-PC+
Click Done
Monitor memory usage:
In menu Start/Programs/Administrative Tools/ start the program Performance Monitor
Click on the button to open the window that adds processes
Fill the fields as follows:
Object: Process
Counter: Private Bytes
Instance: a process whose Memory occupation need to be displayed
Click on Add button
Repeat the last two steps for every process the memory needs to be displayed
Close the window that adds processes
On the bottom of the Performance Monitor window, there is the list of the processes previously selected.
How to use the logged data
- Now open the file Perfmon_.csv or Perfmon_.tsv using WordPad or Excel.
If you have opened the file using Excel, then using the option Save As, save the file in the Microsoft Excel format.
Solution 4
On Windows you can use the GetProcessMemoryInfo Function.
Here is an example on how to use it:
Collecting Memory Usage Information For a Process
user176121
Updated on June 04, 2022Comments
-
user176121 almost 2 years
I have two programs, one in C++, the other in assembler. I want to compare how much memory they use when running respectively. How can I do this?
I am doing the testing on Windows, but I also would like to know how to do it on Linux.
-
user176121 over 14 yearshmm, this is pretty cool, it's like ps command but dynamic. although my programs are rather small so i don't think this particular method will work.
-
discovlad over 14 yearsoh if this is a short program. just put it in the loop while you are watching it in the 'top' for i in {1..3}; do ps; done; runs ps 3 times. Or if you can edit your program, add a sleep at the end so you have time to do 'ps' before the program ends
-
Wojtek Szafraniec over 14 yearsUsually in top I take the 'RES' column (resident size) minus the 'SHR' column (shared memory size). In most cases that provides a good indication of how much your program is really using.
-
Zaimatsu over 8 yearsIt's very useful, but massif analyzes heap memory only by default. As we can read on link: "By default, stack profiling is off as it slows Massif down greatly. [...] Stack profiling can be turned on with the --stacks=yes option."