Reputation: 24785
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.
Upvotes: 1
Views: 86
Reputation: 238421
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
Upvotes: 2
Reputation: 39
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.
Upvotes: 0