Solved

How much memory my program use??

Posted on 2002-06-26
9
373 Views
Last Modified: 2010-04-01
I want to know how much memory my program use.
I'm using Borland C++ Builder 5.

If possible I want the result returned in KB and bytes.
Can someone give me a starting point for this.

Thanks
0
Comment
Question by:morees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7112902
If you're running Windows 2000, NT, or XP you can just run Task Manager--this lists the memory usage of all the processes on the system.
0
 

Author Comment

by:morees
ID: 7112997
I want to do this programmatically
0
 
LVL 2

Expert Comment

by:jonnin
ID: 7113307
Er, low level, you can read from the exe file (you can even read it in the program, if you pause long enough for it to finish loading to avoid file open error); the required memory is in there, you can probably find where on the web somewhere... Then add every byte you dynamically allocate, remove when freed...

I *am fairly sure* the memory is the same until dynamic allocation is used, just code segment + data segment + stack segment ...

you also should be able to query the os for this info but how in old Borland??? I have no idea...





0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 7113589
Call 'GetProcessMemoryInfo()' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/psapi_8d4f.asp). A sample can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/psapi_2oqb.asp ("Collecting Memory Usage Information For a Process"):

0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 7113592
Oops, forgot the sample code:

#include <windows.h>
#include <stdio.h>
#include "psapi.h"

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );

    // Print information about the memory usage of the process.

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
    if (NULL == hProcess)
        return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tPeakWorkingSetSize: 0x%08X\n",
                  pmc.PeakWorkingSetSize );
        printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
        printf( "\tPeakPagefileUsage: 0x%08X\n",
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}

Call that like

PrintMemoryInfo ( GetCurrentProcessId());
0
 
LVL 4

Expert Comment

by:havman56
ID: 7115868
morees see whether u get *.map file while u build ur project.

from map fiel u can calculate very simple u need not do it any programming ..

ther will be option on project setting to build map file.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7117012
Any how would the map file inform you about dynamically allocated memory?
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7117841
More to the point, the asker specifically said he wanted to find out programmatically after I posted my comment about Task Manager, so what use is a manual solution?
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7123776
jkr's suggestion is the only thing you can do to be totally sure. It does however require at least windows NT 4.0 and is not available in any of the win9x-versions (95/98/98se/me). I know, an answer of the "can't be done"-type isn't what you are looking for. But even if you wrote your own memory-manager to keep track of dynamically allocated memory how would you deal with memory allocated by a .dll? You can't since it's compiled code and the only one that can be absolutely sure about memory usage is the one that's providing it, namely the OS.
Btw. writing a *VERY* simple memory manager isn't all that hard. Just put something like that in your code
#ifdef _TRACK_MEM_USAGE_
// variables for tracking dynamic memory usage
static size_t mem_used = 0;
static size_t mem_max  = 0;
// new-operator
void* operator new( size_t nSize ) {
    mem_used += nSize;
    mem_max = max( mem_max, mem_used );
    void* p = malloc( nSize + sizeof( size_t ) );
    *p = nSize;
    return( p + sizeof( size_t ) );
}
void operator delete( void* p ) {
    p -= sizeof( size_t );
    mem_used -= *p;
    free( p );
}
#endif
Note that this will only track memory allocated through new-operators. You can expand this, let's call it 'new-delete-tracker' almost infinately but will never achieve anything near what the OS is providing.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question