1

There is a simple C++ class

class LASet
{
   public:
     long int  maxValue, minValue;
     int _count;
     set <long int>  _mySet;
     LASet()
     {        
       maxValue = 0;
       _count = 0;
       minValue =std::numeric_limits<long int>::max();
       _mySet.clear();
     }
     void Add(long int value)
     {                              
        if (_mySet.find(value) != _mySet.end())
          return;
        if (value > maxValue)
            maxValue = value;
        if (value < minValue)
            minValue = value;
        _mySet.insert(value);
        _count++;
     }
     // some helper functions....   
};

As I instantiate some objects from this class, I want to find their sizes during the runtime. So, I simply wrote the sizeof after many cycles of the execution for each object. For example:

LASet LBAStartSet;
...
...
cout << "sizeof LBAStartSet = " << sizeof( LBAStartSet ) << '\n';

That cout line reports only 72 for all objects means 72B, but top command shows 15GB of memory.

I read the manual at cppreference that sizeof doesn't work on some incomplete data types. Is my data type incomplete?

Any feedback is appreciated.

mahmood
  • 23,197
  • 49
  • 147
  • 242
  • 2
    `sizeof` doesn't do what you think it does... re-read that manual page. Your type is not incomplete – M.M Jun 02 '16 at 08:30
  • To help your understanding, remember that `sizeof` is resolved at compile-time – M.M Jun 02 '16 at 08:31
  • sizeof() is a compile time operator. Your type is complete. As i can see from your code you have used "set" and if i am not wrong you are adding data to this set at run time. Sizeof() wont return you size at run time, it will give size of your class by calculating the data member types at compile time. top will show you the actual runtime memory usage(resident memory). – sagar Jun 02 '16 at 08:31
  • I want to monitor the growth of the data size during the execution. Is there any way to find that? – mahmood Jun 02 '16 at 08:32
  • 1
    The `sizeof` operator only returns the static size of the object not the dynamic memory that member variables may have allocated from the free store. – Galik Jun 02 '16 at 08:32
  • 1
    Have a look at this link:http://stackoverflow.com/questions/4690800/how-to-profile-memory-usage – sagar Jun 02 '16 at 08:35
  • It doesn't work on *any* incomplete data types, but that's not the problem here. – user207421 Jun 02 '16 at 08:46

2 Answers2

2

As I instantiate some objects from this class, I want to find their sizes during the runtime.

You seem to assume that the size of an object can change at run time. That's not possible. The size of an object is the same at compile time, as it is through the entire runtime. It never changes after compilation.

Is my data type incomplete?

No. If it were, then your program would be ill-formed and the compiler would likely refuse to compile. A type is incomplete if it is only declared, but not defined. You've defined LASet, so it is complete.

You seem to also assume, that elements within std::set (I assume that's what you use) increase the size of the set object. They don't. They can't, because the size always remains and has to remain the same. Instead, the objects are stored outside the object, in the so called dynamic storage.

So, the memory used by your program is roughly:

  sizeof LBAStartSet
+ LBAStartSet._mySet.size() * (sizeof(long) + overhead_of_set_node + padding)
+ overhead_of_dynamic_allocation
+ static_objects

Static objects include things like std::cout. Overhead of dynamic allocation depends on implementation, but it could be O(n) in the number of dynamically allocated objects.

I want to monitor the growth of the data size during the execution. Is there any way to find that?

Not in standard c++. But there are OS specific ways. In Linux for example, there is the /proc pseudo-filesystem and you may be interested in contents of /proc/self/status

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

I think the problem is in the member _myset. It is an object, and sizeof(LASet) does not increase when you insert values in _myset. It's likely that sizeof(LAset) is evaluated at compile-time.

You could use set::size to find out how many elements are in a set. If you do that for all instances of LASet you could get rough approximation of the needed memory.

Danny
  • 39
  • 2