Solved

Want to determine how much total memory my code uses?

Posted on 2003-11-18
12
308 Views
Last Modified: 2010-04-02
Hello there experts
(you guys are great!)

I want to automate a way to determine how much total memory my program uses up?

I'm using windows 2000 right now, programming with C++ and the Visualization ToolKit library  (VTK)

Is there a way to automate this..? Not sure how to go about this?  
=s
thanks for any input!!


0
Comment
Question by:lost_bits1110
[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
  • 6
  • 6
12 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9771991
Call 'GlobalMemoryStatus()' and evaluate the 'dwTotalVirtual' member of the MEMORYSTATUS member:

dwTotalVirtual
Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.

e.g.

MEMORYSTATUS ms;

GlobalMemoryStatus ( &ms);

DWORD dwMemUsage = ms.dwTotalVirtual;
0
 

Author Comment

by:lost_bits1110
ID: 9772971
Thats great..! Thanks for the response..
But I dont know what you mean by "user mode portion of the virtual address space of the calling process"..?

Lets say I have the following program:

int main()
{
-------something
-------something
cout<<ms.dwTotalVirtual;
-------something
}

so does this output the number of bytes used upto that point..??
(Sorry for the dumbness of this question.. =S)
Really appreciate your help,
thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 9772995
>>so does this output the number of bytes used upto that point..??

Short answer: This is the memory footprint of your program.
0
Independent Software Vendors: 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!

 

Author Comment

by:lost_bits1110
ID: 9773026
Okay - so basically no matter where I get the value  of 'ms.dwTotalVirtual', it will return the total memory usage of just the code I'm running in bytes?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9773028
Addition - this is the same value that teh task manager shows in the 'Virtual Memory' column for each process.
0
 

Author Comment

by:lost_bits1110
ID: 9773069
okay thanks, so basically no matter where I get the value  of 'ms.dwTotalVirtual', it will return the virtual memory that my program is using?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9773112
Yes :o)
0
 

Author Comment

by:lost_bits1110
ID: 9773132
Also, I just tried this and its telling me my code is running 2 Gigs of memory..!???? This doesnt make any sense.. this is the number i get:
 2147352576
0
 
LVL 86

Accepted Solution

by:
jkr earned 130 total points
ID: 9773186
Ooops, sorry *blushing* - you have to subtract the available bytes, so


cout<<ms.dwTotalVirtual-ms.dwAvailVirtual;

0
 

Author Comment

by:lost_bits1110
ID: 9773351
okay thanks.. i now get about 66293760 which is about 65 MB..? I wonder if this makes sense because I'm using the vtk graphics library, and i input a file that is about 8 Megs and my code is not that long really, and i've allocated a few small int arrays here and there etc..
So 65 MB still seems much? Or does this make sense??

so now if i want to get the value in kB lets say, I divided (ms.dwTotalVirtual-ms.dwAvailVirtual)/(2^10) but I get an incorrect value of: 8286720  when it really should be 64,740..?

Maybe its to do with the fact that its DWORD?
0
 

Author Comment

by:lost_bits1110
ID: 9773398
ok nevermind it works if i divide by 1024..

but do u think 64 Megs could make sense?  My biggest array has 76906 elements, theres an 8Meg file input, and a few ohter small integer arrays.. and i'm using a graphics library..
0
 
LVL 86

Expert Comment

by:jkr
ID: 9773630
>>but do u think 64 Megs could make sense?

Yes. Remember, it is not only your code, but also all system DLLs you are using that are being mapped into your program's address space.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 128
How to convert MFC APP to Win32 APP. 19 133
I could not build boost code, 10 116
c++ reading data from file into two dimensional array 3 148
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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