3

I have a small program in c

#include <stdio.h>
#include <string.h>
#define SIZE 30

int main()
{
  char name[ SIZE ]; 
  FILE *fpPtr; 

  if ( ( fpPtr = fopen( "sorted_file.txt", "r" ) ) == NULL ) {
    printf( "File could not be opened\n" );
  } 
  else {
    printf( "%s\n", "Name" );
    fscanf( fpPtr, "%s", name );

    while( !feof( fpPtr ) ) {
       printf( "%s\n", name );
       fscanf( fpPtr, "%s", name );
    } 

    fclose( fpPtr ); 
  } 

 return 0; 
};

I want to find out how much memory is this program using . Any code or function i should add to find the total memory used by this program. I don't want to check task manager for this. I need to print the memory usage.

Ken White
  • 123,280
  • 14
  • 225
  • 444
shruti_rathee
  • 61
  • 2
  • 12
  • Do you want to know the incremental usage of such a program? Or do you want to know the full memory cost of the program, including libraries, o/s overhead for open files, etc.? – wallyk Jan 20 '12 at 00:44
  • possible duplicate of [Memory usage of a C program under Windows](http://stackoverflow.com/questions/8903512/memory-usage-of-a-c-program-under-windows) – Ken White Jan 20 '12 at 00:46
  • 2
    This question is significantly better than your previous question, good work there, but please do not open new questions when you don't get answers. Either improve the original question to the point where it _can_ get answers, or [post a bounty](http://stackoverflow.com/faq#bounty) to draw more attention to your question. Thanks! – sarnold Jan 20 '12 at 00:53

2 Answers2

3

It sounds like you're after GetProcessMemoryInfo().

You'll need the Windows SDK or the appropriate windows headers from mingw32 in order to compile this (will also work in Visual Studio, too, as well as cygwin etc with appropriate libraries):

#include <windows.h> // these header gives you access to the Windows API -
#include <psapi.h> // include at the top of your code

// do this where you need to get that information - perhaps create a function
// for it.

HINSTANCE hProcHandle = GetModuleHandle(NULL);  // get the current process handle
PROCESS_MEMORY_COUNTERS_EX memory; // output will go here.

/* call function */
GetProcessMemoryInfo(hProcHandle, &memory, sizeof(memory));

You should now be able to access memory.WorkingSetSize or memory.PrivateUsage, whichever measure matters to you. These are size_t types which are unsigned integers of size dependent on your system.

2

You asked this just the other day over here, and I gave you this answer:

Although it's no longer being actively developed, for C and ObjC code (all I've used it for), I haven't found anything better than MSS: Memory Supervision System.

It's extremely straightforward and easy to setup; has been very accurate in my experience and I've used it to great effect to weed out any memory leaks in my applications.

To give you an idea of how easy it is to use: I compiled it into a static library, link it to my program's debug targets and put the include (import for ObjC) for the singular header into a common shared header that all the rest of my program uses, so it tracks memory across the entire program without even needed to think about it.

MSS will do exactly what you're asking for; it'll give you a ton of extra (extremely-useful) information as well, but the top of the report includes these lines:

MSG: Listing info about allocated blocks at end of program:
INFO: 107 blocks currently allocated
INFO: 11720 bytes of memory currently used.
INFO: 2384479 bytes maximum memory used.

In just those lines, it tells you how many blocks of memory leaked, exactly how much memory leaked and the maximum memory footprint your program used.

How to use MSS:

First build the library using the makefile; just use make lib as we don't need the test programs. If you're not using gcc, you'll need to change the default compiler in the makefile. If you're using gcc and want both 64-bit and 32-bit versions, add either -m64 or -m32 to line 53 CFLAGS=... and then just run make on the file.

You should now have a static library of MSS you can link into your program. The only header you need to include in your program along with the library is mss.h. To actually use MSS, you need to #define MSS and #include <mss.h> into every file you wish to have it track. If you want the entire program tracked, define MSS in the arguments to the compiler and include into a universal header that every source file includes.

That is literally it; there's config files you can add to your program directory if you wish to change the verbosity of its logs and other features, but to just get it working, that is all you need to do. By the way, MSS includes a full read me in its doc folder that describes all this in much more detail.

Community
  • 1
  • 1
Rabbit
  • 566
  • 5
  • 13
  • but i didn't understood how to use this – shruti_rathee Jan 20 '12 at 00:45
  • This answer better than your other one, so good work there, but in general please _flag_ duplicate posts for moderator attention when you find them. It helps us prune the site if dupes are caught early. :) Thanks! – sarnold Jan 20 '12 at 00:51
  • I've expanded the details on how to actually use MSS; if you still don't understand, I highly suggest downloading it and reading the instructions it includes. It gives very detailed instructions on how to get it working first, and then how to configure and fully use it. MSS really is one of the easiest libraries of this nature you can use. – Rabbit Jan 20 '12 at 01:03