Jump to content

  • Log In with Google      Sign In   
  • Create Account

c++: memory deallocation (and freeing) not working?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 03 July 2012 - 05:17 AM

I use std::vector(s) as containers for structs, where I store my data.
Just to make a quick example, I've something like this:

struct myFolliclesLib  {
  QString follicleGroupName;
  std::vector<myData::myDataLib> myDataIDs;
} ;

Later I store data into the container:

std::vector<myFollicles::myFolliclesLib> myFollicle;
myFollicle.push_back(myFollicles::myFolliclesLib());
...
myFollicle.at(n).follicleGroupName="something";
myFollicle.at(n).myDataIDs.push_back(someData);

Everything works ok, data is stored, I perform queries etc. I've a problem only with deleting and freeing memory.

I thought (after googling around) that deleting the container would just delete also all the data stored inside the structs:

myFollicle.clear();
myFollicle.erase(myFollicle.begin(), myFollicle.end());

Instead, from the Task Manager (I've also another application called Process Explorer that shows memory usage more in detail) the memory is not freed.

So I was wondering wether I need to delete also all the data contained into the structs (and so each vector, array etc.) ?

Edited by Alessandro, 03 July 2012 - 05:19 AM.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8885

Like
0Likes
Like

Posted 03 July 2012 - 05:34 AM

Instead, from the Task Manager (I've also another application called Process Explorer that shows memory usage more in detail) the memory is not freed.

How much memory are you actually allocating? Memory is allocated in rather large pages, so freeing juste a few kB of memory may not actually cause any page to get deallocated.

It is my understanding that vectors will only free memory allocated to storing the structs themselves. If the structs contain any reference to other data (such as pointers to objects), the data will still be there - only the memory allocated to storing the reference will have been freed. So if you have anything other than primitive types (like ints, etc...) in your struct, you need to explicitly free the objects they point to since they are actually references.

Edited by Bacterius, 03 July 2012 - 05:39 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 Zoomulator   Members   -  Reputation: 273

Like
1Likes
Like

Posted 03 July 2012 - 05:43 AM

Vectors keep their claim to the memory they've allocated, even after you've cleared it's content. It destructs all the elements, but it does not deallocate them. This is a precaution for when you re-use the vector. If it's grown to a certain size once, it's a good chance it will again. That's how they reason anyway..

If you really want to get rid of the allocated memory, replace the vector with a new empty one to force it. C++11 has included the vector.shrink_to_fit() method, but it's a request that may not be fulfilled by the implementation.

myFollicle = std::vector<myFollicles::myFolliclesLib>();

You only need to explicitly free raw pointers! Never call the destructor or a delete on an object in a container. Vector, when clearing, removing or erasing, will call the appropriate destructor (not delete) on the objects it contains. If this is a raw pointer, the actual object it points to will not be deleted unless you call delete explicitly. It's however recommended to use a unique_ptr in this case rather than a raw pointer.

Edited by Zoomulator, 03 July 2012 - 05:54 AM.


#4 Bregma   Crossbones+   -  Reputation: 5133

Like
0Likes
Like

Posted 03 July 2012 - 06:14 AM

Just to be clear: the containers in the C++ standard library by default use a "pooled allocation strategy" for their memory, which means they do not relinquish memory they have allocated but instead return it to a pool for future use. The std::vector, in particular, does not shrink when elements are removed.

The standard library containers use copy semantics. That means that when you insert an object into the container, a new copy of the object is created, and when you remove it from the container, that copy is destroyed. If the container holds a pointer object, the pointer is destroyed, not the object it was pointing to (unless the pointer object is a smart pointer object and the conditions are appropriate).

Memory allocation in the C runtime is not as simple as most beginners seem to believe. Aside from the pooled allocation strategy used by the C++ standard library containers, the runtime itself uses a pooled allocation strategy in which the C library calls (malloc, etc) request large chunks of memory from the underlying OS an doles it out from its arean in order to satisfy requests. It is very rare that such memory is ever returned to the underlying OS. Instead, when freed, it just gest reused again, only growing as needed. The default C++ ::operator new is written 'as if' it uses malloc(), so it, too uses a memory allocation pool.

Using a crude tool like the Task Manager to watch how your application uses memory is more likely to mislead you than to inform you except in very blatantly obvious cases.

I suspect that in the OP's case, everything is in fact working correctly.
Stephen M. Webb
Professional Free Software Developer

#5 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 03 July 2012 - 09:44 AM

Thanks for your answers.
The memory allocated is about 400MB, so that means that each time that I run the routine that allocates data, "costs" about 400MB of memory.
I know that Task Manager is misleading but then I can one be sure that memory is freed?
From one of your suggestions (Zoomulator) it seems that rather using .clear() and .erase(), I should just a new empty vector to do the job. Just did that but again the Task Manager still adds to memory usage.
So I'm a bit confused. Is calling a new empty vector really freeing memory? Can't use shrink_to_fit as it is unsupported in XCode.

#6 ApochPiQ   Moderators   -  Reputation: 15765

Like
0Likes
Like

Posted 03 July 2012 - 12:37 PM

Task Manager is not a reliable way to see how much memory your process is using. Have you verified that you are in fact leaking memory using a proper leak detection tool, or just Task Manager?

#7 Alessandro   Members   -  Reputation: 296

Like
0Likes
Like

Posted 09 July 2012 - 08:59 AM

Vectors keep their claim to the memory they've allocated, even after you've cleared it's content. It destructs all the elements, but it does not deallocate them. This is a precaution for when you re-use the vector. If it's grown to a certain size once, it's a good chance it will again. That's how they reason anyway..

If you really want to get rid of the allocated memory, replace the vector with a new empty one to force it. C++11 has included the vector.shrink_to_fit() method, but it's a request that may not be fulfilled by the implementation.

myFollicle = std::vector<myFollicles::myFolliclesLib>();

You only need to explicitly free raw pointers! Never call the destructor or a delete on an object in a container. Vector, when clearing, removing or erasing, will call the appropriate destructor (not delete) on the objects it contains. If this is a raw pointer, the actual object it points to will not be deleted unless you call delete explicitly. It's however recommended to use a unique_ptr in this case rather than a raw pointer.


Hi, thanks for this suggestion, searching around it seems it's the common way to do it.
But how'd you use it in a situation where you want to deallocate a vector inside another container vector?

See this example below:

// this is the struct that contains a vector with some data
 struct myShaveGroupsLib {
        QString shaveGroupName;
        std::vector<myFollicles::myFolliclesLib> myFollicle;
}

std::vector<myShaveGroups::myShaveGroupsLib> myShaveGroup; // a vector that points to the above struct

for (int g=0; g<MAX_G; g++)
    {
        myShaveGroup.at(currentShaveGroup).myFollicle.push_back(myFollicles::myFolliclesLib());    // fill vectors with data
    }

Now, from what I understood I might simply call the following line to deallocate the memory held by the container (myShaveGroup) and all the data inside it (and so also the struct data):

std::vector<myShaveGroups::myShaveGroupsLib> myShaveGroup;

But what if I'd like just to deallocate the memory held by the 'myFollicle' vector? Is that possible somehow?

I've learned a little bit of C++ by myself and I find memory allocation/deallocation so hard to deal with.
Thanks for any help!

#8 Aardvajk   Crossbones+   -  Reputation: 5982

Like
1Likes
Like

Posted 09 July 2012 - 01:54 PM

But what if I'd like just to deallocate the memory held by the 'myFollicle' vector? Is that possible somehow?

I've learned a little bit of C++ by myself and I find memory allocation/deallocation so hard to deal with.
Thanks for any help!


std::vector<myFollicles::myFolliclesLib>().swap(myShaveGroupsLib.myFollicle);

Ideomatic way to deallocate a vector, swaps the internal contents with a temporary which then immediately goes out of scope at end of expression, freeing the contents.

Convince yourself with proper profiling that you need this sort of stuff before you waste time on it though. Standard library is good at general use of memory stuff. Normally only specific domains require special handling.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS