hello guyz..
Need urgent help on this !!!

Is there a way to find out the total memory being consumed by my C++ program. I have seen in many online coding competitions that set a limit on memory consumption. Is there any tool by which I can find memory being consumed by my program.

Kindly provide any suitable method in Linux in vi editor or in Windows.
I use Dev C++ as an editor in Windows. So if there is any suitable tool in the editor itself please let me know...

unsigned int UI_MemoryUsed = GetMemoryTotalPhys();

Hope this helps. It works for me. I use Borland builder c++ Enterprise edition IDE.

Let Me Know

Thanks..but I am still a bit confused
how do you use it..

i mean can you please give a short code as an example..
what header files are required..

//------------------------------------------------------------------------------
//This function is to calculate the Ram memory 
//------------------------------------------------------------------------------
float TFrm_Main::GetMemoryTotalPhys()  
{
    TMemoryStatus *ms = new TMemoryStatus(); //this is in borland

    GlobalMemoryStatus(ms);
    int dw_Len = ms->dwLength;
    int dw_memLoad = ms->dwMemoryLoad;
    unsigned long dw_TotPhyMem = ((ms->dwTotalPhys/1024.0)); //1024 is used to convert byte to kilobyte here
    unsigned long dw_AvailPhyMem = ((ms->dwAvailPhys/1024.0));
    unsigned long dw_TotPgFile = ((ms->dwTotalPageFile/1024.0));
    unsigned long dw_AvailPgfile = ((ms->dwAvailPageFile/1024.0));
    unsigned long dw_TotVirMem = ((ms->dwTotalVirtual/1024.0));
    unsigned long dw_AvailVirMem = ((ms->dwAvailVirtual/1024.0));
    unsigned long tot_Commit = (dw_TotPgFile/1024.0);
    unsigned long use_Commit = ((dw_TotPgFile-dw_AvailPgfile)/1024.0);
    delete ms;

    return((use_Commit/(float)tot_Commit)*100.0);
}

Sorry for the goof up. Blame it on multi tasking.

use winbase.h header file in the code

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

Hello..thnx for replying

i am afraid but The goof up is really confusing :)

I shall give you an example as to What am I really lookin for :

Here's an example of a simple C++ code

int main()
{
 int a;
 int  b,c;
 cin>>a>>b;
c=a*b;
}

What i really want is a method to know how much memory the above program consumes...

The above is a very simple code..only that my actual code consists of a whole lot of variables that use queues and trees.

I am sure you would have come across programming competitions where a limit is set on memory requirements.In such contests, submitting a code reveals the amount of memory the program has taken up in order to run.

I am lookin for a similar way to find out as to how much memory my program is utilizing...


//------------------------------------------------------------------------------
//This function is to calculate the Ram memory
//------------------------------------------------------------------------------
float TFrm_Main::GetMemoryTotalPhys()
{
TMemoryStatus *ms = new TMemoryStatus(); //this is in borland

GlobalMemoryStatus(ms);
int dw_Len = ms->dwLength;
int dw_memLoad = ms->dwMemoryLoad;
unsigned long dw_TotPhyMem = ((ms->dwTotalPhys/1024.0)); //1024 is used to convert byte to kilobyte here
unsigned long dw_AvailPhyMem = ((ms->dwAvailPhys/1024.0));
unsigned long dw_TotPgFile = ((ms->dwTotalPageFile/1024.0));
unsigned long dw_AvailPgfile = ((ms->dwAvailPageFile/1024.0));
unsigned long dw_TotVirMem = ((ms->dwTotalVirtual/1024.0));
unsigned long dw_AvailVirMem = ((ms->dwAvailVirtual/1024.0));
unsigned long tot_Commit = (dw_TotPgFile/1024.0);
unsigned long use_Commit = ((dw_TotPgFile-dw_AvailPgfile)/1024.0);
delete ms;

return((use_Commit/(float)tot_Commit)*100.0);
}


Sorry for the goof up. Blame it on multi tasking.

use winbase.h header file in the code

cin>>a>>b;
Call the function here initially and store it in a variable
c=a*b;
Call the function again here and store in another variable

hope this helps!

My Code:
i have a timer in my code which keeps calling this function so that based on the memory i decide whether certain section of the code should be executed.Its a mammoth code so i cant paste a snippet of the code

Thanks a lot..
with utmost respect, I feel either I am unable to convey my point to you..or you are misunderstanding it..

Placing the function after every step as you mentioned above would be very cumbersome, since my code is roughly 400 lines.

Ok..let me put it this way..
I have a program -> program.cpp

now i just need to know the memory consumption of this program,
I just need a utility tool in linux or a tool in windows to know memory use.
Even without knowing the details of the program, i need to just know how much memory is it taking..

like in order to find the time required for a program to get an output, we have a utility in linux.

cin>>a>>b;
Call the function here initially and store it in a variable
c=a*b;
Call the function again here and store in another variable

hope this helps!

My Code:
i have a timer in my code which keeps calling this function so that based on the memory i decide whether certain section of the code should be executed.Its a mammoth code so i cant paste a snippet of the code

i am not telling to place after every line. put in where u require thats all.

ok if you need a tool in windows right click on the bottom task bar in that a list of options will appear click on Task Manager.

Click on Processes. See the list of Image Names and look for your application or program. Under Mem Usage you can see with respect to your application the memory used in Kilobytes.


If this is not the requirement. I apologize for wasting your time.

Thanx a lot, ranjita...
i will let u know on gettin a satisfactory answer..
apologies not required... !!! :)

i am not telling to place after every line. put in where u require thats all.

ok if you need a tool in windows right click on the bottom task bar in that a list of options will appear click on Task Manager.

Click on Processes. See the list of Image Names and look for your application or program. Under Mem Usage you can see with respect to your application the memory used in Kilobytes.


If this is not the requirement. I apologize for wasting your time.

This article has been dead for over six months. Start a new discussion instead.