Solved

Want to determine how much total memory my code uses?

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

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now