This has been bothering me for a while now. When I try to clear a vector it just *pretends* to clear it, and the data is still in memory. This led to some obscene memory leaks, so I dug around looking for a way to really clear out a vector, and I found this:

vector<int> testVector;

//  ...
//  Fill testVector up with a whole bunch of ints.

//  Try to clear it.
testVector.clear();

//  Now swap with a new vector to *really* clear it.
vector<int>().swap(testVector);

This does work, but memory still leaks. There is an extra 56 or so bytes after the swap. So is there something that I'm missing here, or is there a better way to do this? How do I really clean out a vector so that I end up with the same amount of memory that I started with?

Sheesh, this is painful..

-Fredric

Recommended Answers

All 8 Replies

what makes you think clear() is not releasing all the memory? The way you are using swap() probably desn't work either (my guess) because there is no real vector.

Try this simple example -- there will be no output because after clear() the vector is empty.

int main()
{
	vector<int> theList;
	vector<int> anotherList;
	theList.push_back(1);
	theList.push_back(2);
	theList.clear();
	anotherList.push_back(3);
	anotherList.push_back(4);
	anotherList.swap(theList);

	for(int i = 0; i < anotherList.size(); i++)
		cout << anotherList[i] << endl;
	return 0;
}

Are those ints in the vector dynamically allocated? why on earth do you think you are leaking memory?

This has been bothering me for a while now. When I try to clear a vector it just *pretends* to clear it, and the data is still in memory. This led to some obscene memory leaks, so I dug around looking for a way to really clear out a vector, and I found this:

vector<int> testVector;

//  ...
//  Fill testVector up with a whole bunch of ints.

//  Try to clear it.
testVector.clear();

//  Now swap with a new vector to *really* clear it.
vector<int>().swap(testVector);

This does work, but memory still leaks. There is an extra 56 or so bytes after the swap. So is there something that I'm missing here, or is there a better way to do this? How do I really clean out a vector so that I end up with the same amount of memory that I started with?

Sheesh, this is painful..

-Fredric

I'm pretty much sure that code will not produce memory leak. In fact I tested it with _CrtDumpMemoryLeaks() function in my MSVC 7.0 and it's OK. There is no memory leak

- Micko

Here is my complete main.cpp file...that might help.

#include <stdio.h>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
	vector<int> test;
	char buffer[256];
	int i;

	gets(buffer);  // Wait for user input...

	printf("Filling...\n");
	for (i=0; i<10000; i++) {
		test.push_back(i);
	}
	printf("Filled...\n");

	gets(buffer);  // Wait for user input...

	test.clear();
	vector<int>().swap(test);


	printf("Emptied...\n");

	gets(buffer);  // Wait for user input...


	return 0;
}

I am checking this with the Task Manager, and noting the amount of memory usage for the entire program at each gets(). The program starts at 604 KB. I press enter and it fills the vector. The Task Manager tells me that the program is using 728 KB. I press entire again. The vector is cleared and swapped. The Task Manager shows that the program is using 660 KB of memory.

If I don't do the swap, then it stays at 728 KB until I close the program completely. I am currently using MSVC++ 6.0, which wouldn't surprise me to be the cause of the problem. Strange... Thanks for the replies.

-Fredric

EDIT: OK, so maybe this isn't a memory leaking problem, it's more of a problem of making the vector class go back to using the same amount of memory as it did before I added data to it. Sorry for mislabeling..

each program has its own memory manager, so when you call free() or delete the memory manager may hold onto the memory in anticipation that you will want to allocat it again. All TaskManager is reporting is the total amount of memory owned by the application -- it has no clue what the program's memory manager is doing with it.

when you call clear() the vector class is releasing all the memory, but that memory is being kept by the program's memory manager, as indicated above. It may or may not be possible for the program to give all the unallocated memory back to the os -- depends on how fragmented the memory bock becomes. memory managers don't normally go to the os on every allocation request -- it first checks its own lists to see if it has an unallocated block big enough to fill the request. If it does, then it will return a pointer to that block, but if not it will request more memory from the os, add it to its list and return a pointer back to your program to that block.

Ah, I think I see what's going on now. It seems like the memory is being held on by the vector class itself, and not the memory manager when I call clear (which frees *no* memory, based on my observations with the task manager), and that when you do the swap() it forces that memory to be freed up from the vector. Then the memory goes to the program's Memory Manager, and it decides to keep that 56 KB that I'm seeing for whatever reason. So I guess that means I should really only be doing vector<int>().swap() if I am in a situation where I need to let go of that memory immediately, and not have it sit around waiting for another vector to fill its place...

Thanks for clearing that up. :)

-Fredric

It makes me most curious that you use this given the topic of your post.

gets(buffer);

Bad habits, I tell ya! :o

-Fredric

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.