C++ Not Freeing Memory

Hello,

I am confused on why my program will not release its memory back to the OS. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>

template <class C>
void delVector(std::vector<C> &vec)
{
	/* an empty vector */
	std::vector<C> empty;

	/* call the destructor of the vector elements */
	for (int a = 0; a < vec.size(); a++)
	{
		delete vec[a];
	}

	/* swap the contents for an empty vector */
	vec.swap(empty);
}

int main(void)
{
	/* memory test location one */
	std::cin.get();

	/* our test vector */
	std::vector<float*> test;

	/* give our vector some elements */
	for (int a = 0; a < 8192; a++)
	{
		test.push_back(new float(a));
	}

	/* memory test location two */
	std::cin.get();

	/* release our vector */
	delVector(test);

	/* memory test location three */
	std::cin.get();

	return 0;
}


I'm using one of the various methods that I found online, because it seemed to have the best results. I'm measuring the memory usage at those certain points in the program that the program uses in Task Manager. These are the results that I am getting:

1
2
3
At Location One: 720kb (roughly) 
At Location Two: 912kb (roughly)
At Location Three: 812kb (roughly)


Shouldn't the memory usage at location one and three be the same since I cleaned up the vector? Any help would be appreciated!

With even worse results, I've also tried:
1
2
test.clear();
test.shrink_to_fit();


Thanks,
MaxterTheTurtle
Last edited on
Well, the issue has nothing to do with your code. The issue is that your vector still exists. Even an empty vector still allocates memory for the potential to fill- it is a dynamic array, after all. So there is still some memory dedicated for the first spot, even though there is none. It is just prepared to fill it, in case you decide to. After all, you are simply emptying it- you aren't deleting it outright.
Thanks for the reply. I tried what you said and I have re-written my code to also delete the test vector as well, but I'm still ending up with some used memory. Though, when I test with crtDumpMemoryLeaks it returns 0. Is it possible that I am not deleting things properly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <crtdbg.h>
#include <iostream>
#include <vector>

template <class C>
void delVector(std::vector<C> *vec)
{
	/* an empty vector */
	std::vector<C> empty;

	/* call the destructor of the vector elements */
	for (int a = 0; a < vec->size(); a++)
	{
		delete (*vec)[a];
	}

	/* swap the contents for an empty vector */
	vec->swap(empty);
	
	vec->clear();
	vec->shrink_to_fit();
}

int main(void)
{
	_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG );
	_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );

	/* memory test location one */
	std::cin.get();

	/* our test vector */
	std::vector<float*> *test = new std::vector<float*>;

	/* give our vector some elements */
	for (int a = 0; a < 8192; a++)
	{
		test->push_back(new float(a));
	}

	/* memory test location two */
	std::cin.get();
	
	delVector(test); delete test;
	
	/* check for memory leaks */
	std::cout << _CrtDumpMemoryLeaks();

	/* memory test location three */
	std::cin.get();
	
	return 0;
}
I try your code and seems to have the same problem
I experimented with raw array
then back to malloc
and still experiencing the same problem
I play with the size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

int main(void)
{
cin.get();

    const int size = 200000;
    float* p[size];


cin.get();
    for( int x = 5; x--; ){

    cout << "Allocating " << size * sizeof( float ) << " after pause "<< endl;
    cin.get();

        for( int i = 0; i < size; ++ i ){
            p[i] = (float*) malloc( sizeof( float ) );
        }

    cout << "freeing it after pause\n";
    cin.get();

        for( int i = 0; i < size; ++ i ){
            free( p[i] );
        }
    }

cin.get();
	return 0;
}


I try to loop like the code above
allocate then deallocate
the allocate again and so forth
there is increase in memory everytime but it's just as much as 4KB or more

I found that the size of your allocation matters
and my conclusion
it's seems that C++ compiler or the OS is so smart that it left behind some memory just in case the next allocation to be a big one too...
Topic archived. No new replies allowed.