Reputation: 71
I am writting a program that use vector
like vector<Myclass* >
The length of this vector is about 1000, and I use a for loop to keep pushing about 200 new object pointers into the vector and pick 100 of them to erase use the erase() function.
And for each object erased I use delete to free the memory. And in the last line of destructor I use sizeof(this) to check whether the memory has been released successfully. It always return 0.
But I use top command on ubuntu to check the usage of the memory for this process. About 3 400 iterations, it will use 89% of memory for mey 2GB memory lab top. I guess this is because the released memory from the Myclass object does not been reused some how and the vector keep asking memory from the OS.
Anybody has any idea about this?
Upvotes: 1
Views: 1780
Reputation: 263128
And in the last line of destructor I use sizeof(this) to check whether the memory has been released successfully. It always return 0.
Since this
is a pointer, sizeof(this)
is evaluated (at compile time, as always) to the size of a pointer, which is typically 4 or 8 on today's systems. Also, sizeof(something)
will never return 0.
If you post some of your code, maybe we can give better advice.
Upvotes: 0
Reputation: 343
What about using std::shared_ptr<object>
instead of regular pointers ?
It sounds like you might have a memory leak.
Upvotes: 2
Reputation: 943
A vector won't use more than 1.5x-2x more memory than required (I think most implementations are 1.5x, but that's probably not standard). Also, since pointers are only 4 to 8 bytes, a vector of pointers generally won't take much space, at least until you get into millions of items.
I'm confused why you're adding 200 objects and removing 100 objects each loop. Unless I'm misunderstanding you, the number of objects on your heap will continue growing indefinitely. After 3400 iterations, each adding a net 100 objects, you'll have 300,000 objects, which could easily take 2 GBs for a nontrivial class, even if you don't have a memory leak (although that also sounds likely).
Upvotes: 1
Reputation: 5823
without seeing the code, all i can suggest is to use either
std::vector<object>
instead of std::vector<object*>
or
boost::ptr_vector
Upvotes: 0
Reputation: 47762
And in the last line of destructor I use sizeof(this) to check whether the memory has been released successfully.
sizeof(this)
? Not sure what you actually wanted to know, but sizeof
is a compile-time construct and can never tell you that something happened at runtime.
But I use top command on ubuntu to check the usage of the memory for this process. About 3 400 iterations, it will use 89% of memory for mey 2GB memory lab top. I guess this is because the released memory from the Myclass object does not been reused some how
Probably, you have a memory leak. This can be caused eg. by an error in your destructor code.
Try running your program under valgrind with --leak-check=full
.
Upvotes: 2
Reputation: 54242
And in the last line of destructor I use sizeof(this) to check whether the memory has been released successfully. It always return 0.
I don't think that's doing what you think it's doing.
But I use top command on ubuntu to check the usage of the memory for this process. About 3 400 iterations, it will use 89% of memory for mey 2GB memory lab top. I guess this is because the released memory from the Myclass object does not been reused some how and the vector keep asking memory from the OS.
Sounds like you have a memory leak. I suspect you're not deleting the memory like you think you are, but we'll need to see your code. Is there any reason your vector has pointers in it and not just the objects themselves?
Upvotes: 0