Problems with GC, trees and array concatenation
Babele Dunnit
babele.dunnit at gmail.com
Fri Jun 1 09:40:01 PDT 2007
Hello everybody,
I had a HELL of a day to understand what was wrong and to strip it out from my sources, but now I got it. Run this and keep an eye on the Task Manager: your memory will be eaten until death. Please note that:
1) the keys of it all seems to be the "recursive" Individual class (I was playing with trees). Substitute the
class Individual
{
Individual[20] children;
}
with a
class Individual
{
real[20] values;
}
and the GC will work flawlessly.
2) please note that I am using static arrays, but dynamic arrays behave in the same way.
3) If you use array slicing and direct assignments everything is OK. Please try both "loseMemory" and "everythingOk" versions.
4) no problem if you DON'T assign arrays (i.e, neither version is selected). GC works OK.
5) I am playing under Windows XP, DMD version 1.014.
here he cometh da snippet:
----------------------------------------------------------------------------------------------------------
import std.stdio;
class Individual
{
Individual[20] children;
}
class Population
{
void grow()
{
foreach(inout individual; individuals)
{
individual = new Individual;
}
}
Individual[20000] individuals;
}
version = loseMemory;
int main(char[][] args)
{
Population testPop1 = new Population;
Population testPop2 = new Population;
Individual[40000] indi;
for(int i = 0; i < 1000000; i++)
{
writefln("Round %d", i);
testPop1.grow();
testPop2.grow();
version (loseMemory){
indi[] = testPop1.individuals ~ testPop2.individuals;
}
version (everythingOk){
indi[0..20000] = testPop1.individuals;
indi[20000..40000] = testPop2.individuals;}
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------
Ciao,
Bab
More information about the Digitalmars-d
mailing list