Reputation: 205
I have a function which uses 0.5 MB memory each time I run it. So I decided to investigate it step by step by watching the Windows task manager at the same time. I noticed after these lines:
int **banned;
banned=new int*[vertices];
for(i=0;i<vertices;i++)
banned[i]=new int[k_colors];
It uses 0,5 MB memory. Then I decided to delete it before the return
line:
for(i=0;i<vertices;i++)
for(j=0;j<k_colors;j++)
delete []banned[j];
delete[]banned;
It was 8,5 MB memory using beginning of the function. After allocation, it became 9 MB, but after the delete part, it was still 9 MB. And I execute this function in the whole program 1000 times. Then it is getting killed by OS. Any idea why is that and how can I solve it?
EDIT: Here the main()
part:
int main()
{
srand(time(0));
input();
initialize();
for(int i = 0; i < MAX_GENERATION; i++)
{
parents = selection(TS);
population = cross_over(parents, PC);
mutation(PM);
elite=tabu_search(population);
elitism(); //270 MB memory using each time.
}
fclose(pFile);
return 0;
}
Above, in elitism()
function's first line are allocation part, and last lines are delete
part.
Upvotes: 2
Views: 230
Reputation: 129314
When you allocate memory in your application, the C++ runtime will ask the OS for "more memory". Under the assumption that you will allocate more memory again, "if there isn't HUGE amounts of memory freed - hold on to it". In other words, "task manager" and other such tools are not ideal for understanding exactly how much memory is ACTUALLY allocated in your application.
However, delete []
does indeed work in all commercial grade released compiler environments.
The problem in your code is that your are deleting things you didn't allocate.
You call delete []
for each of the vertices
and once for the whole array, matching your new
calls. [Every place you have a new
, there should be exactly the same delete
- same number of times, same pointer]
It is also possible that you are "fragmenting" the memory - for example something like this
size_t s = 100;
for(;;)
{
int *p = new int[s];
...
delete [] p;
s += 10;
}
So, the freed memory is "too small" for the next allocation.
Of course, this whole mess could be avoided by using
vector< vector <int> > banned(vertices);
for(i=0;i<vertices;i++)
banned[i].resize(k_colors);
And now the memory used gets cleaned up automatically.
Upvotes: 1
Reputation: 141534
To use delete[]
properly you should delete the same things that you new'd:
for(i=0;i<vertices;i++)
delete [] banned[i];
delete[] banned;
Your "process getting killed" is probably because your original code caused crazy amounts of undefined behaviour, deleting the same pointer multiple times and so on.
This version may or may not release memory to the operating system; that is a decision made by your compiler/library and the operating system. On some systems, the memory may appear to still be allocated to your process, but the OS will be able to claim it if another process needs it.
If you call the same function over and over it should not accumulate memory though; the previously delete
'd blocks can answer the new
call.
Upvotes: 6