473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get memory usage of an adjacency list / transitive closure?

Hi all,

I'm using the BOOST libraries to compute an adjacency list and its transitive closure. I'm defining them as follows:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. typedef boost::adjacency_list<> AdjacencyList;
  3. AdjacencyList g_transitiveClosure;
  4. AdjacencyList g_adjacencyList;
  5. ...
  6.  
What I'm doing is:
loop start
add edge information to the g_adjacencyList
compute the transitive closure of the adjacency list
loop end

After that I want to compute the total memory consumption in bytes of each of the above data structure.

I'm using the sizeof operator as follows:
Expand|Select|Wrap|Line Numbers
  1. boost::graph_traits<AdjacencyList>::vertex_iterator i, end;
  2.     boost::graph_traits<AdjacencyList>::adjacency_iterator ai, a_end;
  3.  
  4.     unsigned int _memAL = sizeof(g_adjacencyList);
  5.  
  6.     for (boost::tie(i, end) = vertices(g_adjacencyList); i != end; ++i)
  7.     {
  8.         _memAL += sizeof(*i);
  9.         tie(ai, a_end) = adjacent_vertices(*i, g_adjacencyList);
  10.         for (; ai != a_end; ++ai)
  11.             _memAL += sizeof(*ai);
  12.     }
  13.  
  14.     unsigned int _memTC = sizeof(g_transitiveClosure);
  15.  
  16.     for (boost::tie(i, end) = vertices(g_transitiveClosure); i != end; ++i)
  17.     {
  18.         _memTC += sizeof(*i);
  19.         tie(ai, a_end) = adjacent_vertices(*i, g_transitiveClosure);
  20.         for (; ai != a_end; ++ai)
  21.             _memTC += sizeof(*ai);
  22.     }
  23.  
The results I get, put me in question that I' doing something wrong.

The bottom line question is that I would like to know if the above method to get the total memory consumption of each data structure in bytes is correct or not.

Thanks in advance.
May 12 '11 #1

✓ answered by Banfa

It is both correct and not correct at the same time (helpful huh :D).

You need to understand what the size of operator does, it returns the size of the type or the type of the variable that is passed to it. This is calculated at compile time.

As such the number returned to you will be correct however you have to take account of what it doesn't calculate for a class. A class is a compound type made of many member variables, if any of these variables are pointers then one might expect that they point to some allocated data. Logically you might consider that data part of the object but physically the memory for that data is not part of the memory for that type. The size of operator will return the size of the pointer.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Test
  6. {
  7. public:
  8.   Test()
  9.   {
  10.     pointer = new int [10000];
  11.   }
  12.   ~Test()
  13.   {
  14.     delete[] pointer;
  15.   }
  16.  
  17. private:
  18.   int *pointer;
  19. };
  20.  
  21. int main()
  22. {
  23.   Test test;
  24.  
  25.   cout << sizeof test << endl;
  26.  
  27.   return 0;
  28. }
  29.  
Outputs the size of the type Test which is the size of a pointer (4 on my WinXP 32bit system) not the size of the memory allocated to the pointer.

So back to my original statement of correct and yet not correct, that would really be more accurately stated as correct but not useful. sizeof correctly returns the size of the type you have asked it to but that is not the number you want (and hence not useful), you want the size of the data allocated for the object to use and to get that you may have to add a method to call on the object that calculates it.

1 1698
Banfa
9,065 Expert Mod 8TB
It is both correct and not correct at the same time (helpful huh :D).

You need to understand what the size of operator does, it returns the size of the type or the type of the variable that is passed to it. This is calculated at compile time.

As such the number returned to you will be correct however you have to take account of what it doesn't calculate for a class. A class is a compound type made of many member variables, if any of these variables are pointers then one might expect that they point to some allocated data. Logically you might consider that data part of the object but physically the memory for that data is not part of the memory for that type. The size of operator will return the size of the pointer.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Test
  6. {
  7. public:
  8.   Test()
  9.   {
  10.     pointer = new int [10000];
  11.   }
  12.   ~Test()
  13.   {
  14.     delete[] pointer;
  15.   }
  16.  
  17. private:
  18.   int *pointer;
  19. };
  20.  
  21. int main()
  22. {
  23.   Test test;
  24.  
  25.   cout << sizeof test << endl;
  26.  
  27.   return 0;
  28. }
  29.  
Outputs the size of the type Test which is the size of a pointer (4 on my WinXP 32bit system) not the size of the memory allocated to the pointer.

So back to my original statement of correct and yet not correct, that would really be more accurately stated as correct but not useful. sizeof correctly returns the size of the type you have asked it to but that is not the number you want (and hence not useful), you want the size of the data allocated for the object to use and to get that you may have to add a method to call on the object that calculates it.
May 13 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
0
by: Jef Driesen | last post by:
I need some help to implement the adjacency list representation of a (undirected) graph. The data structure I need is something like the picture on the website...
3
by: Vincenzino | last post by:
Hi, I have some problem in using SQL3 recursive queries on DB2 database system (8.1 and 8.2 UDB). I need to compute the transitive closure of a (possibly) ciclic graph using SQL3 on DB2. I MUST...
9
by: Mikito Harakiri | last post by:
Transitive closure (TC) of a graph is with TransClosedEdges (tail, head) as ( select tail, head from Edges union all select e.tail, ee.head from Edges e, TransClosedEdges ee where e.head =...
4
by: Hermann Maier | last post by:
hi, i need to find out the memory usage of a specific function that i use in my program. this function does some recursive calculations and i want my program to display the amount of memory the...
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
13
by: placid | last post by:
Hi All, Just wondering when i run the following code; for i in range(1000000): print i the memory usage of Python spikes and when the range(..) block finishes execution the memory usage...
1
by: fishscience | last post by:
I have a problem about memory usage. The code is as below: #include <list> int main(int argc, char* argv) { { std::list<void*ptr_list; { for (int i=0; i<9368; i++) {
12
by: Steve | last post by:
I have been studying the Adjacency List Model as a means of achieving a folder structure in a project I am working on. Started with the excellent article by Gijs Van Tulder ...
2
by: masayuki.takagi | last post by:
hi all, i want to measure memory usage of my python process on Mac OSX. i tired resource module, but it doesn't work on OSX. how can i get it ? thnx.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.