Re: Slow manual 'garbage collection' in C++ ??
On Jun 17, 9:46 pm, brey_maastri...@hotmail.com wrote:
Hello guys,
I really need your help. I'm a Java programmer and right now I'm
experiencing a huge problem with C++.
In a scientific computation I need to store a lot of Point objects
(which have an array of doubles and 2 ints as fields).
To quickly search among all these points I have designed a Hashtable
to store them; a bucket/vector of buckets/vectors.
The hashtable is filled with Points objects in a while loop.
This whole procedure again is repeated a lot of times in a for loop.
At the end of every run of this for loop the Hashtable has to be
emptied in order to prevent for memroy leaks: this means looping
through every vector in the main vector of the hashtable and delete
every Point object manually.
Later on, this C++ computation will be compiled under Matlab to use it
over there.
But now the problem is:
My C++ programm is very fast without emptying the hashtable. But then
I have a huge memory leak; and my swapfile grows enormouly.
With emptying the hashtable my C++ programm becomes dramatically
slow.....even slower than my Java programm.
So the question is: What to do now ?? It cannot be the case that
preventing a memory leak, slows down the computations ??
Kind regards,
Brey
You don't need to write your own hash.
Try using STL. I think an STL 'multiset' can store objects in an RB-
tree fashion. When the multiset goes out of scope it automatically
frees all the memory.
You will need to overload the '<' operator to allow the multiset
comare the nodes in the RB tree.
Here is an example:
#include <set>
#include <iostream>
using namespace std;
// define the Point struct
struct Point {
double ar[10];
int a, b;
Point(int x, int y) {
a = x;
b = y;
}
};
// define the '<' operator, this definition relates to the 'a' field
bool operator < (const Point &p1, const Point &p2) {
return p1.a < p2.a;
}
int main()
{
multiset<Point> points;
// insert some points
for (int i = 0; i < 10; i++) {
cout << "Inserting point with a = " << 10 - i << endl;
points.insert(Point(10 - i, 0));
points.insert(Point(10 - i, 0)); // add each item twice..., just to
see this works
}
cout << endl;
// now scan the set
multiset<Point>::iterator it;
for (it = points.begin(); it != points.end(); it++) {
cout << "Reading point: a = " << it->a << ", b = " << it->b << endl;
}
return 0;
}
The output is:
Inserting point with a = 10
Inserting point with a = 9
Inserting point with a = 8
Inserting point with a = 7
Inserting point with a = 6
Inserting point with a = 5
Inserting point with a = 4
Inserting point with a = 3
Inserting point with a = 2
Inserting point with a = 1
Reading point: a = 1, b = 0
Reading point: a = 1, b = 0
Reading point: a = 2, b = 0
Reading point: a = 2, b = 0
Reading point: a = 3, b = 0
Reading point: a = 3, b = 0
Reading point: a = 4, b = 0
Reading point: a = 4, b = 0
Reading point: a = 5, b = 0
Reading point: a = 5, b = 0
Reading point: a = 6, b = 0
Reading point: a = 6, b = 0
Reading point: a = 7, b = 0
Reading point: a = 7, b = 0
Reading point: a = 8, b = 0
Reading point: a = 8, b = 0
Reading point: a = 9, b = 0
Reading point: a = 9, b = 0
Reading point: a = 10, b = 0
Reading point: a = 10, b = 0
See how the numbers are ordered from least to most, although inserted
from most to least.