0

How would I determine the total amount of memory an object is using, and what percentage of that memory currently exists on the stack? What about the heap as well?
For example, given this program:

#include <cstdlib> #include <vector> #include <string> int main(){ //I wonder how much memory is being //used on the stack/heap right now. std::vector<std::string> vec{"11","22","33"}; //how about now? return EXIT_SUCCESS; } 

how can I view the size of the stack and heap before and after the creation of the vector?
Is this possible to do this with GDB?
The manual provides some information on examining memory, but I haven't been able to report such information.

3
  • 1
    sizeof vec will be the stack size it takes since you allocated it on the stack. The free store size should be something like vec.capacity() * sizeof(std::string). Commented Nov 28, 2012 at 22:59
  • For measuring heap usage, you might have a look at valgrind's massif tool: valgrind.org/docs/manual/ms-manual.html Commented Nov 28, 2012 at 23:11
  • 1
    Regarding heap, you may want to check out this gdb extension: fedorahosted.org/gdb-heap Commented Nov 29, 2012 at 20:38

1 Answer 1

2

If you're prepared to use GLIBC specific functions you can use mallinfo() directly within your program to answer the question:

#include <cstdlib> #include <vector> #include <string> #include <iostream> #include <malloc.h> int main(){ std::cout << "Using: " << mallinfo().uordblks << "\n"; std::vector<std::string> vec{"11","22","33"}; std::cout << "Using: " << mallinfo().uordblks << "\n"; return EXIT_SUCCESS; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.