views:

613

answers:

9

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.

A: 

Depends on your operating system - you would expect to have tools to tell you the memory consumed when the apps are running.

Trying to infer the answer by inspecting the code would be very difficult, run the apps, use your platform's diagnoistics.

djna
A: 

Depending on the size of the programs, this could be nearly unfeasible.

If they are not very large, then you can see how much memory they allocate; for instance, an int would take up 4 bytes, a char would take 1 byte, etc. Assembly is very transparent in how much memory it is using, even on an x86 machine. Cpp is nearly as transparent, as long as you faithfully track object creation and memory destruction/allocation.

If the program is huge, you'll need to use specific tools for tracking/profiling memory use, like GlowCode (http://www.glowcode.com/summary.htm).

Isaac Hodes
+2  A: 

The Windows Task manager can show you the memory usage of each process. I guess you could use Valgrind instead, but I don't see the point in that. On Linux, use Valgrind or ps.

Mads Elvheim
In the Task Manager Processes Tab, you can use View->Select Columns... to select additional memory usage statistics.
Clifford
A: 

On Windows, you can use Microsoft's Performance Monitor to do it. Start, Run, "perfmon". This tool will report on all sorts of statistics about processes, and provide graphs for you. In general, you're going to be interested in reporting on the "Private working set". This will tell you how much memory your process has reserved for its own use.

If you want to just get your heap usage, and you want to do it programatically, you should look into the CRT Debug Heap.

I'm not sure about Linux though, sorry.

Martin
+2  A: 

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.

discovlad
hmm, 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.
oh 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
discovlad
Usually 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.
Benjamin
+2  A: 

On Windows you can use the GetProcessMemoryInfo Function.

Here is an example on how to use it:
Collecting Memory Usage Information For a Process

Nick D
A: 

On Windows, I have found Address Space Monitor very useful, especially for looking at how fragmented your memory is.

Andy Balaam
+1  A: 

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.

Artelius
+5  A: 

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

  1. 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.

sat