Solved

Want to determine how much total memory my code uses?

Posted on 2003-11-18
12
309 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

623 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